Read values from txt file and store them in structure

This is a discussion on Read values from txt file and store them in structure within the C Programming forums, part of the General Programming Boards category; Hello, If I have a text file with the format: Name = MattPhilip Age = 22 how can I use ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Read values from txt file and store them in structure

    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

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Wink

    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

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    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?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    fscanf - C++ Reference

    [%s] - String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
    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.

    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.

  6. #6
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    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);
    
    }
    i think this will help u!!!
    Last edited by krishnapollu; 12-19-2010 at 04:39 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    krishnapollu's code may get you going on the right track, but there are a few problems with it you should be aware of:

    1. conio.h, clrscr and getch are old DOS/Windows specific stuff from Turbo C, so they might not work for you.
    2. You need to check the return value of fopen before trying to read.
    3. 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.
    4. You need to check the return value of fscanf/sscanf to make sure it actually converted the value as you expected.
    5. 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.
    6. 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.
    7. You need to call fclose when you're done reading.
    8. 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, ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help
    By fourseventwo in forum C Programming
    Replies: 4
    Last Post: 12-04-2010, 10:01 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 05:53 PM
  4. Replies: 1
    Last Post: 03-27-2009, 04:21 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21