0
这是我正在使用的程序,我试图读取一个简单的文本文件并将输出存储在结构中。我目前面临的问题是在while循环中它不起作用。文件句柄似乎读取文件。此外,输入文件已使用给定文本使用简单的Windows记事本创建 - 这可能是一个问题?创建可以使用c读取的文本文件fscanf
#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
编辑:更正拼写错误。
不要响应答案编辑你的问题。这会导致混淆并使提供的答案无效。 – hmjd 2013-03-28 09:57:00
这是一个错字。我的实际程序是写入。我为这个错误道歉。 – abhig10 2013-03-28 09:58:53
@ abhig10,检查问题的标题,可能还有一个错字。我想你想'fscanf'而不是'scanf'。 – Dipto 2013-03-28 10:05:19