I need the user to input:
-the name of the file to read from
-name a file to be created to output the data
Ignoring the rest of the program, here is the part i'm having problems with:
#include %26lt;string%26gt;
int main()
{
// Filenames
string inFilename = "";
string outFilename = "";
// For file I/O
ifstream fin; //input file stream
ofstream fout; //output file stream
//User input of file name
cout%26lt;%26lt;"Enter the name of the input file: ";
getline(cin,inFilename);
//Works fine at this point, and opens the named file
//User input of file name
cout%26lt;%26lt;"Enter the new filename: ";
cin%26gt;%26gt;outFilename;
getline(cin,outFilename);
return 0;
}
When I compile I get no errors, but when I run the program, it will open the original file fine, but the output file won't pass the validity check, and won't even output the name of the file I had the user input in the error code as called for. What is the difference between the two!?!??!
C++ string help?
Actually, I don't see how that code is doing what you want. Try this;
std::string infilename;
std::string outfilename:
int main()
{
std::cout%26lt;%26lt;"please enter file to be opened"%26lt;%26lt;std::endl;
std::cin%26gt;%26gt;infilename;
fstream in(infilename.c_str(), ios::in);
if (in.good())
{
std::cout%26lt;%26lt;"file opening successful....."%26lt;%26lt;std::endl;
}
else if(in.bad())
{
std::cout%26lt;%26lt;" file opening failure....."%26lt;%26lt;std::endl;
return 0;
}
std::cout%26lt;%26lt;"please enter output filename"%26lt;%26lt;std::endl;
std::cin%26gt;%26gt;outfilename;
ofstream out(outfilename.c_str(), ios::out);
out%26lt;%26lt;"hi, I'm writing to this file!"%26lt;%26lt;std::endl;
in.close();
out.close();
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment