我讀的文本文件格式:字符串流回路發生故障
哎呀,一些文字,45.4321,54.22134
我只是我的雙看重的存儲在一個字符串變量。
爲什麼它只給了我字符串的第一個數字?
如果我重新開始只有一個while循環和這種新格式的文本文件:
21.34564
它的作品,因爲它應該。
事情是,sLine
具有與我重新開始時的價值相同的價值。最有可能導致問題的三個嵌套for循環不同。
這裏是讓我的代碼是什麼我想:
#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;
}
所以有它的工作原理就像它應該。但我不能讓它的內部工作for循環,或當我在我的文本文件中的多個精密組件...
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;
}
我沒有任何其他的字符在第一代碼迴路過去,因爲我只是閱讀一個不錯的小雙重價值。
在我的第二個代碼中,我有很多字符可以在我想要的之前到達。但不管怎樣,它仍然與其他角色隔絕,並且仍然處於可變狀態。即時通訊生病了解問題所在:/雖然我認爲它是for循環。
我也試過atof,但我得到'0'小數點應該是。 和strtod很難,因爲我需要即時通訊沒有讀取數據到一個常量字符* cPtr