C 每行包含多个元素的读入文本文件
我需要读入一个文本文件“input.in”,以便根据id对代码运行排序功能。input.in文件包含一个id和文件名,总共8行。我知道我需要逐行读取输入文件(不确定我的代码是否正确)。但主要问题是fopen函数返回的结果是它找不到输入文件,即使它与保存在那里的源文件一起在桌面上 任何提示都将不胜感激C 每行包含多个元素的读入文本文件,c,file,file-io,input,nodes,C,File,File Io,Input,Nodes,我需要读入一个文本文件“input.in”,以便根据id对代码运行排序功能。input.in文件包含一个id和文件名,总共8行。我知道我需要逐行读取输入文件(不确定我的代码是否正确)。但主要问题是fopen函数返回的结果是它找不到输入文件,即使它与保存在那里的源文件一起在桌面上 任何提示都将不胜感激 int main() { int id; char node; char item[9], status; FILE *fp; if((fp = fopen("/Users/jacobsprag
int main()
{
int id;
char node;
char item[9], status;
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.txt", "r+")) == NULL)
{
printf("No such file\n");
exit(1);
}
while(42)
{
int ret = fscanf(fp, "%s %c", id, &node);
if(ret == 2)
printf("\n%s \t %c", id, node);
else if(errno != 0)
{
perror("scanf:");
break;
}
else if(ret == EOF)
{
break;
}
else
{
printf("No match.\n");
}
}
printf("\n");
if(feof(fp))
{
puts("EOF");
}
return 0;
}
以下是输入文件的内容:
8
4 Node1111
8 Node11111111
2 Node11
7 Node1111111
1 Node1
5 Node11111
6 Node111111
3 Node111
fopen
可能由于找不到文件以外的原因而失败,因此您应该检查errno
以查看问题所在。但是在这种情况下,正如BLUEPIXY所提到的,问题似乎是您键入了input.txt
而不是input.in
/1)在ops代码中有很多小OOP,
// 1) there were lots of little oops in the ops code,
// 2) the op skipped the detail that the first line contains
// a count of the number of following lines
// all of that is corrected in the following
#include <stdio.h>
#include <stdlib.h>
int main()
{
int id; // value read from file
char node[30]; // string read from file
//char item[9]; // if not commented, raises compiler warning about unused variable
//char status; // if not commented, raises compiler warning about unused variable
int ret; // returned value from fscanf
int lineCount = 0; // number of lines in file after first line
int i; // loop counter
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.in", "r")) == NULL)
{
// perror also outputs the value of errno and the results of strerror()
perror( "fopen failed for file: input.in");
exit(1);
}
// implied else, fopen successful
// get first line, which contains count of following lines
if( 1 != (ret = fscanf(fp, " %d", &lineCount)) )
{ // fscanf failed
perror( "fscanf"); // this also outputs the value of errno and the results of strerror()
exit( EXIT_FAILURE );
}
// implied else, fscanf successful for lineCount
for( i=0; i < lineCount; i++) // read the data lines
{
// note leading space in format string to consume white space (like newline)
if( 2 != (ret = fscanf(fp, " %d %s", &id, node)) )
{ // fscanf failed
// this also outputs the value of errno and the results of strerror()
perror( "fscanf for id and node failed");
break;
}
// implied else, fscanf successful for id and node
printf("\n%d\t %s", id, node);
} // end for
printf("\n");
if( EOF == ret )
{
puts("EOF");
} // endif
return 0;
} // end function: main
//2)op跳过了第一行包含的详细信息
//以下行数的计数
//所有这些都在下面进行了更正
#包括
#包括
int main()
{
int id;//从文件读取的值
char节点[30];//从文件中读取的字符串
//char项[9];//如果未注释,将引发有关未使用变量的编译器警告
//char status;//如果未注释,将引发有关未使用变量的编译器警告
int ret;//从fscanf返回的值
int lineCount=0;//文件中第一行之后的行数
int i;//循环计数器
文件*fp;
if((fp=fopen(“/Users/jacobsrague/Desktop/input.in”,“r”))==NULL)
{
//perror还输出errno的值和strerror()的结果
perror(“文件:input.in的fopen失败”);
出口(1);
}
//否则,fopen成功了
//获取第一行,其中包含以下行的计数
如果(1!=(ret=fscanf(fp、%d、&lineCount)))
{//fscanf失败
perror(“fscanf”);//这还输出errno的值和strerror()的结果
退出(退出失败);
}
//隐含的else,fscanf对lineCount成功
对于(i=0;i
请检查我的答案fscanf(fp、%s%c)、id和node)
错误。如果((fp=fopen(“……input.txt”,“r+”)==NULL)
:.txt
-->.in