My program is supposed to ask a user to enter a text file that isin this format:
ID: 3619184 • Letter: M
Question
My program is supposed to ask a user to enter a text file that isin this format:MATH
101
GEOLOGY
101
GYM
101
I'm supposed to read each of the subject name and store it in anarray and store the subject numbers in another array.
I'm not sure how to do it, I know I have to use fscanf but i'mreally stuck!
Here is what I have so far:
int main()
{
FILE* pFile = NULL;
char data[100];
char filename[80];
int *array;
int number;
// asking the user to enter a filename, the file will readthe conents
printf("Please enter a filename for input:");
scanf("%s", &filename);
pFile = fopen(filename, "r");
if (pFile == NULL)
{
printf ("Error openingfile! ");
return -1;
}
while (!feof(pFile))
{
fscanf(pFile, "%s ", data);
createCourse(data,number);
}
fclose(pFile);
return 0;
}
Explanation / Answer
please rate - thanks I prefer this over feof the name is 2 dimensional since it is many letters many times #include #include int main() { FILE* pFile; char name[100][20]; int number[100]; char filename[80]; int i=0; // asking the user to enter a filename, the file will readthe conents printf("Please enter a filename for input:"); scanf("%s", &filename); pFile = fopen(filename, "r"); if (pFile == NULL) { printf ("Error openingfile! "); return -1; } while(fscanf(pFile, "%s",&name[i])>0) { fscanf(pFile,"%d",&number[i]); printf("%s%d ",name[i],number[i]); i++; } fclose(pFile); getch(); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.