Hey guys.
I am wondering say I have a text file like this:
Code:
Name: xxx
Address: ==
Tel: 000
Name: x
Address: =
Tel 0
etc etc
And in main() I declare an array to store some data:
Code:
char names[ SIZE ][ LENGTH ]
Is it possible to use either fscanf or fgets to store only the names in
this array?
So basically my unsuccessful attempt was:
Code:
#include "telebase.h"
#include <stdio.h>
#include <stdlib.h>
/*function to initilize data members to a default state*/
void initilizeDataMembers ( struct TeleBase *tb ) {
FILE *inFile;
inFile = fopen( "telephonelog.txt", "r" );
if ( inFile != NULL ) {
while ( fgets( tb->name, sizeof( tb->name ), inFile ) != NULL ) {
printf("%s\n", tb->name );
}
fclose( inFile );
}
else {
printf("Cannot open file!\n");
}
}
Which stores the entire file into the array which I do not want, I just want
the first line of each one, ( ie the name of each telephone contact ) in
this one array.
So far I have only learnt fprintf and fscanf basics when dealing with files, is
there another C file function that can do this, or am I on the right tracks?
btw tb is a data struct.
Any help greatly appriciated