Hello,
If I have a text file with the format:
Name = MattPhilip
Age = 22
how can I use C to read from the file and fill in a structure?
Thanks
Hello,
If I have a text file with the format:
Name = MattPhilip
Age = 22
how can I use C to read from the file and fill in a structure?
Thanks
Cprogramming.com - Tutorials - C File I/O
Read through there and come back with what exactly you don't understand about File I/O.
There are also tutorials on structures, but I'm not sure whether you are wanting to know how to put the data into a structure or just how to read the data.
Hint:
Create a struct for saving data. Use File, fopen, fclose for file reading.
Use fscanf to get name and age
Mac OS 10.6 Snow Leopard : Darwin
yes, I am having troubles understanding fscanf
how can I use fscanf to read name, age, school ...etc
I understand if I use:
fscanf(fp, "%s", str);
it will read the whole line and store it in string.. am I right?
fscanf - C++ Reference
This says that fscanf when used with the %s type specifier only reads until the first whitespace character. In this case, it will only read "Name" and store that in 'str'. If you know the exact format of the string (which, in this case, you do) you can include the expected whitespace characters in the format specifier string to read more.[%s] - String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
For example, if I had a file which contained "Hello John" I could read the individual words using 'fscanf( fp, "%s %s", hstr, jstr );'. Note the space in between the two type specifiers.
i think this will help u!!!Code:#include<stdio.h> #include<conio.h> int main() { char *temp="name",*temp1="=",*temp2="age",*string; int i=0; struct str_name { char *name; int age; }object[1]; FILE *fp; clrscr(); fp=fopen(file_name,"r"); while(getc(fp)!=EOF) { fscanf(fp,"%s",string); if(strcmp(string,temp)==0) { fscanf(fp,"%s",string); if(strcmp(string,temp1)==0) fscanf(fp,"%s",object[i].name); } if(strcmp(string,temp2)==0) { fscanf(fp,"%s",string); object[i].number=0; if(strcmp(string,temp1)==0) { fscanf(fp,"%d",&object[i].age); i++;} } } printf("Name: %s\nNumber: %d",object[0].name,object[0].age); //now the structure will contain the details of the file in corresponding order getch(); return(0); }
Last edited by krishnapollu; 12-19-2010 at 05:39 AM.
krishnapollu's code may get you going on the right track, but there are a few problems with it you should be aware of:
- conio.h, clrscr and getch are old DOS/Windows specific stuff from Turbo C, so they might not work for you.
- You need to check the return value of fopen before trying to read.
- Cprogramming.com FAQ > Why it's bad to use feof() to control a loop. You will probably be better off using fgets and then sscanf to parse each line.
- You need to check the return value of fscanf/sscanf to make sure it actually converted the value as you expected.
- Instead of fscanf calls with nested if statements, use a fixed portion of the string, like so: sscanf(buf, "Name = %s", obj.name), which will match Name, = and MattPhilip all at once.
- Your structure needs a char array with a sufficient size, not just a char *. The char * is a pointer, and doesn't reserve any space to actually store string contents. Try name[64] or something.
- You need to call fclose when you're done reading.
- Make your array of objects big enough to hold all the entries in the file so you don't buffer overflow. There is currently an array of size one, but the code will write into indexes 0, 1, 2, 3, ...