![]() |
| | #1 |
| Its hard... But im here Join Date: Apr 2005 Location: England
Posts: 1,466
| Pick data from file store in array I am wondering say I have a text file like this: Code: Name: xxx Address: == Tel: 000 Name: x Address: = Tel 0 etc etc Code: char names[ SIZE ][ LENGTH ] 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");
}
}
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
__________________ I'm just trying to be a better person - My Name Is Earl |
| swgh is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You need to read in all the lines, because you can't get from line 1 to line 4 without going through lines 2 and 3. But: the "f" in scanf stands for format. The format you wish to match is the literal "Name: " followed by everything to the new line. So do that: Code: if (sscanf(buffer, "Name: %[^\n]", array[i]) == 1) {
//read in, so move i and do whatever else you need to do
}
|
| tabstop is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how do you input data from a file into an array? | jorgejags | C Programming | 5 | 11-03-2008 02:48 PM |
| Need Help: Storing numeric data into matrix from a data file | spiit231 | C Programming | 3 | 02-26-2008 02:12 PM |
| Can we have vector of vector? | ketu1 | C++ Programming | 24 | 01-03-2008 05:02 AM |
| [question]Analyzing data in a two-dimensional array[with code] | burbose | C Programming | 4 | 06-14-2005 05:45 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |