Like Tree5Likes
  • 1 Post By jimblumberg
  • 2 Post By Salem
  • 2 Post By grumpy

EOF in text files

This is a discussion on EOF in text files within the C Programming forums, part of the General Programming Boards category; I read somewhere that the end of file in text files is indicated by a character whose ASCII value is ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    EOF in text files

    I read somewhere that the end of file in text files is indicated by a character whose ASCII value is 26, but I don't see any such character when I open a text file under a hex editor, so what does it really mean??

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I think that someone said the other day that this was how it used to be done.

    Nowadays the operating system will return the value of EOF (usually -1) when the end of the file is reached.

    Most probably an side effect of the respective file systems in use at the time.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,700
    Are you running your program on a CPM operating system? DOS and Windows never relied on some value inside the file to signal end of file. The end of file is handled by the operating system/file system, not some garbage inside the file.

    Jim
    Salem likes this.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    I hope we can get this done in fewer than 80 replies, like last time -> EOF

    But then again, this is the 4th or 5th thread you've started with this EOF hangup you seem to have.
    infinite loop
    wats wrong with this code

    Stop worrying about odd-ball dead operating systems, and realise that there is NO CHARACTER stored in a file to mark the end of a file. So stop looking for it - it isn't there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Actually, windows (xp at least) still considers an ascii-26 (ctrl-Z) to mark the end of a text file (for compatibility with CPM), although it does not NEED ctrl-Z to be there (and it would be kind of dumb to put it there). But if you put one before the end, the text beyond it will not be read (in text mode).
    Code:
    #include <stdio.h>
    
    int main() {
        char s[256];
        FILE *f = fopen("test.txt", "w");
        fprintf(f, "hello \x1a there\n"); /* \x1a is 26 decimal */
        fclose(f);
        f = fopen("test.txt", "r");
        fgets(s, 256, f);
        printf("[%s]\n", s);
        fclose(f);
        return 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by oogabooga View Post
    Actually, windows (xp at least) still considers an ascii-26 (ctrl-Z) to mark the end of a text file (for compatibility with CPM), although it does not NEED ctrl-Z to be there (and it would be kind of dumb to put it there). But if you put one before the end, the text beyond it will not be read (in text mode).
    Code:
    #include <stdio.h>
    
    int main() {
        char s[256];
        FILE *f = fopen("test.txt", "w");
        fprintf(f, "hello \x1a there\n"); /* \x1a is 26 decimal */
        fclose(f);
        f = fopen("test.txt", "r");
        fgets(s, 256, f);
        printf("[%s]\n", s);
        fclose(f);
        return 0;
    }
    WIN + R
    cmd
    more test.txt


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

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    And for those of us not going to try that out, what does it show?

    I'm guessing that only "hello " was written to the file?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    I read somewhere that the end of file in text files is indicated by a character whose ASCII value is 26, but I don't see any such character when I open a text file under a hex editor, so what does it really mean??
    Ok... lets put this mess to bed.

    Do you know what the return value of a function is?

    Do you understand what this code does...
    Code:
    int xray(int y)
      {
         return y;
      }
    
    int main (void)
      {
         int x;
    
         x = xray(42);
       
        return 0;
      }
    After calling the xray function what is the value of X ... why is x that value?

    That's right, it's that value because of the return value of the function.

    EOF is a value returned by various functions in the C Standard Library... There's no character in the file.

    It works like this...
    The FILE* handle is actually a struct with information about the file... it's name, size, etc. all obtained from the Operating System.

    When you open a file for reading the file pointer is intially set to 0... the beginning of the file. As the file (or stream) is manipulated by various functions you are passing the handle struct around to these functions that update it's contents as they go. For example: fseek() updates the file pointer, fopen() creates the struct and opens the file at OS level, fclose() closes the OS level file and destroys the struct, ftell() returns the file position from the FILE struct.

    As your program reads data from the file the file pointer is advanced automatically by the OS... reading 3 characters results in fpos += 3 which the OS then compares to the size of the file, obtained from its directory entry. When the file pointer goes beyond the end of the file (as in fpos > fsize) it causes an *error* which is returned to the function by the OS. When this happens scanf(), fread(), fgets() etc. exit with a return value of EOF.

    Remember how we got x to be 42 above... it's exactly the same "returned value" process. EOF is simply a value returned by the function.

    There is no EOF character or any other special marker in the file...

    Get it now? Can we please move on?

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    And for those of us not going to try that out, what does it show?

    I'm guessing that only "hello " was written to the file?
    Yes. Sorry, how rude of me! It prints just as you say. But notepad and more both show all the characters. At any rate, it's something to be avoided, which shouldn't be too hard in a text file.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    Quote Originally Posted by juice View Post
    I read somewhere that the end of file in text files is indicated by a character whose ASCII value is 26, but I don't see any such character when I open a text file under a hex editor, so what does it really mean??
    I've lost track of how many threads you have created, asking essentially this question over and over again, and ignoring the answers you have received.

    On some systems, end of file is indicated by presence of a particular character marker. On some such systems, that character is ASCII value 26 (CTRL-Z). On other such systems, it is ASCII value 4 (CTRL-D).

    The actual way that end of file is represented actually depends on the internal details of things like device drivers that manage file systems. Some file systems do not require any end-of-file marker be placed into a file at all. For example, a file system driver that keeps track of file sizes, may simply indicate end of file when an attempt is made to read more characters than a file has. The more advanced a file system, the less likely it is to require an end-of-file marker in individual files.

    Even on the file systems where an end-of-file marker is stored, the file system driver will not necessarily make that visible to user code (for example, C I/O functions, hex editors).

    I suppose you will ignore this answer as well. Ignoring answers does not make reality comply with your mistaken beliefs.
    Last edited by grumpy; 01-02-2012 at 03:46 PM.
    jimblumberg and stahta01 like this.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    I've lost track of how many threads you have created, asking essentially this question over and over again, and ignoring the answers you have received.
    No real interest in C programming... just wants to swim in the pond with the big fish.

    (Actually, I'm seeing a lot of this in here and other things I do... really aggrevating trend in youth... Ask a question and argue with the answer. Last time this happened I simply looked at the little snarker: "So if you're so much smarter than me why did you waste your time asking me a question" .... the kid honestly didn't know what I was saying to him....)

    On some systems, end of file is indicated by presence of a particular character marker. On some such systems, that character is ASCII value 26 (CTRL-Z). On other such systems, it is ASCII value 4 (CTRL-D).
    As far as I know...
    Windows and Linux have never used in-file markers.
    Unix did and MS-DOS did... but they're both history.

    That this remains in C functions is, well, idiotic.
    Last edited by CommonTater; 01-02-2012 at 04:17 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    And for those of us not going to try that out, what does it show?

    I'm guessing that only "hello " was written to the file?
    No, the whole thing is written to the file. He's just trying to say "Hey, an old text editor stops when it finds "EOF" (since it's not actually EOF). That's why I told him to more it.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text files
    By thinice16 in forum C Programming
    Replies: 5
    Last Post: 08-03-2008, 02:40 AM
  2. Text Files
    By trekintouretter in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2008, 04:34 PM
  3. Viewing other files... (Not Text Files)
    By Junior89 in forum C++ Programming
    Replies: 10
    Last Post: 07-05-2007, 02:30 AM
  4. text files
    By lilhawk2892 in forum C++ Programming
    Replies: 9
    Last Post: 07-04-2006, 11:23 PM
  5. Help With Text Files C++
    By liquidspaces in forum C++ Programming
    Replies: 8
    Last Post: 03-14-2003, 01:04 PM

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