Thread: file problem

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    12

    file problem

    Hi all.
    this is part of my code

    Code:
    FILE *fp;
    			fp = fopen("test.txt", "w");
    
    			system ("cls"); 
    			printf("How many employee info do you wish to enter? ");
    			scanf("%d", &empNum);
    
    
    			for(counter = 0; counter < stuNum; counter++)
    			{
    			 fprintf(stdout, "\nEnter Employee name: ");
    			 fscanf(stdin,"%s", s1[counter].name);
                               
    			 fprintf(stdout, "Enter Salary: ");
    			 fscanf(stdin,"%d", &s1[counter].salary);
                
    			 fprintf(fp, "%s%d", s1[counter].name, 
                                         s1[counter].salary);
    
    fclose(fp);
    }
    when i checked the *.txt file it only hav one input (even if i entered 10 input). can u help me with this ??

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Don't close the file until after all iterations of the loop, not on each.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    12
    Thank you for u'r help

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    EmpNum isn't used for anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM