C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Yeah, I went ahead and changed the switch case to integers instead of characters and it worked great. I am almost done with the program. I have tested the creating student function, and I am working out some minor problems there. I am now testing the load and sorting functions of the program, but I keep running into the problem of not reading in the number of students correctly.
Example--
Code:void SortStudent(StudentLink *root) { StudentLink *temp, *prev, *next, *changeme; int x, y; FILE *fp; int counter; char filename[80]; int studentnumber; char line[80]; printf("Enter the file name: "); //Enter in the file name scanf("%s", filename); if((fp = fopen(filename, "w+")) != NULL) //Opening the file { printf("You have successfully opened the file!\n"); { fscanf(fp, "%d", &studentnumber); //Number of students in the list printf("The number of students on the file is %d\n\n\n"); }
The output comes out something like this, but no error messages.
The file has been successfully opened!
The number of students is 426785254.
When in reality the text file actually only has 7 as the number of students.
I have tried the following: freeing any information before scanning in the number, using some of the examples in STDIN pitfalls, etc. I know that it is not reading in the number correctly, but i am not sure why. I am new at this, and I am sorry to ask so many questions.
Something missing here...Code:{ fscanf(fp, "%d", &studentnumber); //Number of students in the list printf("The number of students on the file is %d\n\n\n"); }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I see that I forgot to tell it what to print...
It is still freezing up and not registering the number. It says the number of students is 0 before it freezes and stops working.Code:fprintf("The number of students is %d", studentnumber);
You should start compiling with warnings on. You are missing an argument to your fprintf call. Even if this is an example, you seem to be making this same mistake over and over in your use of examples (or your actual code). In any event, compile with warnings on, and pay attention to what you're doing with each function you call.
Quzah.
Hope is the first step on the road to disappointment.
I have been compiling with warnings on, but when I typed the code in last night onto here, I forgot to include the fp.
Here is the code I have that won't work, copied straight from the code.
Code:void SortStudent(StudentLink *root) { StudentLink *temp, *prev, *next, *changeme; int x, y; FILE *fp; int counter; char filename[80]; int studentnumber; char line[80]; printf("Enter the file name: "); //Enter in the file name scanf("%s", filename); if((fp = fopen(filename, "w+")) != NULL) //Opening the file { printf("You have successfully opened the file!\n"); fscanf(fp, "%d", &studentnumber); //Number of students in the list printf("The number of students on the file is %d\n\n\n", studentnumber); printf("First Name\tMiddle Name\tLast Name\t"); printf("Street\tCity\tState\tZipcode\tHours\tFees Paid\n\n\n"); temp = root;
I have worked a lot on my code and have fixed the problem of scanning in the number of students, but I am still having difficulty with scanning in the information of the file.
Code:void LoadStudent(int studentnumber, StudentLink *root) //Loads a file of student records { StudentLink *temp, *prev, *next; FILE *fp; char filename[80]; int counter; char line[80]; int stream; printf("Enter the file name: "); //Filename to be opened scanf("%s", filename); fp = fopen(filename, "r+"); if(fp != NULL) //If the file does open { printf("You have successfully opened the file!\n"); fscanf(fp, "%d", &studentnumber); printf("The number of students on the file is %d\n\n\n", studentnumber); //First number in the file should be the number of students printf("First Name Middle Name Last Name\t"); //Headers for style printf("Street City State Zipcode\tHours\tFees Paid\n\n\n"); root = NULL; //Sets the root to null prev = NULL; //Sets the prev to null for(counter = 0; counter < studentnumber; counter++) //counting up through the students { fscanf(fp, "%s", temp->first_name); fscanf(fp, "%s", temp->middle_name); fscanf(fp, "%s", temp->last_name); fscanf(fp, "%s", temp->street); fscanf(fp, "%s", temp->city); fscanf(fp, "%s", temp->state); fscanf(fp, "%d", &(temp->zipcode)); fscanf(fp, "%d", &(temp->hours)); fscanf(fp, "%d", &(temp->feespaid)); if (root == NULL) { root = temp; //Assigns root as temp } if (prev != NULL) { prev->next = temp; //Assigns prev as next } temp = temp->next; //Move on to the next student } fclose(fp); //Close the file }