Thread: printing from file problems

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    printing from file problems

    Code:
    void ld_apps()
    {
         FILE *fp;
         
          fp = fopen("G:/Uniwork/Semester2/ProceduralProgramming/Assignment/Appointment.txt", "rt");
            if (fp == NULL)  {
                   printf("\nUnable to Open Input File\n");
                   exit(EXIT_FAILURE);
                   }
                   
            else  {
                  printf("File Open\n\n The Appointments are Shown Below\n\n");
                  }
                  
                while  ( !feof ( fp ))
                
                  {  
                Appointments++  ;     
                printf("\n\nCurrent Number of Appointments: %d", Appointments);
                
                  {
                fscanf(fp, "%s", PrAppArray[0].name);
                printf("\nName: %s", PrAppArray[0].name);
                
                fscanf(fp, "%s", PrAppArray[0].surname);
                printf("\nSurname: %s", PrAppArray[0].surname);
                
                fscanf(fp, "%f", &PrAppArray[0].starttime);
                printf("\nStart Time: %.2f", PrAppArray[0].starttime);
                
                fscanf(fp, "%f", &PrAppArray[0].endtime);
                printf("\nEnd Time: %.2f", PrAppArray[0].endtime);
                
                fscanf(fp, "%s", PrAppArray[0].place);
                printf("\nPlace: %s", PrAppArray[0].place);
                
                fscanf(fp, "%s", PrAppArray[0].whom);
                printf("\nWhom: %s", PrAppArray[0].whom);
                
                fscanf(fp, "%s", PrAppArray[0].date);
                printf("\nPlace: %s", PrAppArray[0].date);
                
                fscanf(fp, "%s", &PrAppArray[0].priority);
                printf("\nPriority: %s", PrAppArray[0].priority); 
             
         }  
         }       
          printf("\n");
          subm_app();
          return;
       
    } // finish function
    Basically this is the code ive used to pull the information from the file so that it writes all the records to the screen, that are in the text file.

    but when it does, it prints every record twice, very frustrating.

    could anyone please help?

    i also have the code also which writes the information into the file, if its needed please say.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You should never use feof to control a loop. Read why here. That should only cause the last record to get printed twice, not every record, but it's still a problem. Also, you have an extra set of braces between the printf in the loop and the end of the loop. It serves no purpose.

    Does it print the two copies "collated"? That is, does it print A, B, C, A, B, C or A, A, B, B, C, C? Some sample output might be helpful. The writing code might be helpful too. Are you sure there's only one copy of each record written to the file? I see nothing in here that screams "print two copies".

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    reply

    My text file had a bit of a problem, ive now deleted it and remade the text file.

    except i still face a problem but not the same one.

    it prints A B C C. so the last record is printed twice now, the rest print in order.

    even when i add a record C would become a single record and D would become double.

    i.e. A B C C, add 1 record now becomes A B C D D

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Clearly you didn't read my first paragraph thoroughly or the link I provided (click the red text).

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    i kind of dont get it, so im ment to change the feof part to somethin like "while (fgets(buf, sizeof(buf), fp) != NULL)"

    but to be 100% honest im no good with programing and wouldnt be albe to put that into my own progam

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Conor1992 View Post
    but to be 100% honest im no good with programing and wouldnt be albe to put that into my own progam
    And you never will so long as you keep telling yourself that.

    Half of success is attitude. Enter a new venture with a defeatist attitude and even with the best products, tools and people, it will fail.
    On the other hand, go into it with an attitude that says "I can do this!" and just watch what happens.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To follow up a little with Tater, yes, your attitude is crappy. There are some people who truly don't get it, but you do get it. Hell, you wrote the right answer in your post. You need to replace the while (!feof) that I said was wrong with that while (fgets) bit. Only you need to define buf, and change your fscanf to be sscanf. Read the docs on sscanf for usage details. You will have to change very little.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  2. problems printing out
    By pastitprogram in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2008, 06:06 PM
  3. Printing problems
    By Ness757 in forum C Programming
    Replies: 5
    Last Post: 03-06-2006, 07:47 PM
  4. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM
  5. Problems printing a linked list.
    By Ed_Severson in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 10:31 PM