Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

Reading file character by character, line by line in C

I have a file with multiple lines. Each line has two numbers separated by a white-space. I need to use this code, which prints the first line, to print the whole file, but doing it line by line

do
{
   fscanf(fp,"%c", &c);
   if(c == ' ')
     break;
   printf("%c", c);
}
while (c != ' ');

do
{
   fscanf(fp, "%c", &c);
   printf("%c", c);
}
while( c != '
');

I tried to use fgets but got into an infinite loop.

while(fgets(buf, sizeof buf, fp) != NULL) // assuming buf can handle the line lenght
{
   //code above
}

Why i cant use fgets like that to print it line by line?

Sample

Input:

10 5003 20 320 4003 200

Output

10 5003
20 320
4003 200
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Obviously I can't use your exact code, or you would not have asked the question, but this is a similar idea, done a bit differently, and also using the fgets which was causing you trouble. It works by searching each line for digits, and non-digits. Note that I have #define MAXLEN 2000 to be generous, because in the previous question, you say each number can have 500 digits, so the line might be at least 1000 characters.

#include <stdio.h>
#include <ctype.h>

#define MAXLEN  2000

int main(void)
{
    FILE *fp;
    char line[MAXLEN];
    char *ptr;
    if((fp = fopen("test.txt", "rt")) == NULL)
        return 0;                               // or other action

    while(fgets(line, MAXLEN, fp) != NULL) {
        ptr = line;
        // first number
        while(*ptr && !isdigit(*ptr)) {        // skip non-digits
            ptr++;
        }
        while(*ptr && isdigit(*ptr)) {
            printf("%c", *ptr++);              // print digits
        }
        printf(" ");

        // second number
        while(*ptr && !isdigit(*ptr)) {        // skip non-digits
            ptr++;
        }
        while(*ptr && isdigit(*ptr)) {
            printf("%c", *ptr++);              // print digits
        }
        printf("
");

    }
    fclose(fp);
    return 0;
}

EDIT you could make it more concise like this, with a loop to read each set of digits:

char *terminate = " 
";                    // 1st number ends with space, 2nd with newline
int i;
while(fgets(line, MAXLEN, fp) != NULL) {
    ptr = line;
    for(i=0; i<2; i++) {
        while(*ptr && !isdigit(*ptr)) {     // skip non-digits
            ptr++;
        }
        while(*ptr && isdigit(*ptr)) {
            printf("%c", *ptr++);           // print digits
        }
        printf("%c", terminate[i]);         // space or newline
    }
}

Program output (from your input):

10 5003
20 320
4003 200

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...