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!

how to amend a field of a record in a data file

Status
Not open for further replies.

subra

Programmer
Aug 6, 2000
45
IN
I work on the DOS platform only and I use the Turbo-c ver 3.0 compiler

I have written the following code:

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

typedef struct
{
char custno[ 7]; /* customer no */
char accnum[14]; /* account no */
char acdesc[31]; /* account description */
char clrbal[16]; /* clear balance */
char acclim[14]; /* account limit */
char rofint[ 7]; /* rate of interest */
char group1[ 3]; /* group 1 for sub limits */
char group2[ 3]; /* group 2 for sub limits */
char group3[ 3]; /* group 3 for sub limits */
char acopen[ 2]; /* a/c open status 0 closed - 1 open */
} ACLIMITS;

ACLIMITS limit;
void getsub(char *sptr, char *dptr, int stpt, int n);

void main(void)
{
FILE *in;
char buffer[91], substr[50];
int size, counter=0;
long fpos;

size = sizeof(limit);
clrscr();

in = fopen(&quot;limit.dat&quot;,&quot;r+t&quot;) ; /* open the data file */
fpos=ftell(in);
fread(&limit, size, 1, in);
if (strcmp(limit.acopen,&quot;&quot;)!=0)
{
while (!feof(in))
{
strcpy(limit.acopen,&quot;0&quot;);
fseek(in, fpos, SEEK_SET);
fwrite(&limit, size, 1, in);
fpos=ftell(in);
fread(&limit, size, 1, in);
}
}
rewind(in);
fclose(in);


The program is supposed to read each record. Amend a particular field (limit.acopen in this case) and write the record back into the same file. I am unable to get this thing going. Fread and fwrite move the record pointer, and i am therefore unable to over-write the record to the file at the same location. if i use ftell or fgetpos and fsetpos, the feof gets affected. How can i solve the problem? Please help.

Subra if you find this useful let me know at vksubra@icenet.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top