Reading from Files in C

Reading all records in a random access file without printing the last

Page 1 of 1

9 Replies - 1483 Views - Last Post: 24 April 2009 - 08:54 AM Rate Topic: -----

#1 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Reading from Files in C

Posted 16 April 2009 - 07:09 AM

I neeed some help with this problem. When i try to read all the records in a random access file in c it prints the last record twice in all the cases i've tried

#include <stdio.h>
#include <conio.h>


typedef struct
{
	char fname[12], lname[12];
}name;

FILE*info;

void main ()
{
	int i;
	name customer;
   if((info=fopen("Customers.dat", "wb"))==NULL)  //OPen file for writing
   	fprintf(stderr, "Error! Unable to store info");
   else{
   	for(i=0;i<2; i++){//Accept 2 records
   		printf("Please enter name: ");
   		scanf("%s %s", customer.fname, customer.lname);
   		fwrite(&customer, sizeof(name), 1, info);
	  }//end for loop
   } //end if
   fclose(info);							 //close file after writing   

   if((info=fopen("Customers.dat", "rb"))==NULL)  //Open file for reading
   	fprintf(stderr, "Error! Unable to open file");
   else{
   	while(!feof(info)){		//Print info read from file
   		fread(&customer, sizeof(name), 1, info);
		  printf("\nThe name enter is %s %s", customer.fname, customer.lname);
	  }//end while .
   }//end if
   fclose(info);
   getch();
} 



I dont know if i'm not using "feof" correctly

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from Files in C

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading from Files in C

Posted 16 April 2009 - 07:41 AM

I haven't analysed this carefully but often these sorts of things can be resolved by flushing the buffers after each output using fflush.

Worth a try.
Was This Post Helpful? 0
  • +
  • -

#3 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Reading from Files in C

Posted 16 April 2009 - 08:08 AM

View Postjanotte, on 16 Apr, 2009 - 06:41 AM, said:

I haven't analysed this carefully but often these sorts of things can be resolved by flushing the buffers after each output using fflush.

Worth a try.


I did that already but it didnt work
Was This Post Helpful? 0
  • +
  • -

#4 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: Reading from Files in C

Posted 17 April 2009 - 12:39 AM

Are you sure the last record does not get written twice? Could you check this first for example by writing the records in plain text or calculating what the size of file with two structures should be. How about reading, does changing while() {} loop to do {} while() make any difference?
Was This Post Helpful? 0
  • +
  • -

#5 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Reading from Files in C

Posted 17 April 2009 - 01:46 PM

View Postmclang, on 16 Apr, 2009 - 11:39 PM, said:

Are you sure the last record does not get written twice? Could you check this first for example by writing the records in plain text or calculating what the size of file with two structures should be. How about reading, does changing while() {} loop to do {} while() make any difference?



No difference with while{} and do{}while
Was This Post Helpful? 0
  • +
  • -

#6 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Reading from Files in C

Posted 23 April 2009 - 09:00 AM

I'm in need of great help here, I'll appreciate any comments.
I tried fflush, that didnt work, do{}while() instead of while and that didnt work either. Im sure the last record isn't written twice to the file because if i open the file in append mode and add a new record, it will print only the last one twice and not the one it printed twice before. This is very critical cuz i have a code to write that calculates total and this will throw me off by adding the last record twice.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Reading from Files in C

Posted 23 April 2009 - 01:35 PM

FAQ #12.2: Why does the simple line-copying loop while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } copy the last line twice?
Was This Post Helpful? 1
  • +
  • -

#8 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Reading from Files in C

Posted 24 April 2009 - 06:32 AM

View PostJackOfAllTrades, on 23 Apr, 2009 - 12:35 PM, said:

FAQ #12.2: Why does the simple line-copying loop while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } copy the last line twice?



I'm not trying to read lines but rather reading complete records (random files). fgets() read lines at a time and this kind of file is not arranged in lines.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading from Files in C

Posted 24 April 2009 - 06:36 AM

View Poststefaan, on 24 Apr, 2009 - 05:32 AM, said:

I'm not trying to read lines but rather reading complete records (random files). fgets() read lines at a time and this kind of file is not arranged in lines.


Did you follow the link in JackOfAllTrades's posting?
Was This Post Helpful? 1
  • +
  • -

#10 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Reading from Files in C

Posted 24 April 2009 - 08:54 AM

View Postjanotte, on 24 Apr, 2009 - 05:36 AM, said:

View Poststefaan, on 24 Apr, 2009 - 05:32 AM, said:

I'm not trying to read lines but rather reading complete records (random files). fgets() read lines at a time and this kind of file is not arranged in lines.


Did you follow the link in JackOfAllTrades's posting?



Thank you very, it works just fine.
I was anxiously reading the post i miss the link. Thanx again
Bravo!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1