Thread: fscanf printf while loop help

  1. #1
    Registered User
    Join Date
    May 2009

    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?
     #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 (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);
    	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);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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. 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.

  3. #3
    Registered User
    Join Date
    May 2009

    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, 07:40 PM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 05:57 PM
  3. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  4. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  5. Help with a simple loop...
    By Optimus Prime in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 08:28 AM
Website Security Test