在這段代碼中,我不知道為什么但if (temp == '\n')不起作用,因此在輸出中全為零并且第 i 個索引中的零不會更新
while(fin.eof() != 1)
{
if(temp == '\n' )
{
k = 0;
j = 0;
i ;
cout << "call from 2nd if";
}
if(temp == ',')
{
k = 0;
j ;
cout << "call from 1st if";
}
fin >> temp;
data[i][j][k] = temp;
cout << "address " << i << j << k << " : " << data[i][j][k] << endl;
k ;
i,j;
}
輸出:
address at **0**31 : u
address at **0**32 : i
address at **0**33 : c
address at **0**34 : e
address at **0**35 : B
.
.
.
基本上它是第 i 個值沒有更新的 3 維陣列,有什么解決方案
uj5u.com熱心網友回復:
if(temp == '\n' )替換為if(isspace(temp)).
uj5u.com熱心網友回復:
在 C 中回圈 eof 是一個非常糟糕的主意。如果您的檔案為空,fin.eof()則在您嘗試從中讀取某些內容之前將是錯誤的。
因此,作為第一個糾正措施更改為:
while(fin >> temp)
{
....
}
然后我們假設temp定義為char,因為您逐個讀取字符。
問題是它>>往往會吞下很多空格,包括你永遠不會得到的 ' ' 和 '\n' 。如果您確實希望得到一些白色,則需要設定std::noskipws:
while(fin >> noskipws >> temp)
{
....
}
但是,如果您正在逐個字符地閱讀,更好的方法可能是閱讀 fin.get()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/325813.html
