fscanf printf while loop help

This is a discussion on fscanf printf while loop help within the C Programming forums, part of the General Programming Boards category; I made this program and got it to compile without any errors but it doesn't run as expected. If I ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    fscanf printf while loop help

    I made this program and got it to compile without any errors but it doesn't run as expected. If I enter 2 I will get to see all of the student information for each individual. Once I get back to the menu selection and press 2 again I don't see any student information. How can I make it so that every time I press 2 at the menu I get to see all student information?
    Code:
     #include <stdio.h>
    #include <string.h>
    
    int main()
    
    {
    	FILE *pAppend;
    	FILE *pRead;
    	char fName[21];
    	char lName[21];
    	char id[15];
    	float gpa=0;
    	int iSelection=0;
    
    	pAppend = fopen("StudentInformation.dat", "a");
    	pRead = fopen("StudentInformation.dat", "r");
    
    	
    	
    	while(iSelection!=3) {
    	printf("\nStudent Information Exercise\n\n");
    	printf("Select an option\n");
    	printf("1)\tEnter student information\n");
    	printf("2)\tView current student information on file\n");
    	printf("3)\tExit student information exercise\n\n");
    
    	scanf("%d", &iSelection);
    	
    	if (pAppend==NULL)
    		printf("File not opened\n");
    	
    	if(iSelection==1){
    	if (pAppend==NULL){
    		printf("File not opened\n");
    		return 0;	
    			}
    
    		printf("Enter first name, last name id and GPA\n\n");
    		printf("Enter data seperated by spaces:  ");
    		scanf("%s%s%s%f", fName, lName, id, &gpa);
    		fprintf(pAppend, "%s\t%s\t%s\t%.2f\n", fName, lName, id, gpa);
    		fclose(pAppend);
    		}
    	if(iSelection==2) {
    		if (pRead==NULL){
    		printf("File not opened\n");
    		return 0;	
    			}
    
    		printf("Current student information\n\n");
    		printf("First Name\tLast Name\tID\tGPA\n");
    		fscanf(pRead, "%s%s%s%f", fName, lName, id, &gpa);
    		while(!feof(pRead) ) {
    		printf("%s\t\t%s\t\t%s\t%.2f\n", fName, lName, id, gpa);
    		fscanf(pRead, "%s%s%s%f", fName, lName, id, &gpa);
    				}
    		fclose(pRead);
    	}
    
    
    }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Opening the same file for reading and updating won't work.

    You've already opened it for update, that is all you need.

    However, having said that, performing updates on a text file is exceedingly difficult with variable length records.
    It is generally a LOT simpler to read from one file, and write to another.

    Also, your indentation still needs work.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks.

    Link isn't working, I'll check it again later and look at my books indentation till then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 08:40 PM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 06:57 PM
  3. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  4. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  5. Help with a simple loop...
    By Optimus Prime in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21