Thread: Input from file using arrays or struct?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    Input from file using arrays or struct?

    I want to write a program that takes values from a text file, assigns them to different variables and then does different computations on those variables..

    I've tried using fscanf/fgets and freadf, but can't get any of these to copy anything but the first line of text..

    Also, I'm not sure whether it would make the most sense to sort them in an array? I have approximately 200 different values times 8 different variables

    I've been looking around for a while, and can't seem to find the information I need to solve this problem.. Any help is greatly appreciated!
    Last edited by kensing; 11-20-2012 at 02:36 AM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Have you tried this algorithm?
    Code:
    #define MAX_LINES 200
    #define MAX_LINE_LENGTH 100
    
    struct data_t
    {
        // types of values inside the file
    };
    
    struct data_t data[MAX_LINES];
    char line[MAX_LINE_LENGTH];
    int lineNo = 0;
    FILE *file;
    
    file = fopen(filename, "rb");
    if (file != NULL)
    {
        while (lineNo < MAX_LINES && fgets(line, MAX_LINE_LENGTH, file) != NULL)
        {
            // parse data from line to data[lineNo] using sscanf
            ++lineNo;
        }
        fclose(file);
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Thank you for the response, it seems very helpful, I'll look it over right now

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I'm having trouble figuring out how to parse to data[lineNo] with sscanf. You don't have to do it for me, if you could just refer me to some explanatory text on the subject


    Thank you in advance!

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Suppose your input file contains lines of values of different types:
    12 Mon 2.467
    67 Wed 14.23
    9600 Sat 0.129


    Now you have to fill the struct with members of those types
    Code:
    struct data_t
    {
        int value1;
        char day[4];
        double value2;
    };
    Parsing using sscanf:
    Code:
    while (...)
    {
        sscanf(line, "%d %s %lf\n", &data[lineNo].value1, data[lineNo].day, &data[lineNo].value2);
        ++lineNo;
    }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I get the following error messages

    test2.c:25:1: warning: data definition has no type or storage class
    test2.c:25:1: error: conflicting types for ‘file’
    test2.c:23:7: note: previous declaration of ‘file’ was here
    test2.c:25:14: error: ‘kampdata’ undeclared here (not in a function)
    test2.c:26:1: error: expected identifier or ‘(’ before ‘if’


    for the following code:
    Code:
    struct data_t
    {
       ...
    };
     
    struct data_t data[MAX_LINES];
    char line[MAX_LINE_LENGTH];
    int lineNo = 0;
    FILE *file;
     
    file = fopen(kampdata.txt, "rb");
    if (file != NULL)
    { ...

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. You have to put this code into a function.
    2. Use quotation marks around filename kampdata.txt.
    Last edited by DRK; 11-21-2012 at 06:47 AM.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Of course, bingo

    But how to access for example data.value1 in line 1?

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Assuming you read the first line into data[0], then the member value1 of line 1 is

    data[0].value1

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I got it all working now

    Thus my last question related to this thread is this:
    How do I choose to skip a piece of information in the file I'm reading from, using sscanf? For example, if my .txt file contains "a b c" variables, but only "a" and "c" is relevant to my program?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You could always scan it in and just not use it.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    To read, say, an integer and not do anything with it - You can use the %*d in the scanf family

    Code:
    int apple, orange;
    scanf(" %d %*d %d", &apple, &orange);
    First integer is put into apple, second read but ignored, third in orange.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Note that the '*' can be used with any input - %*c, %*d, ...
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Thank you - your responses have been very useful!

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    How can I create a new array that contains the different values of data[lineNo].value1?

    For example, I have data[1].value1 = 50 and data[2].value1 = 50 and data[3].value1 = 100

    I then want to group the ones that have value '50'

    What could that code look like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an input file into a struct.
    By Alan Gott in forum C Programming
    Replies: 4
    Last Post: 11-29-2011, 09:07 PM
  2. problems reading input file into two arrays
    By quasigreat in forum C Programming
    Replies: 2
    Last Post: 05-18-2008, 03:53 PM
  3. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM
  4. Input for Struct?
    By stewade in forum C Programming
    Replies: 26
    Last Post: 05-03-2004, 11:52 AM
  5. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM