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

reading in a file 1

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
hello

I have a file with the following data in which are tab delimted fields:

172.24.55.6 90 0 0
172.25.56.7 80 1 0
172.26.55.6 71 0 3

etc..

I want my C program to be able to read them in and store the ip address in a char* ipAddress and the code after wards (e.g 90 or 80 or 70) in a char* code. What is the best way to do this?

thanks for your time
 
Something like this
Code:
#define MAX 100

// you can change these to char*, but you'd then
// have to allocate memory yourself
struct {
  char ip[16];
  char code[10];
} info[MAX];

char buff[BUFSIZ];
FILE *fp = fopen( "file", "r" );
int num = 0;

// loop until the array is full or the end of the file
while ( num < MAX &&
        fgets( buff, BUFSIZ, fp ) != NULL ) {
  if ( sscanf( buff, "%s %s", info[num].ip, info[num].code ) == 2 ) {
    // valid data
    num++;
  }
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top