无法使用fscanf将数据扫描到c中的数组中
Cannot scan data using fscanf into array in c
我正在编写一个可以标记学生成绩的程序。但是,在分析文件数据之前,我无法扫描以将文件数据输入到数组中。
这是我的输入数据结构:
BECDCBAADEBACBAEDDBE
2019000 BEAABEBCCDDEECXBEBXC
2019001 ABXBBDAXAEAAEAABECED
2019002 ABBCXCBBCBAXCCBEAEAD
第一行是正确答案。下面包括学号和他们的答案。目前我只想将数据保存在数组中。这是我试过的:
#include <stdio.h>
#include <string.h>
// Function Prototype
void ReadFile(char* correct_ans, int* number, char student_ans[100][20]);
int main(void)
{
char correct_ans[20], student_ans[100][20];
int student_number[100];
FILE *fp1;
fp1 = fopen("answer.txt", "r");
int temp;
char temp_answer[20];
fscanf(fp1, "%s\n", correct_ans);
for(int i = 0; i < 100; i++)
{
fscanf(fp1, "%d %s\n", &student_number[i], temp_answer);
strcpy(*(student_ans + i), temp_answer);
}
printf("%s\n", correct_ans);
for(int i = 0; i < 100; i++)
{
printf("%d %s\n", student_number[i], student_ans[i]);
}
return 0;
}
当我运行代码:
2019000 BEAABEBCCDDEECXBEBXCABXBBDAXAEAAEAABECEDABBCXCBBCBAXCCBEAEADACDEDCEDXECDABBCDACCCADCCEAAXDEEBACDBBXXAXCBEACCDACCCDBCADBDEAXXXBCDEXEBDAACXCDEXXEBEXBAABXBDBEDXDACAAACAXADXAAAABDDADCXEXDECXCBEEDCDXACDEBCACCECCACAADDXBCCXECDEXCEBBCEABADCAAEXXXECXDCBEEBACEBBEAEEDBBAABBDBDCBXEBABBDXAEDXCDBDEDDCXECDACBCDCECADBEDEXABXADXCDCDAACBEBECEAXADCDAEDAECECAAEBXEXBBACABCCEAXADDEEDECCXAEXBXEXBEAAEXABDXXXEAEBXEAACEXXDDABDADACAXXCDXXACAADXEDXDXBDACECBEEAECAEXABDBAABEBXBBCEXBCEACBCAXXBXBXEXACCCCAACXXXXCXEDECEBDCDDCEDEXDCXAECXEBXBDDBCXEDXDAXCCABXEBAEXEEXBCABXAXABECAECBXDXBDXCCDXDABEBXXBBDEAXECCCAEDDCXXABXCABXXCAEAXAEEECDEACXDXECDCDCBXAADCECBXDXDBDABAXABACEXXCEDBDEBDCDXBDBAEEEEEBCBCACXCBXBEXDDCDBXCADABXCACACBEBCEXBBCACDEDEXXCDXEBBCCXBDXEXBAEDCCCCXCEXABABDXEDADECEEEXABDBABBAABBBCBBADBEXDXECEXCXDADAXEEBABADECBECXXAAACCAAAXBAACBBXXBBXEXADCBBADXBDACCBDAXBDEBCAXABABBDACCACDABADADBXCADBADADCXCEACBBADAEEXEEEEEXBBBCEEABBXCDDDDEDEXAAEBCDEEXEAEXEECXCEBCBADDXDXEBEDCECDXBAEAAEBBADEEDEDDEXXBCDDAABADADDBAXBXECDDBEXBBDEDCBADEEXEECBDCXCDXACCBBXBDXBEBBCEEXEEXDDADECABEAXEDCEDDDBDXBAXECBECDBEXBCBADDXXBXBEXDCCDBDAECBBCCAEDEEBAXDXXCEAAEDXEBCDXXDXDAADADBEDCXXCBCABABAABAECDECACDDEBCCAACAEBXEXXCEBABXEDCEXBBBCAEXABAEXXBADBACEEBDAAEXDDECCXDCBXBEEAEAXEADACBECEEADAEXXXEXBXAECXEAECCCXADCDCBCDDBCAEBAABDBXXEDAAXCCXXDABCXBXDDCBCECCCEXCXXABEABECEDBXADECCBBCEECEDEBBBBEAAXXCXEXEXCDCCECCAEABXXBDCEAAEADDXXACDXCXBXXCADBAXABXXCDBEBXEBDBEBECBDXCXDDEEDCDAAXABCCEBDABDBXCXEBXAEBXDCXBBCBDDEBDCBBDDCDEEXACDBDXXDBAEEBEBCCCXEAACEBECEEEDEBXAEXECBCBECEDCBEAEXEDDADBCEADEXCECABCCDXAXAXECCEAXEBAEEAAXBEXEXEDCBXBXDBXEBCXEXBACCXXXBXXEADDXXAABECXBEEXXBCXDEXEXDCDACEADEXXCDCBXEDBCBAEADAEBDCCDAXAABXCADCADCBCEEAXBDCXDCACCCDCDXXCBXCDCECXEBAXBXACBBBDBBXCECBAXCXBECAAEDDCCDBCEXEBDAXEXDDCAXXCXEBXXEBCADXBBBBBXDEECXBBBDEEEDACDCEEAADDEEAEDAXACAXDXCEXBACCEEEXBCAEADXBEDEBXDXEEXEBCCABDCCBBDXDACXBXCXDABXDDEADDCDDBBCAAAADAXCDBXXXXACABCADXCEXBXDXEXXEXXCCDDCCEXDXBBBCCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
这只是结果的前面部分,好像所有同学的答案都是累加的,一直递减到最后一个:
2019095
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019096 DECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019097 ECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019098 CXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019099 ACCBDEBECABEBCDECBDD
我对 Whosebug 很陌生,不太确定如何编辑我的问题。很抱歉,但我的问题是
第一个正确答案打印不出来(是不是没有保存到数组?)
同学们的答案不断积累
我要的是correct_ans中要存入正确答案,根据答案扫描学号。
谢谢你的帮助
字符串的长度
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
大于sizeof(temp_answer)
。
来自
fscanf(fp1, "%d %s\n",
&student_number[i],
temp_answer);
开,结果未定义。
如果您用更大的数量替换硬编码常量 20
,它应该可以工作。
另外,这里不需要temp_answer
,直接在student_ans[i]
中阅读即可。
我正在编写一个可以标记学生成绩的程序。但是,在分析文件数据之前,我无法扫描以将文件数据输入到数组中。 这是我的输入数据结构:
BECDCBAADEBACBAEDDBE
2019000 BEAABEBCCDDEECXBEBXC
2019001 ABXBBDAXAEAAEAABECED
2019002 ABBCXCBBCBAXCCBEAEAD
第一行是正确答案。下面包括学号和他们的答案。目前我只想将数据保存在数组中。这是我试过的:
#include <stdio.h>
#include <string.h>
// Function Prototype
void ReadFile(char* correct_ans, int* number, char student_ans[100][20]);
int main(void)
{
char correct_ans[20], student_ans[100][20];
int student_number[100];
FILE *fp1;
fp1 = fopen("answer.txt", "r");
int temp;
char temp_answer[20];
fscanf(fp1, "%s\n", correct_ans);
for(int i = 0; i < 100; i++)
{
fscanf(fp1, "%d %s\n", &student_number[i], temp_answer);
strcpy(*(student_ans + i), temp_answer);
}
printf("%s\n", correct_ans);
for(int i = 0; i < 100; i++)
{
printf("%d %s\n", student_number[i], student_ans[i]);
}
return 0;
}
当我运行代码:
2019000 BEAABEBCCDDEECXBEBXCABXBBDAXAEAAEAABECEDABBCXCBBCBAXCCBEAEADACDEDCEDXECDABBCDACCCADCCEAAXDEEBACDBBXXAXCBEACCDACCCDBCADBDEAXXXBCDEXEBDAACXCDEXXEBEXBAABXBDBEDXDACAAACAXADXAAAABDDADCXEXDECXCBEEDCDXACDEBCACCECCACAADDXBCCXECDEXCEBBCEABADCAAEXXXECXDCBEEBACEBBEAEEDBBAABBDBDCBXEBABBDXAEDXCDBDEDDCXECDACBCDCECADBEDEXABXADXCDCDAACBEBECEAXADCDAEDAECECAAEBXEXBBACABCCEAXADDEEDECCXAEXBXEXBEAAEXABDXXXEAEBXEAACEXXDDABDADACAXXCDXXACAADXEDXDXBDACECBEEAECAEXABDBAABEBXBBCEXBCEACBCAXXBXBXEXACCCCAACXXXXCXEDECEBDCDDCEDEXDCXAECXEBXBDDBCXEDXDAXCCABXEBAEXEEXBCABXAXABECAECBXDXBDXCCDXDABEBXXBBDEAXECCCAEDDCXXABXCABXXCAEAXAEEECDEACXDXECDCDCBXAADCECBXDXDBDABAXABACEXXCEDBDEBDCDXBDBAEEEEEBCBCACXCBXBEXDDCDBXCADABXCACACBEBCEXBBCACDEDEXXCDXEBBCCXBDXEXBAEDCCCCXCEXABABDXEDADECEEEXABDBABBAABBBCBBADBEXDXECEXCXDADAXEEBABADECBECXXAAACCAAAXBAACBBXXBBXEXADCBBADXBDACCBDAXBDEBCAXABABBDACCACDABADADBXCADBADADCXCEACBBADAEEXEEEEEXBBBCEEABBXCDDDDEDEXAAEBCDEEXEAEXEECXCEBCBADDXDXEBEDCECDXBAEAAEBBADEEDEDDEXXBCDDAABADADDBAXBXECDDBEXBBDEDCBADEEXEECBDCXCDXACCBBXBDXBEBBCEEXEEXDDADECABEAXEDCEDDDBDXBAXECBECDBEXBCBADDXXBXBEXDCCDBDAECBBCCAEDEEBAXDXXCEAAEDXEBCDXXDXDAADADBEDCXXCBCABABAABAECDECACDDEBCCAACAEBXEXXCEBABXEDCEXBBBCAEXABAEXXBADBACEEBDAAEXDDECCXDCBXBEEAEAXEADACBECEEADAEXXXEXBXAECXEAECCCXADCDCBCDDBCAEBAABDBXXEDAAXCCXXDABCXBXDDCBCECCCEXCXXABEABECEDBXADECCBBCEECEDEBBBBEAAXXCXEXEXCDCCECCAEABXXBDCEAAEADDXXACDXCXBXXCADBAXABXXCDBEBXEBDBEBECBDXCXDDEEDCDAAXABCCEBDABDBXCXEBXAEBXDCXBBCBDDEBDCBBDDCDEEXACDBDXXDBAEEBEBCCCXEAACEBECEEEDEBXAEXECBCBECEDCBEAEXEDDADBCEADEXCECABCCDXAXAXECCEAXEBAEEAAXBEXEXEDCBXBXDBXEBCXEXBACCXXXBXXEADDXXAABECXBEEXXBCXDEXEXDCDACEADEXXCDCBXEDBCBAEADAEBDCCDAXAABXCADCADCBCEEAXBDCXDCACCCDCDXXCBXCDCECXEBAXBXACBBBDBBXCECBAXCXBECAAEDDCCDBCEXEBDAXEXDDCAXXCXEBXXEBCADXBBBBBXDEECXBBBDEEEDACDCEEAADDEEAEDAXACAXDXCEXBACCEEEXBCAEADXBEDEBXDXEEXEBCCABDCCBBDXDACXBXCXDABXDDEADDCDDBBCAAAADAXCDBXXXXACABCADXCEXBXDXEXXEXXCCDDCCEXDXBBBCCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
这只是结果的前面部分,好像所有同学的答案都是累加的,一直递减到最后一个:
2019095
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019096 DECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019097 ECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019098 CXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019099 ACCBDEBECABEBCDECBDD
我对 Whosebug 很陌生,不太确定如何编辑我的问题。很抱歉,但我的问题是
第一个正确答案打印不出来(是不是没有保存到数组?)
同学们的答案不断积累
我要的是correct_ans中要存入正确答案,根据答案扫描学号。 谢谢你的帮助
字符串的长度
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
大于sizeof(temp_answer)
。
来自
fscanf(fp1, "%d %s\n",
&student_number[i],
temp_answer);
开,结果未定义。
如果您用更大的数量替换硬编码常量 20
,它应该可以工作。
另外,这里不需要temp_answer
,直接在student_ans[i]
中阅读即可。