-
file I/O problem
Hey, I'm helping my friend with this program. Two problems:
1. Is there an escape character in C++ to ignore a \ in a path?
for example in Perl I would just use another \ as in C:\\Progra~1\
2. I need to copy all contents from one file and output to another, but the .get and .put aren't working. Is there a better way to do this or does the syntax need to be corrected?
Code:
#include <iostream> //i/o library
#include <string> // string library
#include <fstream> // both input/output files
#include <cstdlib>
using namespace std;
int main() // starting main function
{
ifstream myinputfile; // declaring input file
ofstream myoutfile; // declaring output file
cout << "Begin file Copy\n"; // print to screen
myinputfile.open ("C:|myinputfile.txt"); //opening input file
if (!myinputfile) // testing success
{
cout << "Error!!\n"; // if unsuccessful
}
myoutfile.open ("C:|myoutfile.txt"); //opening output file
if (!myoutfile) //testing success
{
cout << "Error!!\n"; // if unsucessful
exit (100);
}
int line; // declaring a variable
while (myinputfile.get (line)) {
myoutfile.put (line);
}
myinputfile.close (); // closing input file
myoutfile.close (); // closing output file
cout << "End file copy\n"; // print to screen
exit (102);
return 0; // end of main function
}
Thanks for your help :)
-
-
Oh it is...thanks ::embarrassed::
So, what's the best way to parse the input file and output each line to the output file?
-
-
-
Your variable 'line' is misnamed, but your old code is a better way to copy a text file isn't it
-
My old code didn't compile...
I wasn't familiar with get and put, so I asked what's the best way to copy the text file...
With getline() I have gotten farther:
Code:
int MAX_LENGTH = 100;
char line[MAX_LENGTH];
while ( myinputfile.getline(line, MAX_LENGTH )) {
cout << "read line: " << line << endl;
myoutfile.put(line, MAX_LENGTH);
}
but this line:
Code:
myoutfile.put(line, MAX_LENGTH);
doesn't work. I can now get the text from the one file, but what is the best syntax to throw it in the other?
-
char c; while(myinputfile.get(c)) myoutfile.put(c);
-
Perfect ::embarrassed again::
That's what I had before, but I forgot to change the variable to parse for from int to char... (no wonder)
Thanks :)