我想獲取一個連續的行,用「&」分隔的值加載到多維數組中。這就是我想要做的事情 - 除了包含我的分隔值的字符串「str」,其格式爲「value1,value2,value3等...」之外,所有內容都會在代碼中檢出,只是將整個字符串加載到陣列[0] [0]。我知道有更好的方法可以做到這一點,但我想知道的是爲什麼C++不會將「str」視爲鍵入了單個值和硬編碼「數組」。C++從外部文件的多維數組問題
下面是代碼:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
string str, strTotal;
ifstream in;
in.open("Desktop/01_001.PAC");
getline(in,str);
while (in) {
strTotal += str;
getline(in,str);
}
string searchString("&");
string replaceString(", ");
assert(searchString != replaceString);
string::size_type pos = 0;
while ((pos = str.find(searchString, pos)) != string::npos) {
str.replace(pos, searchString.size(), replaceString);
pos++;
}
string array[4][5] = {str};
cout << array[0][0];
return(0);
}
這裏是外部文件("Desktop/01_001.PAC"
):
void&void&void&void&a&a1&a2&a3&b&b1&b2&b3&c&c1&c2&c3&d&d1&d2&d3
提前感謝!
如果你剛開始學習程序(這聽起來像你是),那麼C++是誠實的你真的可以開始用最糟糕的語言。 – 2011-01-29 23:04:00
我主要用PHP,Javascript等網絡語言編程,昨天剛剛啓動C++。我試圖將PHP的想法應用到C++,它一直是一個活生生的地獄,哈哈...... – tech0234 2011-01-29 23:21:22