Xara
Xara

Reputation: 9098

C: Content searching in file

In the code below, I am doing following things:

  1. Get a file using fp and in that file search for a string written in double quotes
  2. Write the remaining file in another file named "NewData.txt"
  3. Repeat steps 1 and 2 utill all the strings written in double quotes are searched and placed in the buffer array.

but i am getting this error,

 ./my_script: line 9: 27310 Segmentation fault      ./a.out

I am not getting it where I am doing wrong in the code....


   char *read_quoted_string(char outbuff[], FILE *fp){
    char *buffer[1000];
    int ch;
    int i;
    int counter=0;
    int increment=0;

        int prev=ftell(fp);
    fseek(fp, 0L, SEEK_END);
    int lengthOfFile=ftell(fp);
    fseek(fp,prev,SEEK_SET);
    fprintf(stdout,"%d",lengthOfFile);
    while(lengthOfFile>0){

       while(EOF!=(ch=fgetc(fp)))
           if(ch == '"') break;

    for(i=0;EOF!=(ch=fgetc(fp));++i)
    {
        if(ch == '"') break;
        outbuff[i] = ch;
        }

    outbuff[i]='\0';


    ///////////////////////////////////////
    char filename3[] = "NewData.txt";

    FILE *file3 = fopen ( filename3, "w" );

    if(file3!=NULL){
    while(EOF!=(ch=fgetc(fp)))  
    fputc(ch,file3);

    }
    fclose(file3);




    buffer[increment]=outbuff;
    increment=increment+1;
    fp=file3;
    prev=ftell(fp);
        fseek(fp, 0L, SEEK_END);
    lengthOfFile=ftell(fp);
        fseek(fp,prev,SEEK_SET);
}
    return   buffer[increment];
}

Upvotes: 2

Views: 158

Answers (4)

BLUEPIXY
BLUEPIXY

Reputation: 40145

maybe, remake like this

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void){
    FILE *in, *out;
    char *buffer[1000];
    int increment = 0;

    if(NULL!=(in =fopen("data.txt", "r"))){
        if(NULL!=(out=fopen("NewData.txt", "w"))){
            char buff[1000];
            int i=0, ch, in_str = 0;
            while(EOF!=(ch=fgetc(in))){
                switch(ch){
                case '"':
                    if(in_str){
                        buff[i] = '\0';
                        buffer[increment++] = strdup(buff);
                        i=0;//reset
                        if(increment == 1000){
                            fprintf(stderr, "Size of the buffer is insufficient!");
                            fclose(in);fclose(out);
                            return -1;
                        }
                    }
                    in_str = !in_str;
                    break;
                default:
                    if(in_str){
                        buff[i++]=ch;
                    } else {
                        fputc(ch, out);
                    }
                }
            }
            fclose(out);
        }
        fclose(in);
    }
    {
        int i;
        for(i=0;i<increment;++i)
            printf("%s\n", buffer[i]);
        //dealloc
        for(i=0;i<increment;++i)
            free(buffer[i]);
    }
    return 0;
}

Upvotes: 0

fkl
fkl

Reputation: 5535

Unless i am missing something, the following code is not correct.

while(EOF!=(ch=fgetc(fp)))
    if(ch == '"') break;

for(i=0;EOF!=(ch=fgetc(fp));++i)
{
    if(ch == '"') break;
    outbuff[i] = ch;
}

First you loop until the end of file OR ''. Once you are out of this loop you again use fgetc to read from the same pointer?

I believe you need to reset your pointer to the start of file OR you could completely remove the while loop. I don't see any thing useful getting out of it.

Upvotes: 0

log0
log0

Reputation: 10917

Can you use C++ (especially C++11) ? If yes you should rather use std::regex_search instead of reimplementing something in C.

std::string s ("blablabla \"bla bla\" bla \"bla \"");
std::smatch m;
std::regex e ("\"([^\"]*)");

while (std::regex_search (s,m,e)) {
 for (auto x:m) std::cout << x << " ";
 std::cout << std::endl;
}

Upvotes: 0

Orel Eraki
Orel Eraki

Reputation: 12196

How can you go to the end of the file using SEEK_END and then read from that location using your loops. you first need to set the cursor into the start location again and then loop the file.

Upvotes: 2

Related Questions