Thread: Entire array is not printing--why?

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    19

    Entire array is not printing--why?

    Hello,
    I have a file with approx. 2 million rows. I wrote a simple script to simply print out each row but the script is only printing out the first 745000 rows...I would really appreciate it if someone could let my know why.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    enum { SIZE = 2000000 };
    int main(int argc, const char * argv[])
    {
      char b[SIZE];
            FILE *fp = fopen("myfile.txt","r");
        size_t ret_code = fread(b, sizeof *b, sizeof(b)/sizeof(b[0]), fp); 
        if(ret_code == SIZE) {
            for(int n = 0; n < SIZE; ++n) printf("%c", b[n]);
            {
            putchar('\n');
            }
        } else { 
            if (feof(fp))
            {
                printf("Error reading test.bin: unexpected end of file\n");
            }
            else if (ferror(fp)) {
                perror("Error reading test.bin");
            }
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you confusing "rows" with characters? What is the file format?
    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
    Jun 2015
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Are you confusing "rows" with characters? What is the file format?
    Thank you! I probably am confusing "rows" with charachters. The file format is plain text I have 200000 "rows" that look like the first 5 rows below:
    1 2 3 4 5
    6 7 8 9 10
    20 24 26 27 28
    7 8 9 33 34
    1 2 23 24 25
    etc..etc...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Charles Driver View Post
    Thank you! I probably am confusing "rows" with charachters. The file format is plain text I have 200000 "rows" that look like the first 5 rows below:
    1 2 3 4 5
    6 7 8 9 10
    20 24 26 27 28
    7 8 9 33 34
    1 2 23 24 25
    etc..etc...
    Are you sure it's not a text file?
    Something like this could work, based on what I see:
    Code:
    int i = 0;
    int b[??] = {0};
    
    while ( fscanf(fp, "%d %d %d %d %d", &b[i], &b[i + 1], &b[i + 2], &b[i + 3], &b[i + 4]) == 5) 
    {
        printf("[%d] %d %d %d %d %d\n", i, b[i], b[i + 1], b[i + 2], b[i + 3], b[i + 4]);
        i += 5;
    }
    Last edited by whiteflags; 06-19-2015 at 12:32 PM.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by whiteflags View Post
    Are you sure it's not a text file?
    Something like this could work, based on what I see:
    Code:
    int i = 0;
    int b[??] = {0};
    
    while ( fscanf(fp, "%d %d %d %d %d", &b[i], &b[i + 1], &b[i + 2], &b[i + 3], &b[i + 4]) == 5) 
    {
        printf("[%d] %d %d %d %d %d\n", i, b[i], b[i + 1], b[i + 2], b[i + 3], b[i + 4]);
        i += 5;
    }
    Thank you for your response! I think my original post may be confusing, so I will try to clear it up.

    I have a text file with 2000000 newlines of serial numbers, my file looks like this:
    1 2 3 4 5
    6 7 8 9 10
    20 24 26 27 28
    7 8 9 33 34
    1 2 23 24 25
    etc, etc for 2000000 rows

    I want to put each row in its own separate row in an array an print the array with the 2000000 rows.

    In php to do this is simple:
    Code:
    $myArray=file("somefile.txt");
    That's it! Now all of the rows in somefile.txt have populated $myArray. How can I do this equivalent in C?? I attempted above but it is not populating fully, it is only taking the first 700000 numbers. THANK YOU FOR ANY AND ALL HELP

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah you should use fscanf() then. It can be similar to what I showed you.

    I dunno about storing the whole file as an array though. It's definitely possible, but it is a huge array. An integer is typically 4 bytes, so

    4 * 5 * 2 million = 40 megabytes

    You're going to want to allocate that much on the heap with malloc or calloc. Unless it's important for some reason, I wouldn't worry about preserving the file's formatting. That is, I wouldn't read the file into an integer matrix (5 x 2 million cells).

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by whiteflags View Post
    Yeah you should use fscanf() then. It can be similar to what I showed you.

    I dunno about storing the whole file as an array though. It's definitely possible, but it is a huge array. An integer is typically 4 bytes, so

    4 * 5 * 2 million = 40 megabytes

    You're going to want to allocate that much on the heap with malloc or calloc. Unless it's important for some reason, I wouldn't worry about preserving the file's formatting. That is, I wouldn't read the file into an integer matrix (5 x 2 million cells).

    Thanks for your help*...I'll try to see if I can get it working with the fscanf(). If anyone else has suggestions, I would so appreciate it.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Could you describe what you are actually trying to do? Maybe there's a better way. You say you want to read the values into an array and then print it, but if you want to print them row-by-row then you don't need to read them all into an array first. So what exactly are you trying to do with all those numbers?

  9. #9
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    If you are trying to mimic the behavior of PHP's file() (without the optional arguments), you need to create an array of cstrings, with each cstring being a copy of a file-line.

    For that, fgets() would be a better option for reading the file into the array of cstrings. If you are not familiar with pointers and dynamic memory management, the task may not be trivial, but if you are it's not a big deal (assuming the max length of any line in the file is known before hand).

    A struct describing a line like the following:
    Code:
    typedef struct Line {
        char *string;
    } Line;
    may prove handy if you don't feel like dealing with double pointers.

    Since mem for 2000000 cstrings may be quite big for the stack, you can allocate dynamically in the heap an array of 2000000 Line structs...
    Code:
    Line *lines = calloc( 2000000, sizeof(*lines) );
    Then you can fgets() each line from the file into a temp cstring within a loop, duplicating it into lines[i].string as you go.
    Last edited by migf1; 06-19-2015 at 02:12 PM.
    "Talk is cheap, show me the code" - Linus Torvalds

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by algorism View Post
    Could you describe what you are actually trying to do? Maybe there's a better way. You say you want to read the values into an array and then print it, but if you want to print them row-by-row then you don't need to read them all into an array first. So what exactly are you trying to do with all those numbers?
    Thank you! Yes it would be more helpful if I explained my goal:
    I have a file with a couple of million serial numbers in it, many of the serial numbers are duplicates within the file. For example, some serial numbers can be in the file 7 times and other serial numbers can be in the file 10 times etc. how many times a serial number is in the file is variable.
    I want to know which serial numbers are in the file exactly 5 times and I want to store those in an array. That is all I am trying to do.
    Sure do hope that helps...

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by migf1 View Post
    If you are trying to mimic the behavior of PHP's file() (without the optional arguments), you need to create an array of cstrings, with each cstring being a copy of a file-line.

    For that, fgets() would be a better option for reading the file into the array of cstrings. If you are not familiar with pointers and dynamic memory management, the task may not be trivial, but if you are it's not a big deal (assuming the max length of any line in the file is known before hand).

    A struct describing a line like the following:
    Code:
    typedef struct Line {
        char *string;
    } Line;
    may prove handy if you don't feel like dealing with double pointers.

    Since mem for 2000000 cstrings may be quite big for the stack, you can allocate dynamically in the heap an array of 2000000 Line structs...
    Code:
    Line *lines = calloc( 2000000, sizeof(*lines) );
    Then you can fgets() each line from the file into a temp cstring within a loop, duplicating it into lines[i].string as you go.
    Thank you for this suggestion and letting me know it might not be trivial to do at my level of knowledge of C. I will try all of these suggestions and report back.

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Charles Driver View Post
    Thank you! Yes it would be more helpful if I explained my goal:
    I have a file with a couple of million serial numbers in it, many of the serial numbers are duplicates within the file. For example, some serial numbers can be in the file 7 times and other serial numbers can be in the file 10 times etc. how many times a serial number is in the file is variable.
    I want to know which serial numbers are in the file exactly 5 times and I want to store those in an array. That is all I am trying to do.
    Sure do hope that helps...
    I'm assuming the serial numbers are not sorted.

    Do the serial numbers take the form you show? Five small integers? What's the range of the integers? Are the five integers of a serial number always on the same line and in a particular order (e.g., increasing, as you've shown)? Show some real data.

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    19

    Cool

    Quote Originally Posted by algorism View Post
    I'm assuming the serial numbers are not sorted.

    Do the serial numbers take the form you show? Five small integers? What's the range of the integers? Are the five integers of a serial number always on the same line and in a particular order (e.g., increasing, as you've shown)? Show some real data.
    Ahhhh...Great idea to show real data , I have attached a plain txt file to show exactly what it looks like:
    boardpatterntype1_converted_merged_level_final_final_round_4_6.txt

    You are correct, the info is not sorted. The info is not in any particular order in the file and thankfully its order is not important, I am trying to find ALL of the serial numbers that appear n times. The highest integer there can be is 35 and the lowest is 1.

    There can be all single integers in a row, or there can be all double integers in a row or a combination of both. There just has to be 5 integers with a space in between each integer, like in the file.
    Thank you for the Question.

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I'm a little confused here. How do you define a "serial number"? Does each line contain 5 "serial numbers"? Or does each line contain a single "serial number" consisting of 5 individual integers?

    If each line has 5 serial numbers, where a serial number can range from 1 to 35, then your sample file contains all 35 possible serial numbers, and the serial number 6 shows up the least at a mere 2907 times. The serial number 4 shows up the most at 3102 times.

    If that's not what you expect, you need to define your terms and requirements more clearly.

  15. #15
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by christop View Post
    I'm a little confused here. How do you define a "serial number"? Does each line contain 5 "serial numbers"? Or does each line contain a single "serial number" consisting of 5 individual integers?

    If each line has 5 serial numbers, where a serial number can range from 1 to 35, then your sample file contains all 35 possible serial numbers, and the serial number 6 shows up the least at a mere 2907 times. The serial number 4 shows up the most at 3102 times.

    If that's not what you expect, you need to define your terms and requirements more clearly.
    I appreciate you taking the time to point out the confusing point of "serial numbers". Each line contains a single "serial number" consisting of 5 individual integers. Maybe I should get rid of the word "serial" and just call each row a number.

    For example the very first row of the attached text file I provided above is "1 2 3 4 14". I would like to know how many times this number of "1 2 3 4 14" appears in the file as a duplicate . The file I attached is only to show an example of what a file would look like, but I could not add in the duplicates because the .txt file in original duplicate form was too large for upload. So no number is duplicated in the example file because duplicated file is too big for uploading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printing .txt from an array
    By shaddock in forum C Programming
    Replies: 5
    Last Post: 06-03-2015, 05:07 AM
  2. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 01:09 PM
  3. Printing an array
    By LoveTractor in forum C Programming
    Replies: 5
    Last Post: 11-21-2010, 04:26 PM
  4. Replies: 4
    Last Post: 05-17-2009, 03:28 AM
  5. printing array
    By Cpro in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2008, 09:47 AM

Tags for this Thread