Thread: c++ eof chrachter

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    c++ eof chrachter

    Hi all
    I need a some help inserting a c++ end of file character in a(.txt) file so that when some one comes to read the file he will reach the virtual end before reaching the physical end of the file, the eof charachter should be inserted at the first white space.
    (what is the character and how to insert it
    and how to reset the flags after i reach an eof)
    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    EOF cannot be represented as a char, as its value is outside of the given range.

    You will need to "invent" your own eof type character, something in the range of 0x00 to 0xFF that will not be used for anything in your file except denoting a virtual eof marker.

    But why does this anyway?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    while (!eof())
          //do stuff

  4. #4
    bub
    Guest
    Code:
    ofstream outFile;
    ...
    outFile << EOF;
    perhaps?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Jamsan
    Code:
    while (!eof())
          //do stuff
    No. Also, that has nothing to do with outputing an EOF character.

    outFile << EOF;
    Read my reply. EOF is not valid in char format.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    havent got it yet

    Hello here id the code i tried

    #include <fstream.h>
    #include <stdio.h>




    void main()
    {

    ofstream SaveFile("test.txt");


    SaveFile <<"I should see this when i open the file with note pad, "<<EOF<< " I should not see this text";

    SaveFile.close();

    ifstream OpenFile("test.txt");


    char ch;

    while(!OpenFile.eof())

    {

    OpenFile.get(ch);

    cout << ch;


    }

    OpenFile.close();


    }

    It didnt work
    I guess the missing link is what to output instead of EOF
    i tried (-1,00Xff)
    I guess i just need to stuff a badbit some where but what is a bad bit and how to stuff it
    Thanks a lot for your time

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    hamada: You need to actually read what I've already posted.

    And read this too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Ok, here's how you do it, allthough it isn't portable. The EOF "character" in DOS (or windows for that matter) is Cntrl-Z, 0x1A or 26. For most Unix style machines, it's Cntrl-D, 0x04, or 4.
    If you run this on a windows machine, you will get the desired effect:

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
        ofstream out("test.txt");
    
        out << "Hello World!" << (char)26 << " This is EOF";
        out.close();
    
        ifstream in("test.txt");
    
        while (!in.eof())
        {
            char c;
            in.get(c);
    
            cout << c;
        }//while
        
        return 0;
    }//main
    This will output "Hello Word!". The file test.txt will have all the text we inserted (notepad.exe will show the EOF character as a block). You should note that this trick will only work on non-binary input streams (which is why notepad has no problem reading the whole file).

    The EOF constant in C/C++ is -1, which is why that don't work.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    As far as I know files can only have one EOF and it is put there automatically, you can't do it at will. You can design your code to stop writing to file or reading from file at will, that is at a given flag, which could be any valid symbol or group of symbols.

    for example, say you never will have a tilde in the useable data contained in a file. Then you can use a tilde to act as a flag.
    Code:
    ofstream fout("filename.ext");
    
    char data[] = "Save up to the tilde. ~ Not after.";
    
    for(int i = 0; i < strlen(data); ++i)
    {
      if(data[i] != '~')
        fout << data[i];
    }
    and the reverse if you have created a file in some other program and want to read it in to the current program up to a given flag.

    Code:
    ifstream fin("filename.ext")
    
    char input[100];
    char ch;
    int index = 0;
    
    fin >> ch;
    while(ch != '~' && fin)
    {
       input[index++] = ch;
       fin >> ch;
    }
    input[index] = '\0';
    there are other ways of accomplishing this using different types of flags, etc. , but this works. To my knowledge, however, you can not have more than one EOF per file and you can't manipulate where it is.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can have as many OS dependant EOFs as you want. This hack is usefull if you are trying to trick another program that processes text files (using a text, non-binary stream).
    If you are not trying to trick some existing program (that you don't have the source for) then you should do something along the lines of what elad suggested: use your own special char, which could be out of the printable range like (char)255.

    More code using OS dependant EOFs:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    const char REAL_EOF = 26; //for DOS, use 4 for Unix
    
    //dump contents of file 'fn'
    void dump_file(const char *fn);
    
    //replace the first occurence of an embedded EOF with the given char
    void replace_first_EOF(const char *fn,char replace);
    
    int main()
    {
        const char *fn = "test.txt";
    
        ofstream out(fn);
    
        out << "Hello World!" 
            << REAL_EOF << "Text after first EOF." 
            << REAL_EOF << "Text after second EOF."; 
        out.close();
    
        dump_file(fn);
        //output: "Hello World!"
    
        replace_first_EOF(fn,' ');
    
        dump_file(fn);
        //output: "Hello World! Text after first EOF"
    
        replace_first_EOF(fn,' ');
    
        dump_file(fn);
        //output: "Hello World! Text after first EOF. Text after second EOF."
    
        return 0;
    }//main
    
    void dump_file(const char *fn)
    {
        ifstream in(fn);
    
        if (!in)
            return;
    
        char c;
        while (in.get(c))
            cout << c;
    
        cout << endl;
    
        in.close();
    }//dump_stream
    
    void replace_first_EOF(const char *fn,char replace)
    {
        fstream in(fn,ios::in | ios::out | ios::binary);
    
        if (!in)
            return;
        
        char c;
        while (in >> c)
        {
            if (c == REAL_EOF)
            {
                //"rewind" file pointer and replace EOF
                in.seekp(-1,ios::cur);
                in.put(replace);
                in.close();
                return;
            }//if
        }//while
    
        in.close();
    }//replace_first_EOF
    gg

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Thanks for all of you

    Thanks guys for all your help. Although i was trying to force notepad to recognize the virtual eof but i think that is impossible and what have been done untill know is the maximum we can reach.
    Thanks for all your effort it have been helpfull

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, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03: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, 05:28 AM