screenshot Hi guys I'm trying to read a text file that has 3 doubles in a row and then save these three values into my variables ( 1 variable per double).
So far my code is looking like this:
cout<<"ready to read file...:";
ifstream theFile("pose.txt");
double first,second,third;
while(theFile >> first >> second >> third){
cout<<"In while loop and got following values: ";
cout<< first<<endl<< second <<endl<< third;
}
And my input file looks like this when opened:
1.5 2.4 3.3
However even though the first cout runs and it tells me that the program is ready to read the file, it doesn't enter the while loop.
I've tried using other methods of reading the file which have also been ineffective.
Any help is welcome, Thanks.
I found the answer on another question on StackOverflow. This is the link to the answer: https://stackoverflow.com/a/23448835/4117002
Basically I had to specify in Xcode the path of the text file, even though I had already created it in the project.
Thanks for the help anyway everyone!
I don't recognize your IDE from the screenshot but if it is like other IDEs, your program will not by default be run in the directory containing your source file main.cpp
and your data file example.txt
. It will be run in a directory configured in the project settings, probably the one where the executable is created.
So let's pretend the (debug build) executable is created in Products/Debug
and is run there. In that case:
std::ifstream theFile("example.txt");
will fail because there is no file called example.txt
in Products/Debug
. You would need to open theFile
with the relative path:
std::ifstream theFile("../../InputFileExample/example.txt");
or else specify the absolute filename.
Assuming you are familiar with cpp:
Buffer each line of ifstream
data into a std::string
. Example:
std::getline(std::ifstream , std::string )
Buffer the std::string
into std::istringstream
. Example:
std::istringstream (std::string )
Split std::istringstream
into the double values you need. Example:
istringstream iss
iss >> first >> second >> third
Good Luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.