Thread: Help in reading from file using fgets with array struct.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    30

    Help in reading from file using fgets with array struct.

    Hello my dears,
    I have a problem regarding to reading from a file.
    The problem is that I have 4 lines in the text file, the first is the IDs of employees, and the second line is their names, so I want to read it and sort it inside an array struct, but this attached code is for the first two lines, because the code works for the last two lines perfectly. I watched many concepts of reading from files and about fgets but I didn't find anything useful.
    Code:
        FILE*in;    char tempNames[8];
        char *namesLine;
        int i;
        in=fopen("employees.txt", "r");
        employee_record AR[8];
            for (i=0; i<8; i++){
                fscanf(in, "%d", &AR[i].e_num);
            }fgets(tempNames, 8, in);
            namesLine=strtok(tempNames, " ,;");
            i=0;
            while (namesLine!=NULL){
                strcpy(AR[i++].name, namesLine);
                namesLine=strtok(NULL, " ,;");
            }
            for (i=0; i<8; i++)
                printf("%d %s\n", AR[i].e_num, AR[i].name);
    File's data:
    111111 222222 333333 444444 555555 666666 777777 888888
    Sami;Mohammad Kamal,Waleed Maryam;Bayan Ahmad,Jaber
    That what I get when I run the compiler:
    111111

    222222 └i
    333333 └i
    444444 ╝>╜┘
    555555 Ç
    666666 ╠i
    777777 πnàvΦ>╜┘α@
    888888 ╠ a
    Last edited by amohammed63; 05-05-2020 at 06:58 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't mix fscanf with fgets. Use fgets to read line by line. Parse the first line with sscanf; parse the second line with strtok.

    tempNames, at just 8 characters, doesn't cut it for reading a line. I would declare:
    Code:
    char buffer[BUFSIZ];
    You don't need a for loop that just runs once, so simplify by getting rid of that extra loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    How can I use sscanf for the first line which is a integer data? Also, I really don't know how to read line by line by using fgets.
    If you could give me few hints about buffer since I don't know it.
    Last edited by amohammed63; 05-05-2020 at 06:56 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amohammed63
    How can I use sscanf for the first line which is a integer data?
    If it is expected that there is going to be 8 employee IDs, then just do one sscanf call with a suitable format string to get all 8 at one go. If not, you might want to parse the line into string tokens using strtok instead, then parse each token using sscanf or strtol, keeping track of the number of IDs for the next line.

    Quote Originally Posted by amohammed63
    Also, I really don't know how to read line by line by using fgets.
    It is just two lines, i.e., two fgets calls.

    Quote Originally Posted by amohammed63
    If you could give me few hints about buffer since I don't know it.
    You use it to store the line read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Quote Originally Posted by laserlight View Post
    If it is expected that there is going to be 8 employee IDs, then just do one sscanf call with a suitable format string to get all 8 at one go. If not, you might want to parse the line into string tokens using strtok instead, then parse each token using sscanf or strtol, keeping track of the number of IDs for the next line.
    Yeah it's known that the line contains 8 IDs.
    This code keeps reading the first ID
    Code:
    fgets(buffer, BUFSIZ, in);    for (i=0; i<8; i++){
            sscanf(buffer, "%d", &AR[i].e_num);
        }
    or should I do this way
    sscanf(buffer, "%d %d %d ....", AR[0].e_num, AR[1].e_num, AR[2].e_num, ....);?

    Also the same problem with the second line, it keeps reading the first name
    Last edited by amohammed63; 05-05-2020 at 07:40 PM.

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Never mind, I solved everything.
    Thank you for helping.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    #include <stdio.h>
     
    int main() {
        char buffer[1024] = "12 123 1 1234 21 2 321 32\n";
        int n[8];
     
        char *pbuf = buffer;
        for (int i = 0, pos = 0; i < 8; ++i)
            sscanf(pbuf += pos, "%d%n", &n[i], &pos);
        
        for (int i = 0; i < 8; ++i)
            printf("%d\n", n[i]);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a .txt file into struct array
    By Alpha8 in forum C Programming
    Replies: 5
    Last Post: 12-01-2019, 12:38 PM
  2. Problem with reading a file into array of struct
    By Lozy in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2014, 08:29 AM
  3. Replies: 13
    Last Post: 12-17-2013, 07:18 PM
  4. Replies: 2
    Last Post: 05-04-2013, 04:29 PM
  5. reading the content of a file into an array of struct
    By m_programmer in forum C Programming
    Replies: 6
    Last Post: 09-20-2012, 07:29 AM

Tags for this Thread