EOF Oddity

This is a discussion on EOF Oddity within the C Programming forums, part of the General Programming Boards category; When I write out a character with a value of 26 to a file and then read it back in, ...

  1. #1
    Anonymous
    Guest

    Post EOF Oddity

    When I write out a character with a value of 26 to a file and then read it back in, instead of getting that character back I get an EOF. It is clearly an EOF because that read operation is checked for error at line 23. Also the write operating is checked for error at line 12 so the file has indeed been written to. I have also verified that after this program has been run the file has exactly one character written to it. I can't figure out why I can't read that character back in.

    Program Source:

    Code:
    1: 
    2: #include <stdio.h>
    3: 
    4: int main()
    5: {
    6: 	int ch;
    7: 	FILE *test;
    8: 	
    9: 	if (!(test = fopen("test.txt", "w")))
    10: 		return 1;
    11: 	
    12: 	if (fputc(26, test) != 26)
    13: 		return 3;
    14: 	
    15: 	fclose(test);
    16: 	
    17: 	
    18: 	if (!(test = fopen("test.txt", "r")))
    19: 		return 1;
    20: 	
    21: 	ch = fgetc(test);
    22: 	
    23: 	if (ferror(test))
    24: 		return 2;
    25: 	
    26: 	printf("%d\n", ch);
    27: 	printf("%c\n", (char)ch);
    28: 	fclose(test);
    29: 	
    30: 	return 0;
    31: }
    32:
    Program Output:

    Code:
    -1

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    26 is outside the printable range, so on my compiler I have to open the file with mode rb.

    Code:
    if ((test = fopen("test.txt", "rb")) == NULL)
    	return 1;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    If I'm not mistaken, 26 has the same effect of pressing control + Z which is dos's EOF shortcut key.

    I don't believe that it is a printable character, but more like hearing the pc speaker go off when character 7 is acted on.

    doh... Hammer beat me to it.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532

    Reduce a FILE. Easy way. set EOF?

    Hi,
    A text file is N byte in size. I want keep 0 to L where L is smaller than N.

    I want to set end of file at L th position of the file.

    Is that possible? Or any other idea/solution.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  5. #5
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    Is there anything to solve the problem with
    File control structure for streams.
    Code:
      typedef struct {
        short             level;
        unsigned       flags;
        char               fd;
        unsigned       char  hold;
        short             bsize;
        unsigned char *buffer, *curp;
        unsigned       istemp;
        short             token;
      } FILE;
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  6. #6
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    It's Linux & Slackware. But I will like ANSI functions..
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  7. #7
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    Seems interesting...
    Okay lets see what is it really doing internally...
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Originally posted by zahid
    Seems interesting...
    Okay lets see what is it really doing internally...
    Are you asking us to show you..or....????

  9. #9
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    No, thanks. I have already got it. But why it is not in ANSI.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by zahid
    No, thanks. I have already got it. But why it is not in ANSI.
    Because ANSI didn't specify it (ie: didn't require it to be done in a specific way, or at all) in the standard.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 03:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM

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