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!

opening a file and 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 and add one number to it and 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, below are some comments and suggestions.

>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))

I think you mean while(!feof(fp)) Otherwise the loop never
gets entered. There's another problem though with calling
feof() like this. feof() doesn't evaluate the stream
passed to it, so it doesn't detect feof() until fscanf()
does below.

You can do:

do {
/* ... */
} while (!feof(fp));

Or, the more typical way is to just test the return value of fscanf():

while (fscanf(/* ... */)==1) {
}

Then, later check for error with feof() or ferror():

if (!feof(fp)) {
/* error - file didn't reach EOF */

>{
>fscanf(fp,&quot;%d&quot;,data.fname);

This is wrong. You should be supplying a pointer to int here:

fscanf(fp,&quot;%d&quot;,&data.fname[j]);

This is pretending that j is also an int that you're keeping track
of. Frankly, I don't know what the purpose of having an
array of 5 structs where each struct contains an array of 20 ints
for this is.

>i++;
>}
>//here how to retrieve the last record no...
>//and how to add one to it and again append to the file 123.txt

You don't really need the struct since all you're interested in is the
value of the last record. Here's an outline of what you need to do
(if I understand what you want to do correctly)

Declare a variable to hold the field number:

int f_num=0;

Declare an array of char to hold your new file name:

char new_fname[FILENAME_MAX+1];

Open your stream for reading AND writing:

fp=fopen(&quot;C:\\123.txt&quot;, &quot;r+&quot;);

Loop through the file to the end, getting the last record number:

while (fscanf(fp,&quot;%d&quot;,&f_num)==1)
; /* empty loop */

/* test for errors here */

Increment the record variable to the next record number:

++f_num;

Add a new record at the end and close the stream:

fprintf(fp,&quot;%d\n&quot;,f_num);
fclose(fp);

Build the new file name:

sprintf(new_fname,&quot;C:\\%d.txt&quot;,f_num);

Create the new file:

fs=fopen(new_fname,&quot;w&quot;);

if (fs!=NULL) {
fclose(fs);
} else {
/* error */
}










Russ
bobbitts@hotmail.com
 
Hi, I have seen the same type of question here. I posted the following [ furget the thread now ] for that, I may useful for you.

#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(1)
{
fscanf(fp, &quot;%d&quot;, &num);
if(feof(fp)) break;
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;
}


Note : in the above suggestion rbobbitt opened the source file in r+ mode. r+ for modification, ie we can read and write to existing records only, we can't add any new records at the end of the file.

Regards
Maniraja S
 
Yes, you can write new data (a &quot;record&quot;) at the end of the file in &quot;r+&quot; mode. The difference between &quot;r+&quot; and &quot;a+&quot; is that in &quot;r+&quot; mode the file pointer is positioned at the beginning of the file when the file is opened, in &quot;a+&quot; mode it's positioned at the end. Another difference is that in &quot;r+&quot; mode writes may occur anywhere in the file (including being appended at the end), while in &quot;a+&quot; mode all writes will occur after end of file.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top