0

File Management

I have data stored in a text file as followed Course Name(String) Course ID(String) Number of Students(Int) Day,Time,Location(This is accepted as one string using a multi-dimensional array) The user should be able to Input the course ID and everything associated with the course ID should be printed Example of data in the text file: Computer Science CS1234 4 Monday 8:30-9:30 Auditorium English A EA1234 5 Monday 10:30-11:30 Room 1 Tuesday 12:30-1:30 Room 2 Using fgets i can print a line from the file how it is displayed in the file (Everything on one line), which is what I did, but I would like the data to printed in a specific format instead of all on one line, How I want it to print: Course Name:(Course Name) Course ID: (Course ID) Number of Students: (Number of students) Weekly classes: (Day,Time,Location) Any help is appreciated. Thank you in advance. (If any code is needed please let me know and I will post the code)

22nd Mar 2020, 7:09 PM
Jergan Hector
Jergan Hector - avatar
4 odpowiedzi
0
Using fscanf, read words from text files and compare as needed by if else then print. This is one way.. https://www.sololearn.com/learn/C/2952/
22nd Mar 2020, 7:35 PM
Jayakrishna 🇮🇳
0
https://code.sololearn.com/cBtatqUkr7UJ This is what i have as an attempt of reading everything into variables but when I print it to the screen I get process returns 0
22nd Mar 2020, 8:05 PM
Jergan Hector
Jergan Hector - avatar
0
Make sure your file opened correctly.. Sorry this can't be able to check here.. conio.h is deprecated header don't use that.. In Linux, sys don't works.. Iam not sure. May be issue is in this, fscanf(schedule,"%s", &queue_array[20][MAX]); No need of & there. This works well.. int main() { //use int main FILE *fp; fp = fopen("test","a"); fprintf(fp,"%s %s %d\n","1","1a1",2); fclose(fp); int n; fp = fopen("test","r"); char rollno[10],name[10]; fscanf(fp,"%s %s %d\n",rollno,name,&n); printf(" %s %s %d\n",rollno,name,n); fclose(fp); } After reading into variable, you can check and print as you needed...
23rd Mar 2020, 10:42 AM
Jayakrishna 🇮🇳