String operations
-
I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<
-
I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<
Cpudood wrote: inData.open("data.dat"); Use the full path to the file. Without a path, the OS looks in the current directory, whatever that happens to be, and the file evidently isn't in that directory. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER
-
Cpudood wrote: inData.open("data.dat"); Use the full path to the file. Without a path, the OS looks in the current directory, whatever that happens to be, and the file evidently isn't in that directory. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER
-
I have inlcuded the data file as a part of the project workspace. I don't know if that is the current directory or not but I wouldn't know how to specify its location in the actual project.
If the data file is in the same folder as the app, then use GetModuleFileName() to get the app's path, lob off the app's name, and add the data file's name.
[
](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!
-
I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<
Cpudood wrote: getline(cin, line1); shouldn't that be
getline(inData, line1);
if you are reading in from the file?
cin
is reading in from the console.
[
](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!
-
I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<
2 things I can see: 1) Did you remap cin to your input stream somewhere in code that you didn't include? If not, your file could be open, but you aren't trying to read from it. 2) Where is your input file? if it is sitting in your project directory your code won't see it. You need to put the file in the Debug and Release directories - which is where your executable file is running from.