End of file

This is a discussion on End of file within the C Programming forums, part of the General Programming Boards category; I have a small problem . I have a text file which i have to truncate after a conditionis met ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    38

    End of file

    I have a small problem . I have a text file which i have to truncate after a conditionis met (after i reach a specific point). So i thought that instead of copying that file to another one .. what i would do is that i would add end of file character at the desired location. So i checked for the condition and when it was satisfied i put end of file (ascii 26) using fprinf(fp,"%c",26). But this trick is not working as instead of end of file a special symbol is being inserted instead.

    So my problem is Can i insert end of file in middle of a text file and if yes then how??
    Please help.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The short answer is: No.

    The size of a file is contained in its directory entry, and doesn't depend on any character contained in the file itself.

    Now, back in the olden days (CP/M, anyone?), file lengths were defined in multiples of some block size, and if the size of a text file was not an exact multiple of the block size, a special non-printing character was placed to indicate the end of the file. This character was ascii 26 (sometimes known as control-z).

    [edit]
    As a matter of fact, if you put control-z (ascii 26) somewhere in a text file, I have found out that a c-program compiled by Borland C++ 5.5.1 will return EOF when getchar() encounters that character. (Also Microsoft Visual C++). Legacy code from ???? Or feature ???

    Gnu C does not exhibit this behavior on Windows or Linux platforms.
    [/edit]


    Dave
    Last edited by Dave Evans; 08-28-2004 at 02:33 PM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    thanx dude
    is there any non standard funtion to do what i wanna do??

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >So my problem is Can i insert end of file in middle of a text file and if yes then how??
    EOF is NOT:
    • A char
    • A value that exists at the end of a file
    • A value that could exist in the middle of a file
    How can I insert or delete a line (or record) in the middle of a file?
    Short of rewriting the file, you probably can't. The usual solution is simply to rewrite the file.
    [edit]>is there any non standard funtion to do what i wanna do??

    When you go nonstandard, it's best to mention the platform. But here is somewhere to start:
    Last edited by Dave_Sinkula; 08-28-2004 at 02:47 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    platform i generally use is dos/windows
    but i also have linux on my system so i can go for that also if it has anything specific

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Dave Evans
    [edit]
    As a matter of fact, if you put control-z (ascii 26) somewhere in a text file, I have found out that a c-program compiled by Borland C++ 5.5.1 will return EOF when getchar() encounters that character. (Also Microsoft Visual C++). Legacy code from ???? Or feature ???

    Gnu C does not exhibit this behavior on Windows or Linux platforms.
    [/edit]


    Dave
    Yeah, in UNIX (and I assume Linux) EOF is simply that, end of file - the read() routine returns how many bytes are left of whatever is being read - if that number is 0, then its end of file.

    ~/
    Last edited by kermit; 08-28-2004 at 04:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 02:41 AM

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