Hi,
I've read a file, and stored its contents into a string array.
I need to change some numerical values, interpreted as characters by the compiler.
The file is of the format: ABCDE,EFGHIJ KLMNOPQRS,45.58867,122.59750
I've searched and studied but haven't got onto anything too comprehensive.
If someone can please tell me how to do this I would be very glad.
And I'm not allowed to use strtod; I think it's a C function and my program needs to be strictly C++ only.
Here is my code that I've developed so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
int main()
{
string usrFileStr,
fileStr = "airNames.txt", // declaring an obj string literal
sBuffer,
sLine = "";
istringstream iStrStrm;
int lineCount = 1;
int nStart;
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() )
{
nStart = -1 ;
cout << "First Str " << lineCount << " (";
for ( int x = nStart + 1; x < sBuffer.length(); x++ )
{
if ( sBuffer[ x ] == ',' )
{
nStart = x;
break;
}
cout << sBuffer[ x ];
}
cout << ") ";
for ( int x = nStart + 1; x < sBuffer.length(); x++ )
{
if ( sBuffer[ x ] == ',' )
{
nStart = x;
break;
}
cout << sBuffer[ x ];
}
cout << " (Second Str: "; // lattitude loop
for ( int x = nStart + 1; x < sBuffer.length(); x++ )
{
if ( sBuffer[ x ] == ',' )
{
nStart = x;
break;
}
cout << sBuffer[ x ];
}
cout << ", Third String: ";
for ( int x = nStart + 1; x < sBuffer.length(); x++ )
{
if ( sBuffer[ x ] == ',' )
{
nStart = x;
break;
}
cout << sBuffer[ x ];
}
cout << ") \n";
lineCount++;
}
cout << "There are a Total of: " << lineCount << " 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've tried to only read two digits after the decimal, but I only get one character. My first attempt was with static_cast but that was way off. And a istringstream variable won't let its argument be an array. I don't know what to do..