I am writing a program that can mark the students' result. However I have problem to scan to input file data into array before I analyze them. This is my input data structure:
BECDCBAADEBACBAEDDBE
2019000 BEAABEBCCDDEECXBEBXC
2019001 ABXBBDAXAEAAEAABECED
2019002 ABBCXCBBCBAXCCBEAEAD
The first line is the correct answer. Below included the student number and their answer. Currently I just want to save the data inside array. This is what I tried:
#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;
}
When I run the code:
2019000 BEAABEBCCDDEECXBEBXCABXBBDAXAEAAEAABECEDABBCXCBBCBAXCCBEAEADACDEDCEDXECDABBCDACCCADCCEAAXDEEBACDBBXXAXCBEACCDACCCDBCADBDEAXXXBCDEXEBDAACXCDEXXEBEXBAABXBDBEDXDACAAACAXADXAAAABDDADCXEXDECXCBEEDCDXACDEBCACCECCACAADDXBCCXECDEXCEBBCEABADCAAEXXXECXDCBEEBACEBBEAEEDBBAABBDBDCBXEBABBDXAEDXCDBDEDDCXECDACBCDCECADBEDEXABXADXCDCDAACBEBECEAXADCDAEDAECECAAEBXEXBBACABCCEAXADDEEDECCXAEXBXEXBEAAEXABDXXXEAEBXEAACEXXDDABDADACAXXCDXXACAADXEDXDXBDACECBEEAECAEXABDBAABEBXBBCEXBCEACBCAXXBXBXEXACCCCAACXXXXCXEDECEBDCDDCEDEXDCXAECXEBXBDDBCXEDXDAXCCABXEBAEXEEXBCABXAXABECAECBXDXBDXCCDXDABEBXXBBDEAXECCCAEDDCXXABXCABXXCAEAXAEEECDEACXDXECDCDCBXAADCECBXDXDBDABAXABACEXXCEDBDEBDCDXBDBAEEEEEBCBCACXCBXBEXDDCDBXCADABXCACACBEBCEXBBCACDEDEXXCDXEBBCCXBDXEXBAEDCCCCXCEXABABDXEDADECEEEXABDBABBAABBBCBBADBEXDXECEXCXDADAXEEBABADECBECXXAAACCAAAXBAACBBXXBBXEXADCBBADXBDACCBDAXBDEBCAXABABBDACCACDABADADBXCADBADADCXCEACBBADAEEXEEEEEXBBBCEEABBXCDDDDEDEXAAEBCDEEXEAEXEECXCEBCBADDXDXEBEDCECDXBAEAAEBBADEEDEDDEXXBCDDAABADADDBAXBXECDDBEXBBDEDCBADEEXEECBDCXCDXACCBBXBDXBEBBCEEXEEXDDADECABEAXEDCEDDDBDXBAXECBECDBEXBCBADDXXBXBEXDCCDBDAECBBCCAEDEEBAXDXXCEAAEDXEBCDXXDXDAADADBEDCXXCBCABABAABAECDECACDDEBCCAACAEBXEXXCEBABXEDCEXBBBCAEXABAEXXBADBACEEBDAAEXDDECCXDCBXBEEAEAXEADACBECEEADAEXXXEXBXAECXEAECCCXADCDCBCDDBCAEBAABDBXXEDAAXCCXXDABCXBXDDCBCECCCEXCXXABEABECEDBXADECCBBCEECEDEBBBBEAAXXCXEXEXCDCCECCAEABXXBDCEAAEADDXXACDXCXBXXCADBAXABXXCDBEBXEBDBEBECBDXCXDDEEDCDAAXABCCEBDABDBXCXEBXAEBXDCXBBCBDDEBDCBBDDCDEEXACDBDXXDBAEEBEBCCCXEAACEBECEEEDEBXAEXECBCBECEDCBEAEXEDDADBCEADEXCECABCCDXAXAXECCEAXEBAEEAAXBEXEXEDCBXBXDBXEBCXEXBACCXXXBXXEADDXXAABECXBEEXXBCXDEXEXDCDACEADEXXCDCBXEDBCBAEADAEBDCCDAXAABXCADCADCBCEEAXBDCXDCACCCDCDXXCBXCDCECXEBAXBXACBBBDBBXCECBAXCXBECAAEDDCCDBCEXEBDAXEXDDCAXXCXEBXXEBCADXBBBBBXDEECXBBBDEEEDACDCEEAADDEEAEDAXACAXDXCEXBACCEEEXBCAEADXBEDEBXDXEEXEBCCABDCCBBDXDACXBXCXDABXDDEADDCDDBBCAAAADAXCDBXXXXACABCADXCEXBXDXEXXEXXCCDDCCEXDXBBBCCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
This is just the front part of result, seems the answer of all students are accumulated and keep decreasing until the last one:
2019095
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019096 DECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019097 ECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019098 CXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
2019099 ACCBDEBECABEBCDECBDD
I am quite new to stackoverflow and not so sure how to edit my question. Sorry to it, but my problems are
The first correct answer cannot print out (means it didnt save to array?)
The answer of students keep accumulating
What I want is that the correct answer should be stored in correct_ans, and the student number is scanned according to the answer. Thx for ur help
CodePudding user response:
The length of the string
CCBEBADEADCBECXDECEBDECXXDDXDXBCADEEECCXECBBCDAACBCACDCCXXDDCXDAECAEEAXDCEBXBAEXACCBDEBECABEBCDECBDD
is larger than sizeof(temp_answer)
.
From the
fscanf(fp1, "%d %s\n",
&student_number[i],
temp_answer);
on, the result is undefined.
If you replace the hardcoded constant 20
with a bigger amount, it should work.
Also, there is no need to a temp_answer
here, you can read directly in student_ans[i]
.