I am reading a text file with this format:
grrr,some text,45.4321,54.22134
I just have my double valued stored in a string variable.
Why is it only giving me the first digit of the string?
If I start over with just one while loop and a text file of this new format:
21.34564
it works as it should.
The thing is, sLine has the the same value as the one when I started over. What is different is the three nested for loops that most likely is causing the problem.
Here is the code that gets me what I want:
#include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> #include <cstdlib> #include <sstream> using namespace std; int main() { string usrFileStr, fileStr = 'test.txt', // declaring an obj string literal sBuffer, sLine, str; double dValue ; int lineCount = 1; int nStart; istringstream issm; fstream inFile; // declaring a fstream obj // cout is the name of the output stream cout << 'Enter a file: '; cin >> usrFileStr; inFile.open( usrFileStr.c_str(), ios::in ); // at this point the file is open and we may parse the contents of it while ( getline ( inFile, sBuffer ) && inFile.eof() ) { cout << 'Original String From File: ' << sBuffer << endl; cout << 'Modified Str from File: ' << fixed << setprecision(2) << dValue << endl; } fgetc( stdin ); return 0; }
So there it works just like it should. But i cant get it to work inside a for loop or when i have multiple feilds in my text file…
With this code, why is it taken off the decimal? #include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> #include <cstdlib> #include <sstream> #include <errno.h> using namespace std; int main() { string usrFileStr, myFileStr = 'myFile.txt', // declaring an obj string literal sBuffer, sLine = ''; istringstream inStream; int lineCount = 1; int nStart; double dValue = 0, dValue2 = 0; float fvalue; fstream inFile; // declaring a fstream obj // cout is the name of the output stream cout << 'Enter a file: '; cin >> usrFileStr; inFile.open( usrFileStr.c_str(), ios::in ); // at this point the file is open and we may parse the contents of it if ( !inFile ) { cout << 'Not Correct ' << endl; } while ( getline ( inFile, sBuffer ) ) { nStart = -1 ; for ( int x = nStart + 1; x < sBuffer.length(); x++ ) { if ( sBuffer[ x ] == ',' ) { nStart = x; break; } cout << sBuffer[ x ]; } for ( int x = nStart + 1; x < sBuffer.length(); x++ ) { if ( sBuffer[ x ] == ',' ) { nStart = x; break; } cout << sBuffer[ x ]; } for ( int x = nStart + 1; x < sBuffer.length(); x++ ) { if ( sBuffer[ x ] == ',' ) { nStart = x; break; } sLine = sBuffer[ x ]; inStream.clear(); inStream.str( sLine ); if ( inStream >> dValue ) cout << setprecision(1) << dValue; } for ( int x = nStart + 1; x < sBuffer.length(); x++ ) { if ( sBuffer[ x ] == ',' ) { nStart = x; break; } sLine = sBuffer[ x ]; inStream.clear(); inStream.str( sLine ); if ( inStream >> dValue2 ) cout << setprecision(1) << dValue2; } cout << ') \n'; lineCount++; } cout << 'There are a Total of: ' << lineCount -1 << ' line(s) in the file.' << endl; inFile.clear(); // clear the file of any errors inFile.close(); // at this point we are done with the file and may close it fgetc( stdin ); return 0; }
I don’t have any other characters to loop over in the first code because im just reading a nice little double value.
In my second code, i have many characters to get to before the one that i want. But regardless, it is still isolated from the other characters and it is still in its own varaible. im to sick to realize what the problem is :/ although i think its the for loops.
I have also tried atof but i get a ‘0’ where the decimal should be. and strtod is hard because i need im not reading data into a const char *cPtr
Using instream>>dvalue is certainly the right way to do things. But sometimes what’s right isn’t always easiest or necessarily best.
We could do something like this:
Or perhaps something like this, while less efficient, might be easier to understand…
Assuming: