wierd fopen prob

This is a discussion on wierd fopen prob within the C Programming forums, part of the General Programming Boards category; i'm getting a prob with fopen in a loop to copy a file into a buffer: it returns EOF when ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    wierd fopen prob

    i'm getting a prob with fopen in a loop to copy a file into a buffer:
    it returns EOF when it tries to read 1A (26) wherever in the file a put one

    here's a test that reproduces the error

    Code:
    #include <stdio.h>
    int main(void)
    {
       FILE *file=fopen("./in.bmp","r");
       int byte;
       if (!file) return 1;
       while ( ( printf( "%2X", byte = fgetc( file)), byte ) != EOF );
       if (file) fclose(file);
       getchar();
       return 0;
    }
    any ideas?
    thx, jo

  2. #2
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    An idea, open file in binary mode, "rb"

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    thx c99

    cool, that works!
    why?...

    jo

  4. #4
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    jo.

    The End Of File (EOF) is indicated by ASCII 1A (26)(Ctrl-Z) in text mode.

    Hope that helps.

  5. #5
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    aha

    well that explains...
    thx.


    Code:
    printf( "\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7\7");
    or how to annoy one's teacher
    lol

    jo

  7. #7
    Deleted Account
    Join Date
    Jan 2004
    Posts
    40
    heh printf("\b"); also works.

  8. #8
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    brian.

    Cool, there's more here.

    http://www-ccs.ucsd.edu/c/charset.html

    Take a peek.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while ( ( printf( "%2X", byte = fgetc( file)), byte ) != EOF );
    A more conventional (and safer) way to write that line would be:
    Code:
    while ((byte = fgetc(file)) != EOF)
    {
      printf("%2X", byte);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by Brian2
    heh printf("\b"); also works.
    Actually, '\a' is the bell. '\b' is backspace.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    7
    A more conventional (and safer) way to write that line would be:

    Code:
    while ((byte = fgetc(file)) != EOF)
    {
      printf("%2X", byte);
    }
    that's because I wanted to print the EOF as well.

    but it's just a test that reproduces an error in my main prog
    the main one doesent do any printing, it was just to try and understand what was happening.

    thx 4 the link, that could come in useful sometime

    Jo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. wierd segmentation fault with fopen
    By jaredw in forum C Programming
    Replies: 5
    Last Post: 08-27-2008, 07:43 PM
  3. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  4. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 08:56 PM
  5. prob with fopen
    By ukcpaul in forum C Programming
    Replies: 2
    Last Post: 01-09-2002, 07:23 AM

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