Here is the psuedocode my teacher gave us.
void list(FILE *fp) {
/* move the file pointer to the beginning using fseek() */
get a line using fgets()
print the line to the console using puts()
until end of file is reached
}
I am having trouble getting my code to work. Here is what i have in my main and what i have in my function, respectively. Keep in mind I am only on case 2 so far so ignore the other cases... I keep getting syntax errors, what am i doing wrong?
Code:
#include <stdio.h>
#include "list.h"
int main() {
int user_input;
printf("1. insert\n2. list\n3. delete\n4. update\n5. exit\nKey in 1-5:\n");
scanf("%d", &user_input);
while (user_input > 0) {
switch(user_input) {
case 1:
//insert();
break;
case 2:
list(FILE *fp);
break;
case 3:
//delete();
break;
case 4:
//update();
break;
case 5:
/* close file and exit */
fclose(fp);
break;
default:
break;
/* error inputs handling */
}
printf("1. insert\n2. list\n3. delete\n4. update\n5. exit\nKey in 1-5, -1 to exit:\n");
scanf("%d", &user_input);
}
} /* end of main() */
and now the function list...
Code:
#include <stdio.h>
void list(FILE *fp);
int main()
{
FILE * fp;
char mystring [100];
fp = fopen("file.txt" , "w+");
if (fp == NULL) perror ("Opening File");
else {
fgets (mystring , 100 , fp);
puts (mystring);
fclose (fp);
}
return 0;
}