Problem with fscanf loop!

This is a discussion on Problem with fscanf loop! within the C Programming forums, part of the General Programming Boards category; Hi all, Apologies for posting a thread so soon after finding an answer to my last one. This little snippet ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    Problem with fscanf loop!

    Hi all,

    Apologies for posting a thread so soon after finding an answer to my last one. This little snippet of my code is driving me crazy.

    Soooooo I dont understand the output for my code. Right now my text file is like this.

    20010929 0 1
    20010929 15 2
    20010929 30 3
    20010929 45 4
    20010929 0 1
    20010929 15 2
    20010929 30 3
    20010929 45 4
    20010929 0 1
    20010929 15 2
    20010929 30 3
    20010929 45 4

    So with my code I'm hoping to put it into an array. This is just a pratice .txt as the one i will be using is 330770 rows long. When i display the 2nd row, 3rd column. The program displays a 45 instead of a 2. Any help or correction in understanding would be much appreciated. I'll add that it was working fine until I started to use malloc to define my array.




    Code:
        
       FILE *fr;                                                                                // declare the file pointer 
       
       int x, y;                                                                                // Dimensions of .txt data, 3 columsn and 330770 inputs;
       
       //DYNAMIC MEMORY ALLOCATION FOR data ARRAY and finalData ARRAY.
       
       int** data; 
       data = (int **) malloc(330770 * sizeof(int*)); 
       for (int h = 0; h < 330770; h++)  
                data[h] = (int*) malloc(3*sizeof(double));
                                                                                                //determines the placement of the finalData
       fr = fopen ("c:\\blahblah.txt", "r");                                                    /* reads the file */                                 
       for(x=0; x<330770; x++){                                                                 // loops through the rows
                         y = 0;                                                                 //starts at first column during each row loop        
                         while(fscanf(fr,"%d", &data[x][y++]) >0);                             // loops through the columns until each row ends.
       }
       
       cout<<data[1][0];

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    You changed from double to int, presumably to simplify things during testing, but now this is wrong:
    Code:
    data[h] = (int*) malloc(3*sizeof(double));
    You could have avoided this problem by writing:
    Code:
    data = malloc(330770 * sizeof(data[0]));
    for (int h = 0; h < 330770; h++) {
        data[h] = malloc(3 * sizeof(data[0][0]));
    }
    By the way, since you have a rectangular 2D array for which the inner arrays are of fixed size, this might be better:
    Code:
    int (*data)[3] = malloc(330770 * sizeof(data[0]));
    /* no need to allocate for each inner array */
    EDIT:
    Also, I strongly suggest that you use const or #define to give a name to 330770. It is just too magical.

    EDIT #2:
    Oh, and this is wrong:
    Code:
    while(fscanf(fr,"%d", &data[x][y++]) >0);
    You should be checking that the number of times this loop runs is exactly 3.
    Last edited by laserlight; 07-08-2012 at 09:14 PM. Reason: Wrong magic number. See? Don't use magic numbers!
    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
    Jul 2012
    Posts
    9
    You are seriously the most awesome person on the internet <3. I did all the fixes and it works great!

    However, if i use...
    Code:
    int (*data)[3] = malloc(330770 * sizeof(data[0]));
    it gives me an error saying invalid conversion from void to int[*][3]

    Thank you so much! I understand fscanf a lot more now.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Quote Originally Posted by turkadurka
    You are seriously the most awesome person on the internet <3. I did all the fixes and it works great!
    You're welcome!

    Quote Originally Posted by turkadurka
    it gives me an error saying invalid conversion from void to int[*][3]
    You're probably compiling as C++. Configure your compiler to compile as C and/or use a ".c" extension for your source file(s).
    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
    Jul 2012
    Posts
    9
    haha yep! That was the problem!

    Thank you again, laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do a while loop until fscanf hits a string?
    By d1puffpuff in forum C Programming
    Replies: 3
    Last Post: 10-28-2011, 01:52 PM
  2. fscanf printf while loop help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 02:10 AM
  3. fscanf in loop
    By pshirishreddy in forum C Programming
    Replies: 4
    Last Post: 11-08-2008, 04:50 AM
  4. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  5. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-04-2007, 11:14 PM

Tags for this Thread


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