I need a getline() equivalant for all lines

My first program i decided to make an encryptor using character casting into an int and then using a rolling code to change the value and then cast back into the new charater.

I gradually improved this so it can now read a file using getline() and re-write the modified version, however this only works for single lines in the file.

Because i want it to re-write to file it'll encrypt the first line but delete the rest of the file only leaving the modified first line.




Is there an equivalant to the getline() that get the whole file for me INCLUDING white-spaces and newlines...
Or would somebody be able to post a way to iterate through the lines saving each to a 2dimentional vector or something...

Please help, Thanks
You can use read() to read whole file or combine while loop and operator>> .

http://www.cplusplus.com/reference/iostream/istream/read/
Last edited on
Sounds like you are reading a line and then encrypting it then writing that line back to file? If this is the case, instead, read the entire file, line by line, use a vector and push_back each line of the file until you have read it's contents. Then encrypt each element of the vector (which contain 1 line of your file each). Then write the entire vector to the file, line by line.
> Is there an equivalant to the getline() that get the whole file for me INCLUDING white-spaces and newlines...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <string>
#include <iterator>

int main()
{
    // for encryption, open in binary mode
    std::ifstream input_file( "input_file_name", std::ios::binary ) ;

    // read the entire contents of the file into a string
    std::string contents( std::istreambuf_iterator<char>(input_file),
                          ( std::istreambuf_iterator<char>() ) ) ;

    // encrypt the contents
    // ...

    // and write it out
    std::ofstream output_file( "output_file_name", std::ios::binary ) ;
    output_file << contents ;
}
Sort of. Use a #include<sstream> header, and declare a stringstream object. In fact, an example code would be best.

1
2
3
4
5
6
7
8
9
#include <fstream>
#include <sstream>

int main()
{
    std::stringstream foo;
    std::ifstream bar (/*Insert path to file here*/);
    foo << bar.rdbuf();
}


Now foo.str() is a ready-to-use string equal to the full contents of the file.
Last edited on
Thank you to codekiddy for that read() function and the link, this looks like it could work quite work and is also nice and simple.

clanmjc who mentioned using vectors...
i'd already tried using this along with an iterator that can select what line to read but unfortunately i couldn't figure out how to detect when this is on the last line/if it needs to read the next line.

And thanks o anyone else who's contributed but i think for now i'm going to go for the read() function mentioned before along with the file size detection using the seekg() and tellg() mainly because i already know a little about these functions and how to use them, i just didn't think of this method.
Last edited on
Topic archived. No new replies allowed.