0

I want to read a line from file in C. Eg in file I have following data

"It was a good

day

but suddenly
everything"

Rightnow, my code just reads line by line but as mentioned in above example I want to read data from the starting inverted commas (") till the ending inverted commas (") and then write that whole string (like "It was a good day but suddenly everything") into another file. i only need help in reading these above lines from starting inverted commas till ending inverted commas. Please guide me which functions in C will help me to do that.

Rightnow, I am just reading data line by line

FILE *file = fopen ( filename, "r" );

 if (file != NULL )
  {
   char line [1000];
   while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */
    {
    //do something
     }
    }
Xara
  • 8,748
  • 16
  • 52
  • 82

4 Answers4

3
#include <stdio.h>

char *read_quoted_string(char outbuff[], FILE *fp){
    int ch, i;
    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';
    return outbuff;
}

int main(void){
    FILE *file = fopen("data.txt", "r" );

    if (file != NULL ){
        char buff [1000];
        printf("%s", read_quoted_string(buff, file));

        fclose(file);
    }
    return 0;
}

Also repeats

    if (file != NULL ){
        int i=1;//sequence number
        char buff [1000];
        while(*read_quoted_string(buff, file)){//empty is "" (*"" == '\0')
            printf("%2d:%s\n", i++, buff);
        }
        fclose(file);
    }
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • space character should be reduce ? – BLUEPIXY Jun 12 '13 at 18:50
  • @BLUEPIXY this code works for just one statement under double quotes but I have lot of such statements in my file which i want to read.How can i extend this code to do that. Do I need to add a while loop which will read a file line by line? – Xara Jun 13 '13 at 07:55
  • @Zara Those who were treated by the state in each character rather than processing each row from must be combined later in such cases when you read each row is simple. It should be better if it can take action on more than one just call more than once simply because it skip first case of this code. – BLUEPIXY Jun 13 '13 at 08:05
2

You may want to use fgetc to read one character at a time:

  • Allocate a buffer that is large enough
  • Start reading from the file char by char
  • If you find a " start saving next chars into the buffer
  • Keep saving into the buffer until you reach an other "
  • Terminate you string with a '\0'
  • If you need to remove new lines and have all chars between two " in the same line then just don't save '\r' and '\r' and save a simple space ' ' instead.
  • Repeat until you have consumed the hole file, i.e. fgetc return EOF.
A4L
  • 17,353
  • 6
  • 49
  • 70
1

You can use the following code to do this:

#include <stdio.h>

void copyToAnother(FILE *inFile, FILE *outFile)
{
    int ch, flag = 0;
    while(EOF!=(ch=fgetc(inFile)))
   {
        if(ch != '"' && toggle)
        {
          fputc(ch,outFile);
        }
        else
        {
           toggle = toggle ^ 1;
        }
}

int main(void)
{
    FILE *inFile  = fopen("in.txt", "r" );
    FILE *outFile = fopen("out.txt", "w" );

    if (inFile != NULL && outFile != NULL)
    {
        copyToAnother(inFile, outFile);
        fclose(inFile);
        fclose(outFile);
    }
    return 0;
}

Note: In this code. It'll write all the data between all " ".

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

You could just use 'strcat' to concatenate your arrays of chars. See that post for a good example : How do I concatenate const/literal strings in C?.

Community
  • 1
  • 1
Led
  • 71
  • 1
  • 3