按字符,行用C读取文件字符由行 [英] Reading file character by character, line by line in C

查看:203
本文介绍了按字符,行用C读取文件字符由行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个行的文件。每一行都有一个空格隔开的两个数字。的我需要使用此code,打印的第一行,由行的 <打印整个文件,做起来的的/ p>

 
{
   的fscanf(FP,%C,和C);
   如果(C =='')
     打破;
   的printf(%C,C);
}
而(C =''!);做
{
   的fscanf(FP,%C,和C);
   的printf(%C,C);
}
而(C ='\\ n'!);

我尝试使用与fgets但陷入无限循环。

 而(与fgets(buf中,sizeof的BUF,FP)!= NULL)//假设BUF可以处理该行lenght
{
   // code以上
}

为什么我不能用像与fgets到逐行打印?

样品

输入:

10 5003
 20 320
 4003 200

输出

  10 5003
20 320
4003 200


解决方案

很明显,我不能使用您的确切code,或者你就不会问这个问题,但是这是一个类似的想法,做了一些不同,并且还使用与fgets 这是给你造成麻烦。它的工作原理通过搜索每一行数字和非数字。请注意,我有的#define MAXLEN 2000 要大方,因为在previous问题,你说的每一个号码都可以有500个数字,所以该行可以至少1000个字符。

 的#include&LT;&stdio.h中GT;
#包括LT&;&文件ctype.h GT;#定义MAXLEN 2000INT主要(无效)
{
    FILE * FP;
    焦线[MAXLEN]
    字符* PTR;
    如果((FP = FOPEN(test.txt的,RT))== NULL)
        返回0; //或其他行动    而(与fgets(行,MAXLEN,FP)!= NULL){
        PTR =行;
        //第一个数字
        而(* PTR和放大器;&安培;!ISDIGIT(* PTR)){//跳过非数字字符
            PTR ++;
        }
        而(* PTR和放大器;&安培; ISDIGIT(* PTR)){
            的printf(%C,* PTR ++); //打印数字
        }
        的printf();        //第二个数字
        而(* PTR和放大器;&安培;!ISDIGIT(* PTR)){//跳过非数字字符
            PTR ++;
        }
        而(* PTR和放大器;&安培; ISDIGIT(* PTR)){
            的printf(%C,* PTR ++); //打印数字
        }
        的printf(\\ n);    }
    FCLOSE(FP);
    返回0;
}

修改您可以使它更加简洁这样,一个循环读一组数字:

 的char *终止=\\ n; //第一个数字空间结束,第二个新行
INT I;
而(与fgets(行,MAXLEN,FP)!= NULL){
    PTR =行;
    对于(I = 0; I&2;我++){
        而(* PTR和放大器;&安培;!ISDIGIT(* PTR)){//跳过非数字字符
            PTR ++;
        }
        而(* PTR和放大器;&安培; ISDIGIT(* PTR)){
            的printf(%C,* PTR ++); //打印数字
        }
        的printf(%C,终止[I]); //空格或换行
    }
}

节目输出(从输入):

  10 5003
20 320
4003 200

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 != '\n');

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

解决方案

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("\n");

    }
    fclose(fp);
    return 0;
}

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

char *terminate = " \n";                    // 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

这篇关于按字符,行用C读取文件字符由行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆