Reading issue, mixed text file

This is a discussion on Reading issue, mixed text file within the C Programming forums, part of the General Programming Boards category; I am reading in a 2mb mixed file (i.e. contains CFLF and LF) and non-standard characters. Now, at the start ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    Reading issue, mixed text file

    I am reading in a 2mb mixed file (i.e. contains CFLF and LF) and non-standard characters. Now, at the start of the program the file is redirected into stdin from the command prompt. The problem is the file is never fully read.

    It gets to a certain point in the file and decides that its at the end of the file! How can I solve this issue?

    Sample code (not the actual code of my program as this is an assignment). Note that in the assignment specs it explicitly states that this is a text file that we are reading.

    running from command prompt:

    prog < file > output

    Code:
    #define READ_LINE_SIZE 1024
    ...
    char *buf = (char *)malloc(sizeof(char) * READ_LINE_SIZE);
    while (!feof(stdin))
    {     
       fgets(buf, READ_LINE_SIZE, stdin);
       printf("%s", buf);
    }
    free(buf);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop using the standard input stream for starters. It's probably getting one of those nice little "non standard characters", which it happens to consider the equivalent value of CTRL Z or CTRL D (depending on OS/shell), and stops reading. Also, don't use feof to control loops. Read the FAQ section if you want to know why.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    I'm using stdin because its a requirement of the assignment. I started using the return value from fgets itself but it always stops at the same spot.

    Can you recommend an alternative to fgets? I also tried using fread but it also doesn't read the whole file.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    size_t x = 0;
    while( (c = fgetc( myfile )) != EOF )
    {
        x++;
        printf( "Character %d is %c, value of %d\n", x, c, c );
    }
    printf( "Total bytes read: %d\n", x );
    See where it's stopping and find out why (what's really there, or if it's really reaching the end of your file). Move x to after the printf statement, if you want to start counting at zero instead of one. And actually open the file, don't pipe it in. Read it and find out what's wrong with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Its indeed reaching an EOF character, however that is not the actual end of the file. Its only about 1/2 through the file. It doesn't print out the value in the loop, so I printed it out after the end of the loop.

    Character 1356031 is , value of -1

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    So you know using fseek I verified that the file size is indeed a lot larger then the amount of data read in.

    Code:
    fseek(myFile, 0, SEEK_END);
    printf("File Size: %d\n", ftell(myFile));
    This gives me a size of 2107007. There is 750977 bytes that aren't getting read.

    Thanks for your help so far.

    EDIT, not suprisingly, when I open the file using binary mode it reads the whole file without problems.

    Is there anyway to reopen the standard stream to read binary?
    Last edited by hawaiian robots; 05-22-2009 at 05:22 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could try freopen, but you need to pretty much do that before you try to do any other IO.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Quote Originally Posted by quzah View Post
    You could try freopen, but you need to pretty much do that before you try to do any other IO.


    Quzah.
    Yeah I figured as much while researching it. Anyway, thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM

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