#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0

    Reading data from a 4 column file into an interger pointer variable


    Hi,

    I know how to read data from a text file and to store it in a variable.

    I now have to read data from a 4 column file where each column is separated by a whitespace and store the data from the second column into an interger pointer variable. The length of the column is known in advance and is always fixed.

    Can somone point me in the right direction by providing a snippet of the line that reads data from the 2nd column and stores into the interger pointer variable? Thats where I am stuck. I am not a programmer but rather someone who has to write code occasionally so this particular line is baffling me!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    What language? This forum deals with C (in its various forms; eg C89, C99, Objective-C), C++, and C#. We would commonly assume C as per the 1989 ISO standard (AKA "C89") and what we would answer would also apply to C++, but not to C# and possibly not to Objective-C.

    Let's assume C. Using fscanf would do the job, since each column is separated by whitespace. However, I would recommend first reading in the entire line and then processing that line. Look into fgets() to read an entire line of text from a file. Then look into sscanf to process the second column of that line of text.

    BTW, *scanf and *printf are families of functions which all operate very similarly, but one variant works with the standard terminal files (stdin and stdout), another with files (substituting "f" for that "*"), another with strings (substituting "s" for that "*").

    Find out about those functions and how to use them either with your compiler's help files or by Google'ing on man page function-name.

    Once you know what function(s) to use, you can then Google for sample code (good luck on that! Like finding an old high school friend using a phone book.). Or play with the functions to figure them out.


    Of course, if you're trying to do this with C# or something else that does not have *scanf (C++ instructional books won't ever tell you this, but C++ can work perfectly well with *scanf), then what I just told you will mean nothing. But then, shouldn't you have told us up-front what you're using?
    Last edited by dwise1_aol; September 10th, 2012 at 02:04 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0
    Originally Posted by dwise1_aol
    What language? This forum deals with C (in its various forms; eg C89, C99, Objective-C), C++, and C#. We would commonly assume C as per the 1989 ISO standard (AKA "C89") and what we would answer would also apply to C++, but not to C# and possibly not to Objective-C.
    I meant the C language, without stating what I think was quite the obvious given that this is a C Forum.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Look towards the top of the page for this text:
    C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
    Or, to take our inspiration from Chief Inspector Jacques Clouseau (from early in the movie, A Shot in the Dark:
    I assume nothing. I suspect everything.
    IOW, given what this very forum says about itself, it is not "quite the obvious given that this is a C Forum".

    However, given your response, I feel free to assume that you are indeed talking about C itself. That being the case, I hope that my reply was helpful.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0
    Yes, thanks. I have started reading on fscanf and fgets and am currently experimenting with them.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Just a quibble: it's either fscanf, or a combination of fgets and sscanf.

    Since fscanf would be sensitive to how many columns there are, I would personally gravitate towards fgets and sscanf. Please think it through and see what I mean.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0
    Hi,

    Here is what I have written so far. I get some results but not exactrly what I want.
    If I put the result of fscanf as 1, then I can only the 1st line (with all four columns ) printed. Other entries are 0.

    If I put result of fscanf as 4 (which is what it should be since I am reading 4 values), the the LAST line of data file is printed (with 4 columns) and all other entries are 0. Do you think you can spot where I am wrong?

    Mv data file has intergers in the first two columns and doubles in the last two. There are 640 entries in the data file.

    int main(int argc, char *argv[]){

    FILE *in;
    int column[640];
    int column2[640];
    double column3[640];
    double column4[640];

    // Specify file to read
    sprintf(filename, "log-1.dat");
    in = fopen(filename, "r");

    if ( in ){
    while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
    }

    for ( int i = 0; i < 10; i++ ){
    printf("array[%d] = %d, %d, %lf, %lf\n", i, scan[i], scan2[i], scan3[i], scan4[i]);
    }

    return 0;
    }
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    *scanf needs addresses where it can store the converted values. The address operator is &.

    What are the declarations for scan, scan2, scan3, scan4, and scan[]?

    Also, please use code tags when you post code.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0
    Hi,

    Sorry about not using the code tag. Also, scan[i] corressponds to the columns of data in the text file. Four columns so four scan i's. Thing is that after compiling my code, I copied and pasted and thought of renaming all the scan i's to columns to make it better understood but in the process I missed out renaming all :(

    The code works but only for the first or last line of the data file. I have tried searching high and low and have come across lots of similar questions but without any solutions

    Code:
    int main(int argc, char *argv[]){
    
    FILE *in;
    int scan[640];
    int scan2[640];
    double scan3[640];
    double scan4[640];
    
    // Specify file to read
    sprintf(filename, "log-1.dat");
    in = fopen(filename, "r");
    
    if ( in ){
       while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
    }
    
    // printing out first 10 values only
    for ( int i = 0; i < 10; i++ ){
    printf("array[%d] = %d, %d, %lf, %lf\n", i, scan[i], scan2[i], scan3[i], scan4[i]);
    }
    
    return 0;
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Given:
    Code:
    int scan[640];
    int scan2[640];
    double scan3[640];
    double scan4[640];
    this line of code,
    while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
    makes absolutely no sense whatsoever.

    You might possibly have wanted to use some iteration subscript such as n, in which case it should have been:
    while (fscanf(in, "%d %d %lf %lf", &scan[n], &scan2[n], &scan3[n], &scan4[n]) == 4){}
    Please note the use of the address operator, &, to get the address of that particular element in the array, as well as the subscripting of the array to access each individual element. Very, very basic programming concepts (well, the array subscripting at least, though the address operator isn't exactly rocket science).

    What you had done was to give fscanf array names, which are treated the same as addresses. Only the address an array name provides is only to the first element in the array.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0
    Thanks for your help so far but I gave up and demanded the format of the data file to be changed to a 1 column file. Now it makes my life easier.

    Thanks again for the pointers you gave,

IMN logo majestic logo threadwatch logo seochat tools logo