在C中使用fscanf将文本文档的内容扫描到数组中
我需要扫描文本文档的内容,即:在C中使用fscanf将文本文档的内容扫描到数组中,c,arrays,scanf,C,Arrays,Scanf,我需要扫描文本文档的内容,即: 77, 66, 80, 81 40, 5, 35, -1 51, 58, 62, 34 0, -1, 21, 18 61, 69, 58, 49 81, 82, 90, 76 44, 51, 60, -1 64, 63, 60, 66 -1, 38, 41, 50 69, 80, 72, 75 将每个数字放入一个数组中,每个数字都在它自己的块中,然后读取每个块以确定内容是什么。我觉得我已经完成了处理部分,但我无法确定如何将一个数字分配给数组中的一个块。这是我
77, 66, 80, 81
40, 5, 35, -1
51, 58, 62, 34
0, -1, 21, 18
61, 69, 58, 49
81, 82, 90, 76
44, 51, 60, -1
64, 63, 60, 66
-1, 38, 41, 50
69, 80, 72, 75
将每个数字放入一个数组中,每个数字都在它自己的块中,然后读取每个块以确定内容是什么。我觉得我已经完成了处理部分,但我无法确定如何将一个数字分配给数组中的一个块。这是我到目前为止所拥有的
int main()
{
FILE * marks;
marks = fopen("marks.txt", "r");
int marksArray[4][10], x, y, greaterThan70 = 0, between60and69 = 0, between50and59 = 0, between40and49 = 0, lessThan39 = 0, notSubmitted = 0;
//Scanning in the contents
while ((fscanf(marks, "%d", &marksArray[x][y])) != EOF)
{
x++;
y++;
}
//Processing the array
for(x = 0; x < 4; x++)
{
for(y = 0; y < 10; y++)
{
if(marksArray[x][y] == -1)
{
notSubmitted++;
}
else if(marksArray[x][y] >= 0 && marksArray[x][y] <= 39)
{
lessThan39++;
}
else if(marksArray[x][y] >= 40 && marksArray[x][y] <= 49)
{
between40and49++;
}
else if(marksArray[x][y] >= 50 && marksArray[x][y] <= 59)
{
between50and59++;
}
else if(marksArray[x][y] >= 60 && marksArray[x][y] <= 69)
{
between60and69++;
}
else
{
greaterThan70++;
}
break;
}
}
printf("The number of marks greater than 70 was %d", greaterThan70);
printf("The number of marks between than 60 and 69 was %d", between60and69);
printf("The number of marks between than 50 and 59 was %d", between50and59);
printf("The number of marks between than 40 and 49 was %d", between40and49);
printf("The number of marks less than 39 was %d", lessThan39);
printf("The number of coursework submissions not handed in was %d", notSubmitted);
}
首先,你真的确信C++不能做到这一点吗?< /P> 其次,如果必须用C语言编写,那么应该查看文件并计算每行的行数和逗号数。合计 然后,您可以逐行重复该文件,并计算逗号的数目(如果不是像您的问题中那样每行都有4个数字)
(fscanf(marks, "%d", &marksArray[x][y])
被第一个逗号堵住了。您将需要更多类似于:
(fscanf(marks, "%d, ", &marksArray[x][y])
#include <stdio.h>
int main()
{
FILE * marks;
marks = fopen("in.txt", "r");
int marksArray[4][10], x, y, greaterThan70 = 0, between60and69 = 0, between50and59 = 0, between40and49 = 0, lessThan39 = 0, notSubmitted = 0;
//Scanning in the contents
//No need for EOF if you know the size ahead of time
for(y=0; y<10; y++)
{
for(x=0; x<3; x++)
{
fscanf(marks, "%d, ", &marksArray[x][y]);
}
//To deal with the annoying lack of comma at the end of the line
//Be wary of EoL variance, the difference between \n and \r\n, bloody windows...
fscanf(marks, "%d\n", &marksArray[x][y]);
}
//Processing the array
for(x = 0; x < 4; x++)
{
for(y = 0; y < 10; y++)
{
if(marksArray[x][y] == -1)
{
notSubmitted++;
}
else if(marksArray[x][y] >= 0 && marksArray[x][y] <= 39)
{
lessThan39++;
}
else if(marksArray[x][y] >= 40 && marksArray[x][y] <= 49)
{
between40and49++;
}
else if(marksArray[x][y] >= 50 && marksArray[x][y] <= 59)
{
between50and59++;
}
else if(marksArray[x][y] >= 60 && marksArray[x][y] <= 69)
{
between60and69++;
}
else
{
greaterThan70++;
}
break;
}
}
printf("The number of marks greater than 70 was %d\n", greaterThan70);
printf("The number of marks between than 60 and 69 was %d\n", between60and69);
printf("The number of marks between than 50 and 59 was %d\n", between50and59);
printf("The number of marks between than 40 and 49 was %d\n", between40and49);
printf("The number of marks less than 39 was %d\n", lessThan39);
printf("The number of coursework submissions not handed in was %d\n", notSubmitted);
}
逗号和空格也代表新行,这一点很重要。除了最后一个字符不是以逗号结尾,所以这不太合适
是的,还有这个:
while ((fscanf(marks, "%d", &marksArray[x][y])) != EOF)
{
x++;
y++;
}
如果成功,它将以以下模式遍历阵列:
X000
0X00
00X0
000X
您想要的更像:
(fscanf(marks, "%d, ", &marksArray[x][y])
#include <stdio.h>
int main()
{
FILE * marks;
marks = fopen("in.txt", "r");
int marksArray[4][10], x, y, greaterThan70 = 0, between60and69 = 0, between50and59 = 0, between40and49 = 0, lessThan39 = 0, notSubmitted = 0;
//Scanning in the contents
//No need for EOF if you know the size ahead of time
for(y=0; y<10; y++)
{
for(x=0; x<3; x++)
{
fscanf(marks, "%d, ", &marksArray[x][y]);
}
//To deal with the annoying lack of comma at the end of the line
//Be wary of EoL variance, the difference between \n and \r\n, bloody windows...
fscanf(marks, "%d\n", &marksArray[x][y]);
}
//Processing the array
for(x = 0; x < 4; x++)
{
for(y = 0; y < 10; y++)
{
if(marksArray[x][y] == -1)
{
notSubmitted++;
}
else if(marksArray[x][y] >= 0 && marksArray[x][y] <= 39)
{
lessThan39++;
}
else if(marksArray[x][y] >= 40 && marksArray[x][y] <= 49)
{
between40and49++;
}
else if(marksArray[x][y] >= 50 && marksArray[x][y] <= 59)
{
between50and59++;
}
else if(marksArray[x][y] >= 60 && marksArray[x][y] <= 69)
{
between60and69++;
}
else
{
greaterThan70++;
}
break;
}
}
printf("The number of marks greater than 70 was %d\n", greaterThan70);
printf("The number of marks between than 60 and 69 was %d\n", between60and69);
printf("The number of marks between than 50 and 59 was %d\n", between50and59);
printf("The number of marks between than 40 and 49 was %d\n", between40and49);
printf("The number of marks less than 39 was %d\n", lessThan39);
printf("The number of coursework submissions not handed in was %d\n", notSubmitted);
}
别听那个家伙说,C语言很棒,是解决所有问题的正确语言。永远 此行:int-marksArray[4][10]有一个错误,因为行在前,列在后。因此,给定示例文件内容,行应为:int marksArray[10][4],然后需要更新“x”和“y”的“for”循环上的限制值以匹配。读取数组值时,当列计数y达到其“limit”时,代码只应增加行计数x,因为代码对数组的内容不做任何处理,总的来说,最好只有一个int变量,读入该int变量,然后处理该单个值,然后循环读入该单个变量的下一个输入。此行:while fscanfmarks,%d,&marksArray[x][y]!=EOF有一个问题,因为“,”不是空白。因此,可能无法读取超过第一个int值的值。建议一次读取一行fgets,然后使用strtok…,和atoi之类的方法解析该行以提取连续的整数,这也将允许文件中一行中的整数数量不同。您好,感谢您的回复!代码似乎是大部分工作,但它似乎只读取第一行数字,即77、66、80、81。知道为什么它不能沿y轴递增吗?@SexRobomb。是的,这是底部for循环的中断。它在有机会,你知道,循环之前就脱离了循环。只要把它扔掉就行了。还有,投票并接受。我渴望这些积分,晚上睡不着觉,想着如何获得更多。我的朋友们。哈哈,谢谢,整理好了!您赢得了宝贵的代表积分: