// print the content of a text file.
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
string test = "save";
if(test == "load") {
ifstream infile;
infile.open ("test.txt", ifstream::in);
while (infile.good()){
cout << (char) infile.get() << endl;
}
infile.close();
}
if (test == "save") {
char * buffer;
long size;
ifstream infile ("test.txt",ifstream::binary);
ofstream outfile ("new.txt",ofstream::binary);
// get size of file
infile.seekg(0,ifstream::end);
size=infile.tellg();
infile.seekg(0);
// allocate memory for file content
buffer = newchar [size];
// read content of infile
infile.read (buffer,size);
// write to outfile
outfile.write (buffer,size);
// release dynamically-allocated memory
delete[] buffer;
outfile.close();
infile.close();
}
return 0;
}
The problem is that the saving portion involves 2 files and the data is copied from the first to the second. I want the data to be from a string the user types in and have that be saved to the new file. I changed the save part to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if (test == "save") {
string buffer;
long size;
cin >> buffer;
size = buffer.length();
ofstream outfile ("test.txt",ofstream::binary);
// write to outfile
outfile.write (buffer,size);
outfile.close();
}
I get an error on the outfile.write(buffer,size); line.
Error:
38 C:\Documents and Settings\programming\Desktop\Science Fair\C++\MEDS\test.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::write(std::string&, long int&)'