Editing Text files from a program.

We have been set a task to make an encryption program.

My program must open and close text files whilst reading them and using the contents and encrypting them. I know how to encrypt them using ascii, but what my whole class dont understand, is the opening and closing of the file itself.

Any helpers?

(I have been using #include <fstream>
You include fstream so you allready know where to look.

C++ - Reference - Input/Output - fstream
http://www.cplusplus.com/reference/fstream/

There is basically everything you need to know.

ifstream = input file stream = read a file
ofstream = output file stream = write to file

The methods of each stream are discribed in the link above
Okay, so how do I display a text file, as i need to be able to store it into a string.
http://www.cplusplus.com/reference/istream/istream/seekg/

but you could do it like this too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    std::string strFile;

    std::ifstream myFile( "file.txt" );
    if (myFile.is_open())
    {
        
        std::string line;
        while ( std::getline(myFile, line) )
        {
            strFile += line + '\n';
        }
    }
    
    std::cout << strFile << std::endl;  
    myFile.close();
Topic archived. No new replies allowed.