C语言 在c编程中读取文本文件并存储在数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27856886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read text file and store in array in c programming
提问by mehr20
I want read text file and store in array then show.
我想读取文本文件并存储在数组中然后显示。
This is my code:
这是我的代码:
int i = 0, line = 5;
char ch[100];
FILE *myfile;
myfile = fopen("test.txt","r");
if (myfile== NULL)
{
printf("can not open file \n");
return 1;
}
while(line--){
fscanf(myfile,"%s",&ch[i]);
i++;
printf("\n%s", &ch[i]);
}
fclose(myfile);
return 0;
}
This is my text:
这是我的文字:
test 123562
测试 123562
856
856
59986
59986
But result:
但结果:
est
美东时间
2356
2356
56
56
9986
9986
What is wrong? :(
怎么了?:(
回答by haccks
ch[i]is holding a single character. Statement fscanf(myfile,"%s",&ch[i]);will scan string to ch[i]which can hold only one character. There is no place for '\0'which leads your program to undefined behavior.
Change
ch[i]持有一个字符。语句fscanf(myfile,"%s",&ch[i]);将扫描ch[i]只能容纳一个字符的字符串。没有任何地方'\0'会导致您的程序出现未定义的行为。改变
fscanf(myfile,"%s",&ch[i]);
to
fscanf(myfile,"%s",ch);
fscanf(myfile,"%s",&ch[i]);
到
fscanf(myfile,"%s",ch);
Previous answer was wrong. Behavior of program is well defined but you are scanning the file in a wrong manner. Your program will work as expected if you place i++;after printfstatement.
之前的答案是错误的。程序的行为定义明确,但您以错误的方式扫描文件。如果您放置i++;在printf语句之后,您的程序将按预期工作。
while(line--){
fscanf(myfile,"%s",&ch[i]);
printf("\n%s", &ch[i]);
i++;
}
The reason is that &ch[i]is a pointer to the ithelement of the array and string will be stored in array starting at position i. For the input given, this will work because the given array is large enough to hold the string.
原因是它&ch[i]是指向数组第i个元素的指针,字符串将存储在数组中,从位置 开始i。对于给定的输入,这将起作用,因为给定的数组足够大以容纳字符串。
You can do this as:
你可以这样做:
while(line--){
fscanf(myfile,"%s",ch);
printf("\n%s", ch);
i++;
}
but it will overwrite the array cheach time a string is scanned to it. Better to use a two dimensional array to store strings and read file with fgets.
但ch每次扫描字符串时它都会覆盖数组。最好使用二维数组来存储字符串并使用fgets.
回答by unwind
You're not going to be able to fit five lines in the single char ch[100]array; that's just an array of 100 characters.
您将无法在单个char ch[100]数组中放入五行;这只是一个 100 个字符的数组。
You can make it an array of arrays, i.e. char ln[5][100]which will give you room for five lines of 100 characters each.
您可以将其设为数组数组,即char ln[5][100]这将为您提供五行每行 100 个字符的空间。
Then you of course need to index into that array in the loop, i.e.:
然后你当然需要在循环中索引到该数组,即:
for(int i = 0; i < 5; ++i)
{
if(fgets(ln[i], sizeof ln[i], myfile) == NULL)
{
fprintf(stderr, "Read error on line %d\n", i);
exit(1);
}
}
This uses fgets()which is muchbetter suited at reading in whole lines; fscanf()will stop at whitespace with %swhich is seldom what you want.
这种用途fgets()是很多在全行阅读更适合; fscanf()将停在%s你很少想要的空白处。
回答by Karthikeyan.R.S
There is no need to use the ampersand in the scanf while getting the string. Make that into like this.
获取字符串时无需在 scanf 中使用&符号。把它变成这样。
fscanf(myfile,"%s",&ch[i]);
to
到
fscanf(myfile,"%s",ch);
&ch[i]It will get the character for i th position in that array. If you want to get like that you can use the %cinstead of %s. And change this one to.
&ch[i]它将获取该数组中第 i 个位置的字符。如果你想这样,你可以使用%c代替%s。并将此更改为。
printf("\n%s", ch);
While printing the string when you use the ampersand(&) that will access the address of that variable.
当您使用将访问该变量的地址的&符号(&)时打印字符串。
回答by khaliq
- The program developed must be able to read the input files containing matrix A and matrix B using fopen function a. Matrix A and B of different size may be stored in different input file (if required).
- Scan and assign matrix A and B as array using fscanf function and for loop
- Perform matrix operations a. Add matrix A and B b. Subtract matrix A and B c. Multiply matrix A and B
- Use conditional statement if or switch for switching between 3, 4 and 5 elements matrix.
- Print all input matrices and results obtained in a new file called output.dat using fprintf function.
- The output.dat file must have a header with the following information: a. Student name b. Student matric number c. Class section d. Lecturer name e. Project title
- Below the header, the output file must contain matrix A and B and the results from matrix operation. Use matrix A and B as given below:
- 开发的程序必须能够使用 fopen 函数 a 读取包含矩阵 A 和矩阵 B 的输入文件。不同大小的矩阵 A 和 B 可以存储在不同的输入文件中(如果需要)。
- 使用 fscanf 函数和 for 循环扫描并将矩阵 A 和 B 分配为数组
- 执行矩阵运算 添加矩阵 A 和 B b。减去矩阵 A 和 B c。将矩阵 A 和 B 相乘
- 使用条件语句 if 或 switch 在 3、4 和 5 元素矩阵之间切换。
- 使用 fprintf 函数打印在名为 output.dat 的新文件中获得的所有输入矩阵和结果。
- output.dat 文件必须具有包含以下信息的标题:学生姓名 B. 学生矩阵号 C. 班级 D. 讲师姓名 e. 项目名称
- 在标题下方,输出文件必须包含矩阵 A 和 B 以及矩阵运算的结果。使用矩阵 A 和 B,如下所示:

![C语言 对于 main() 的第二个参数,char *argv[] 和 char **argv 之间的区别](/res/img/loading.gif)