Note that there are some explanatory texts on larger screens.

plurals
  1. POfscanf crash while reading to struct in C
    primarykey
    data
    text
    <p>I am developing some C app for my homework and I'm facing with annoying crash. Here's my code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; //Constants //Available user choices enum commands {READ_LIST = 1, QUIT}; struct student { char* surname; char* name; char* group; }; typedef struct student Student; typedef Student * studentPtr; //Globals int studentCount = 0; //Function declarations void displayCommands(); void readList(); //Main function int main() { char enteredValue[999]; int thisCommand; int running = 1; while(running) { displayCommands(); scanf("%s", enteredValue); thisCommand = atoi(enteredValue); puts("\n----------------------------------------------"); switch(thisCommand) { case READ_LIST: readList(); break; case QUIT: running = 0; break; default: puts("Wrong command!"); break; } } system("pause"); return 0; } void displayCommands() { puts("\n---------------------------------------------"); puts("Enter a command number:"); printf("%d - Read students from file.\n", READ_LIST); printf("%d - Quit.\n", QUIT); puts("----------------------------------------------"); } void readList() { FILE *fp = NULL; fp = fopen("studs.txt", "r"); studentPtr newStudentPtr = malloc(sizeof(Student)); if(fp != NULL) { fscanf(fp, "%d", &amp;studentCount); if(newStudentPtr != NULL) { fscanf(fp, "%s %s %s", newStudentPtr-&gt;surname, newStudentPtr-&gt;name, newStudentPtr-&gt;group); } fclose(fp); } else { puts("Unable to open file for reading!"); } } </code></pre> <p>I basically try to read integer ant three strings into a structure. The program crashes when it's trying to read last line of the file.</p> <p>Any help, please? What mistakes am I making?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload