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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem reading from a text file

Status
Not open for further replies.

adev111

Programmer
Jul 8, 2004
44
0
0
CA
Hi i have a text file that I am unable to read properly.

Here's what I do.
Code:
/* tblD.txt */
NET06   1       --
NET25   1       --

/* se.h */
typedef struct {           
     char netID[7];
        int hopCount;                     
        char nextHop[2];                     
        }Routers;

/* InitD.c */
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "se.h"

int main()
{
char buff[10];
Routers routerD;
int rdr;
int loop = 0;
FILE *fp;
fp = fopen("tblD.txt","r");
rdr = fgetc(fp);

/*read network ID*/
while((rdr == '\n') && (rdr != EOF))
rdr = fgetc(fp);
if(rdr == EOF) return 0;

while((rdr != '\n')&&(rdr!='\t') && (rdr != EOF)){
buff[loop++] = rdr;
rdr = fgetc(fp);
}
buff[loop]=0;
strcpy(routerD.netID,buff);
printf("%s",routerD.netID);
if(rdr==EOF) return 0;
loop=0;
/* read hop count 
 */  while( (rdr != '\n') && (rdr !='\t') && (rdr != EOF)) {
      buff[loop++] = rdr;
      rdr = fgetc(fp);
        if(rdr==EOF)return 0;
   }
 buff[loop]=0;
   routerD.hopCount =  atoi( buff );
printf("%d",routerD.hopCount);
printf("done");

while((rdr != '\n') && (rdr != '\t')&&(rdr != EOF)){
buff[loop++]=rdr;
rdr=fgetc(fp);
}
 buff[loop]=0;
   routerD.hopCount =  atoi( buff );
   if( rdr == EOF ) return 0;
printf("%d",routerD.hopCount);

return 1;
/*
routerD.netID = ReadFromFile
routerD.hopCount = ReadFromFile
routerD.nexthop = ReadFromFile
*/
}

It doesn't print me the file as is. Could somebody tell me what am I doing wrong?

Thanks
 
Acutally I tried it n it works. However it only reads the first line and exits?

Can somebody help me print everything in the file?

Thanks

Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "se.h"

int main()
{
char buff[10];
Routers routerD;
int rdr;
int loop = 0;
FILE *fp;
fp = fopen("tblD.txt","r");
rdr = fgetc(fp);

/*read network ID*/
while((rdr == '\n') && (rdr != EOF))

rdr = fgetc(fp);
if(rdr == EOF) return 0;

while((rdr != '\n')&&(rdr!='\t') && (rdr != EOF)){
buff[loop++] = rdr;
rdr = fgetc(fp);
}
buff[loop]=0;
strcpy(routerD.netID,buff);
printf("%s",routerD.netID);
if(rdr==EOF) return 0;

loop=0;
/* read hop count 
 */  while( (rdr != '\n') &&(rdr =='\t') && (rdr != EOF)) {
      buff[loop++] = rdr;
      rdr = fgetc(fp);
        if(rdr==EOF)return 0;
   }
 buff[loop]=0;

while((rdr != '\n') && (rdr != '\t')&&(rdr != EOF)){
buff[loop++]=rdr;
rdr=fgetc(fp);
}
 buff[loop]=0;
   routerD.hopCount =  atoi( buff );
   if( rdr == EOF ) return 0;
printf("\t");
printf("%d",routerD.hopCount);

loop=0;
/* read hop count
 */  while( (rdr != '\n') &&(rdr =='\t') && (rdr != EOF)) {
      buff[loop++] = rdr;
      rdr = fgetc(fp);
        if(rdr==EOF)return 0;
   }
 buff[loop]=0;
while((rdr != '\n') && (rdr != '\t')&&(rdr != EOF)){
buff[loop++]=rdr;
rdr=fgetc(fp);
}
 buff[loop]=0;
strcpy(routerD.nextHop,buff);
printf("\t");
printf("%s",routerD.nextHop);
if( rdr == EOF ) return 0;

return 1;
/*
routerD.netID

routerD.netID = ReadFromFile
routerD.hopCount = ReadFromFile
routerD.nexthop = ReadFromFile
*/
/*
PrintRouterD
routerD.hopCount = routerD.hopCount++;
RouterD.data write to the textfile

sendmessagetoRouterB
sendmessageTo RouterC
*/
}
 
Actually I want to do more after reading. I want to be able to modify a particular record.

My text file contents are:
NET09 1 --

So after reading in the file if I find NET09 I want to replace 1 by a 2.

In my code i have:
Code:
routerD.hopCount=routerD.hopCount++;
while (fread(&routerD.hopCount,sizeof(routerD.hopCount),1,fp)==1)
{if(strcmp(routerD.netID,"NET09")==0)
{ 
   fseek(fp,-sizeof(routerD.netID),SEEK_CUR);
   fwrite(&routerD.hopCount,sizeof(routerD.hopCount),1,fp);
 
}}
/*So the modified text file contents will be:
NET09     2     --
*/

Would someone please advise.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top