Я прочитал файл и сохранил его содержимое в строковом массиве.
Мне нужно изменить некоторые числовые значения, интерпретируемые компилятором как символы.
Файл имеет формат: ABCDE, EFGHIJ KLMNOPQRS, 45.58867,122.59750
Я искал и учился, но не нашел ничего слишком всеобъемлющего.
Если кто-нибудь может подсказать, как это сделать, я был бы очень рад.
И мне не разрешено использовать strtod; Я думаю, что это функция C, и моя программа должна быть строго C ++.
Вот мой код, который я разработал до сих пор:
#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;
}
Я пытался прочитать только две цифры после десятичной точки, но я получил только один символ.
Моя первая попытка была с static_cast, но это было далеко.
И переменная istringstream не допустит, чтобы ее аргумент был массивом. Я не знаю, что делать ..