Thread: Separate info in a txt file and put it into variables

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    Smile Separate info in a txt file and put it into variables

    I need help with reading text in a file and then putting it into variables.
    Like say this is a line of text in the file
    #Mike#12/05/1971#555678#Coment about this person#

    Now I have created a struct:
    Code:
    typedef struct {
            char name[MAX_PESSOA];
            char phone[MAX_TELE];
            int day;
            int month;
            int year;
            int num[QTD]; // if the person has more than one phone number
            char text[MAX_TXT];
            } Person;
    
    Person Contacts[NUM_PEOPLE];
    
    FILE *eoftest;
        int i, ending=0;
        Person lines[512];
          eoftest=fopen("contactos.txt","r");      
          while((fgets(&lines,512,eoftest))!=NULL) 
     
          { 
     
          ending++;        
     
          }
          fclose(eoftest);
     
          eoftest=fopen("contactos.txt","r"); 
                 
                 for(i=0;i<ending;i++)
                 {
                                   
                   fgets(&lines[i],512,eoftest);
                  
                   Contacts[i]=lines[i];
                               
    
    
      
      
    }
    fclose(eoftest);
    Ok so this code works in the following manner it loads a whole line into each position of my array Contacts, but there is a problem I want to load each pseudo field "name" "day/month/year" etc.... from each line of the file into the variables of my struct
    example line:
    Code:
    //example code I know I will need a loop to go through all the lines in the txt file.
    Contacts[1].name='mike'; Contacts[1].day=23; Contacts[1].month=5
    ...etc
    #Mike#12/05/1971#555678#Coment about this person#

    Can someone please help me with this....

    Thanks in advance :-)


    P.S:Please don't look at indentation and stuff like that cause I am just trying right now to get this right then I will fix everything. Bye the way this is a project and I have to kinda follow this kind of idea of the line being Separated by the # but if any one has a different suggestion that will still load the "fields" that is fine :-)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Person Contacts[NUM_PEOPLE];
    Code:
        Person lines[512];
    So what's the point of having two of the same type arrays? Read into Contacts if you're just going to assign them later.

    Code:
          eoftest=fopen("contactos.txt","r");
    You don't check if the open fails. Do that; otherwise you'll probably get yourself a crash if it fails to open.

    Code:
          while((fgets(&lines,512,eoftest))!=NULL)
    And what, exactly, do you think this does?
    512 is the number of objects you have in your array, but fgets read a LINE.
    Further, you are reading a LINE in text and not binary, so this is not what you should do. Not at all.
    &lines is also incorrect. It should be &lines[0] or lines.

    Code:
          eoftest=fopen("contactos.txt","r"); 
                 
                 for(i=0;i<ending;i++)
                 {
                                   
                   fgets(&lines[i],512,eoftest);
                  
                   Contacts[i]=lines[i];
    Repetition of your old loop except you assign to Contacts now.

    Ok so this code works in the following manner it loads a whole line into each position of my array Contacts, but there is a problem I want to load each pseudo field "name" "day/month/year" etc.... from each line of the file into the variables of my struct
    example line:
    Code:
    //example code I know I will need a loop to go through all the lines in the txt file.
    Contacts[1].name='mike'; Contacts[1].day=23; Contacts[1].month=5
    ...etc
    #Mike#12/05/1971#555678#Coment about this person#

    Can someone please help me with this....

    Thanks in advance :-)
    You really need to read up on your skills some. You shouldn't read directly into a a struct at all unless you use binary, which you don't.
    Due to the nature of your file, you might use fscanf or some similar function (I'm not an expert at that) to read until you hit a '#' or something like that.

    P.S:Please don't look at indentation and stuff like that cause I am just trying right now to get this right then I will fix everything. Bye the way this is a project and I have to kinda follow this kind of idea of the line being Separated by the # but if any one has a different suggestion that will still load the "fields" that is fine :-)
    Indentation is more important than your program. Fix it FIRST, THEN fix the code. Indentation is supposed to HELP you find bugs, NOT create them.
    Indent as you type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You need to convert text into ints. Try atoi() for a single number, or sscanf() for more complex parsing of multiple variables...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    strtol is better than atoi
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Why do you recomend converting text into ints?
    What are the advantages?
    Can you explain please?

    Thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because you're reading a string with fgets and int != string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Elysia View Post
    strtol is better than atoi
    If you read the source, you'll see how atoi is just a wrapper function for strtol.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The runtime is implementation defined.
    That means atoi may or may not call strtol depending on who created the library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    strtol is better than atoi
    Why?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't really know why they wrote those functions in such a sloppy way to begin with. You'd think they would have done something like:
    Code:
    int atoi( const char* str, int* num, int base )
    Then they can just return a 0 or 1 if the function failed or not (or even better, just return the errno)...

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True. Standard API way. C runtime is flawed
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Can someone please help me with this....
    Here's a sample.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define  MAX_PESSOA 128
    #define MAX_TELE    37
    #define QTD 3
    #define MAX_TXT  128
    
    typedef struct {
        char name[MAX_PESSOA];
        char phone[MAX_TELE];
        int day;
        int month;
        int year;
        int num[QTD]; // if the person has more than one phone number
        char text[MAX_TXT];
    } Person;
    
    int writeacoupleofrecords(char *fname)
    {
        Person contacts;
        FILE *writefile;
        writefile=fopen(fname,"ab");  // Append records
        if (!writefile)
            return 1;
        strcpy(contacts.name, "Bob S.");
        strcpy(contacts.phone, "570-555-1212");
        contacts.day = 24;
        contacts.month = 12;
        contacts.year = 2007;
        contacts.num[0] = 1;
        strcpy(contacts.text,"text1 message");
        fwrite(&contacts,sizeof(Person),1,writefile);       
    
        strcpy(contacts.name, "Ted F.");
        strcpy(contacts.phone, "570-555-1313");
        contacts.day = 11;
        contacts.month = 9;
        contacts.year = 1989;
        contacts.num[0] = 1;
        strcpy(contacts.text,"text2 message");
        fwrite(&contacts,sizeof(Person),1,writefile);
        fclose(writefile);   
        return 0;
    }
    
    int readtherecords(char *fname)
    {
        FILE *readfile;
        Person contacts;
        readfile=fopen(fname,"rb");
        if (!readfile)
            return 1;
        while(fread(&contacts, sizeof(Person),1, readfile) == 1)
        {
            printf("%s %s %02d %02d %d %d %s\n",
                contacts.name,contacts.phone,contacts.day,contacts.month,contacts.year,contacts.num[0],contacts.text);
        }    
        fclose(readfile);
        return 0;
    }
    
    int main(void)
    {
        if(!writeacoupleofrecords("contacts.txt"))
            readtherecords("contacts.txt");
        else printf("writeacoupleofrecords failed\n");
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Thanks Bob... for the help :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Reading a file to variables
    By BigBadApe in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2002, 04:34 PM