0
我試圖從文本文件中的數據讀取到全局2D向量「矩陣 該文件的內容將其存儲在2D矢量,如:從文本文件中讀取數據,並使用C++語言
8 ,3
1,6,2
9,2,5
1,5,25
7,4, 25
我找不出什麼是我的錯誤。我的代碼存儲在第一行。
#include <iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include <sstream>
#define EXIT_FILE_ERROR (1)
#define EXIT_UNEXPECTED_EOF (2)
#define EXIT_INVALID_FIRSTLINE (3)
#define MAXLINE (10000)
std::vector< std::vector<int> > matrix;
int main(int argc, const char * argv[])
{
FILE *fp;
std::string sFileName = "Matrix1.txt";
std::ifstream fileStream(sFileName);
if (!fileStream.is_open())
{
std::cout << "Exiting unable to open file" << std::endl;
exit(EXIT_FILE_ERROR);
}
std::string line;
while (getline (fileStream,line))
{
std::stringstream ss(line);
std::vector<int> numbers;
std::string v;
int value;
while(ss >> value)
{
numbers.push_back(value);
std::cout << value << std::endl;
}
matrix.push_back(numbers);
}
fileStream.close();
if ((fp = fopen(sFileName.c_str(), "r")) == NULL)
{
std::cout << "Exiting unable to open file" << std::endl;
exit(EXIT_FILE_ERROR);
}
return 0;
}
有人能告訴我我的錯誤是什麼?
可能這些逗號混淆了你的解析。 –
除此之外,你爲什麼混合FILE *和ifstream?順便說一句,'std :: string v;'是未使用的。另外,請始終分享您的輸出內容以及您希望看到的實際內容。 – lpapp
Noooooo !!!!請不要另一個文件解析問題。請在StackOverflow中搜索「C++讀文件分析2d」。 –