[英]Creating a text file that could be read using c fscanf
这是我尝试读取一个简单文本文件并将输出存储在结构中的程序。 我目前面临的问题是在while循环中无法正常工作。 文件句柄似乎正在读取文件。 另外,输入文件是使用带有给定文本的简单Windows记事本创建的-这可能是问题吗?
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct studyCentre {
char stdCode[10], stdName[40], regName[40], coord[20], prgin[20], address[35], email[40], webs[45];
int ph;
} std;
int flag=0;
void main() {
FILE *fp;
fp = fopen("studyCentre.txt", "r+");
if (fp==NULL) {
printf("No file found");
exit(0);
}
while(feof(fp)!=0) {
//while(!fp.EOF()) {
fscanf(fp, "%s%s%s%s%s%s%s%s%d", std.stdCode, std.stdName, std.regName, std.coord, std.prgin, std.address, std.email, std.webs, &std.ph);
printf("\n Study Center Name :: %s \n Regional Centre Name :: %s \n Coordinator Name = %s \n Program incharge Name :: %s Address %s \n Email :: %s \n Website :: %s \n Phone :: %d", std.stdName, std.regName, std.coord, std.prgin, std.address, std.email, std.webs, &std.ph);
flag=1;
}
if (flag==0) {
printf ("No record found");
}
fclose(fp);
}
产量
No record found
这是我的文本文件中的文本。
111
a
b
c
d
e
f
g
h
122
编辑:更正了一个错字。
另外,您还需要查看feof函数返回的内容:
返回:
如果设置了与流关联的文件结束指示符,则返回非零值。 否则,返回零。
你应该写
while(feof(fp)==0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.