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("C:\\123.txt", "r"
int i=0,reccount;
char found;
if(fp==NULL)
{
printf("No such file"
exit(1);
}
while(feof(fp))
{
fscanf(fp,"%d",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
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("C:\\123.txt", "r"
int i=0,reccount;
char found;
if(fp==NULL)
{
printf("No such file"
exit(1);
}
while(feof(fp))
{
fscanf(fp,"%d",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