在C语言中逐字逐行读取文件
我有一个多行的文件。每行有两个数字,用空格隔开。我需要使用这段代码打印第一行,来打印整个文件,但是要逐行打印 我试图使用FGET,但陷入了无限循环在C语言中逐字逐行读取文件,c,file,io,C,File,Io,我有一个多行的文件。每行有两个数字,用空格隔开。我需要使用这段代码打印第一行,来打印整个文件,但是要逐行打印 我试图使用FGET,但陷入了无限循环 while(fgets(buf, sizeof buf, fp) != NULL) // assuming buf can handle the line lenght { //code above } 为什么我不能用这样的FGET逐行打印 样品 输入: 105003 20 320 4003200 输出 10 5003 20 320 400
while(fgets(buf, sizeof buf, fp) != NULL) // assuming buf can handle the line lenght
{
//code above
}
为什么我不能用这样的FGET逐行打印
样品
输入:
105003
20 320
4003200
输出
10 5003
20 320
4003 200
很明显,我不能使用你的确切代码,或者你不会问这个问题,但这是一个类似的想法,做得有点不同,而且还使用了给你带来麻烦的
fgets
。它的工作原理是在每行中搜索数字和非数字。请注意,我将MAXLEN 2000定义为慷慨的,因为在上一个问题中,您说每个数字可以有500位数字,因此行可能至少有1000个字符
#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;
}
如果我们将printf(fp,“%c”,c)替换为printf(“%c”,c)(因为我们不是在文件中打印,对吗?),下面的示例应该完成这项工作(它创建了包含十行的测试文件)
#包括
#包括
int main(){
字符c;
文件*fp;
fp=fopen(“test.txt”,“w+”);
对于(int i=0;i<10;i++)
{
fprintf(fp,“测试线%i\n”,i);
}
倒带(fp);
做{
做{
fscanf(fp、%c、&c);
如果(c='')
打破
printf(“%c”,c);
}而(c!=”;
做{
fscanf(fp、%c、&c);
printf(“%c”,c);
}而(c!='\n');
}而((c=fgetc(fp))!=EOF?printf(“%c”,c):0);
fclose(fp);
}
请看这篇文章,它还规定数字可以是任意大小,最多有500位。这个问题是我提出的。没有任何答案可以解决这个具体问题。如果500位的可能性仍然存在,请在任何人建议使用int
或fscanf(fp,%d%d,&m,&n)之前将其添加到问题中代码>问题强调需要使用第一个代码。这还不够吗?所以你不需要储存任何东西?只是输出数字?请提供示例输入文件和所需输出。感谢回复。比较指针和整数时不是最后一个吗?(fgetc(fp)!=NULL)感谢您发现这个错误,最后一行应该是“while(fgetc(fp)!=EOF)”而不是“while(fgetc(fp)!=NULL&&!feof(fp))”(我刚刚编辑了这篇文章)很抱歉,我在回复中犯了一个错误,do while循环的条件应该是“while((c=fgetc(fp))!=EOF?printf(%c),c):0;”,即我们在c中读取一个字符,如果它不是EOF-我们打印它,否则我们通过返回0作为?:运算符的结果来结束循环。
#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;
}
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
}
}
10 5003
20 320
4003 200
#include <stdio.h>
#include <stdlib.h>
int main() {
char c;
FILE* fp;
fp = fopen("test.txt", "w+");
for (int i = 0; i < 10; i++)
{
fprintf(fp, "Test line %i\n", i);
}
rewind(fp);
do {
do {
fscanf(fp, "%c", &c);
if (c == ' ')
break;
printf("%c", c);
} while (c != ' ');
do {
fscanf(fp, "%c", &c);
printf("%c", c);
} while (c != '\n');
}while ((c=fgetc(fp))!=EOF?printf("%c", c):0);
fclose(fp);
}