Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

READING LAST RECORD...

Status
Not open for further replies.

stevejohn

Programmer
May 26, 2001
2
US
Hi,

I want to write a program in c which will open a file and reads the contents adn goto the last record and get the details of the last record.

Bbasically the file contains numbers. 0,1,2,.....

Add one number to the last no and open a file on that name and again append the new number to the first file. This process should go on whenever a the program starts.

For eg. in a file 123.txt there is no record.

First time the file should open and say there is no records so it is null. Add one to the null and it will become 1 open a file with 1.txt and add this 1 to 123.txt.

The second time when 123.txt is accessed there is a record so get that number and add one to it. It will vecome 2. open a file with 2.txt and add 2 to the last record.

So there are 2 records in 123.txt.. when third time it is accessed it will become 3... and soon...

I have written a code for this.. but somehow I am unable to get to the logic...

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

struct dtls{
int fname[20];
}data[5];

FILE *fp, *fs

char cont = 'y';
int fno, no, reccount;

int main()
{
fp=fopen(&quot;C:\\123.txt&quot;, &quot;r&quot;);
int i=0,reccount;
char found;
if(fp==NULL)
{
printf(&quot;No such file&quot;);
exit(1);
}
while(feof(fp))
{
fscanf(fp,&quot;%d&quot;,data.fname);
i++;
}
//here how to retrieve the last record no...
//and how to add one to it and again append to the file 123.txt

Steve
 
Hi you can do it by opening the file in a+ [ append and read] mode.

The following code may help you.#include <stdio.h>

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp, *fp_new;
int num = 0;
char fname [20];
fp = fopen(&quot;123.txt&quot;, &quot;a+&quot;);
fseek(fp, 0l, SEEK_SET);
while(!feof(fp))
{
fscanf(fp, &quot;%d&quot;, &num);
printf(&quot;\n%d&quot;, num);
}
printf(&quot;Last record = %d&quot;,++num);
sprintf(fname, &quot;%d.txt&quot;, num);
printf(&quot;%s&quot;, fname);
fprintf(fp, &quot;%d &quot;, num);
fp_new = fopen(fname, &quot;w&quot;);
fclose(fp);
return 0;
}


Regards
Maniraja S
 
Hi, just a few comments on your code smaniraja.

>#include <stdio.h>
>#include <stdlib.h>
>int main()
>{
> FILE *fp, *fp_new;
> int num = 0;
> char fname [20];

A good choice for the size of fname is FILENAME_MAX, which is defined in <stdio.h>. Though, 20 is not wrong, this is a good habit to get into.

> fp = fopen(&quot;123.txt&quot;, &quot;a+&quot;);

Some error checking should be inserted here to make sure the stream was opened.

> fseek(fp, 0l, SEEK_SET);

This is unnecessary, when you open the stream you are already at the beginning.

> while(!feof(fp))

Since feof() doesn't evaluate the stream, it doesn't know that the stream is at EOF until the fscanf() call occurs below, causing fscanf() to read the value of EOF into num.

> {
> fscanf(fp, &quot;%d&quot;, &num);
> printf(&quot;\n%d&quot;, num);
> }
> printf(&quot;Last record = %d&quot;,++num);
> sprintf(fname, &quot;%d.txt&quot;, num);
> printf(&quot;%s&quot;, fname);
> fprintf(fp, &quot;%d &quot;, num);
> fp_new = fopen(fname, &quot;w&quot;);
> fclose(fp);
> return 0;
>}


Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top