当文件s2.txt
看起来像
ID 1 (string)
22 30 30 4 2 4 5 7 5 3 ......................................(a few lines
of numbers)
ID 2
30 4 2 1 2 ................................. (other lines of numbers)
我希望将数字保存到矢量但不起作用:
void readFromFile (){
ifstream file("s2.txt");
if( file.good() == true )
cout << "open" << endl;
else
cout << "denied" << endl;
int m=0;
while(!file.eof()) {
string ID;
int qual;
vector <int> quality;
getline(file,ID);
while (file>>qual) {
quality.push_back(qual);
cout<<quality[m]<<endl;
m++;
}
}
file.close();
}
}
main () {
readFromFile();
}
当我点击“运行”时,只有一串数字被保存到矢量(对于ID1)。
PS。阅读ID并不重要。
答案 0 :(得分:3)
您应该将每一行读成字符串。然后检查&#34; ID&#34;串。如果&#34; ID&#34;在一行的0位找到字符串然后将该行读作ID行,如果不是,则将该行读作整数行。
答案 1 :(得分:1)
正如Linh Dao的答案中所建议的,我做了相应的示例代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
void readFromFile(std::istream &in)
{
if (!in.good()) {
std::cerr << "ERROR!" << std::endl;
}
std::string buffer;
std::vector<int> quality;
while (std::getline(in, buffer)) {
if (buffer.size() >= 2 && buffer.compare(0, 2, "ID") == 0) {
std::cout << buffer << std::endl;
quality.clear(); // reset quality vector
} else {
// read numbers
std::istringstream in(buffer); int qual;
while (in >> qual) {
quality.push_back(qual);
std::cout << quality.back() << std::endl;
}
}
}
}
int main(void)
{
#if 0 // in OP
{ std::ifstream fIn("s2.txt");
readFromFile(fIn);
} // fIn goes out of scope -> file is closed
#else // instead
readFromFile(std::cin);
#endif // 0
return 0;
}
输入:
ID 1 (string)
22 30 30 4 2 4 5 7 5 3
22 30 30 4 2 4 5 7 5 3
ID 2
30 4 2 1 2
输出:
ID 1 (string)
22
30
30
4
2
4
5
7
5
3
22
30
30
4
2
4
5
7
5
3
ID 2
30
4
2
1
2
ideone上的生活演示。
注意:
逐行读取输入流(进入std::string buffer
)。
进一步处理取决于输入缓冲区内容是否以ID
开头。
如果没有,则buffer
与std::istringstream
一起用于提取int
个数字。
如果我理解正确的评论,提问者打算在每次迭代中输出整个收集的quality
向量。因此,我修改了第一个示例,并为operator<<()
添加了std::vector<int>
:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
// a stream operator for vector<int> ouput
std::ostream& operator<<(std::ostream &out, const std::vector<int> &values)
{
const char *sep = "";
for (int value : values) {
out << sep << value; sep = " ";
}
return out;
}
void readFromFile(std::istream &in)
{
if (!in.good()) {
std::cerr << "ERROR!" << std::endl;
}
std::string buffer;
std::vector<int> quality;
while (std::getline(in, buffer)) {
if (buffer.size() >= 2 && buffer.compare(0, 2, "ID") == 0) {
std::cout << buffer << std::endl;
quality.clear(); // reset quality vector
} else {
// read numbers
std::istringstream in(buffer); int qual;
while (in >> qual) {
quality.push_back(qual);
std::cout << quality << std::endl;
}
}
}
}
int main(void)
{
#if 0 // in OP
{ std::ifstream fIn("s2.txt");
readFromFile(fIn);
} // fIn goes out of scope -> file is closed
#else // instead
readFromFile(std::cin);
#endif // 0
return 0;
}
输入:与上面相同
输出:
ID 1 (string)
22
22 30
22 30 30
22 30 30 4
22 30 30 4 2
22 30 30 4 2 4
22 30 30 4 2 4 5
22 30 30 4 2 4 5 7
22 30 30 4 2 4 5 7 5
22 30 30 4 2 4 5 7 5 3
22 30 30 4 2 4 5 7 5 3 22
22 30 30 4 2 4 5 7 5 3 22 30
22 30 30 4 2 4 5 7 5 3 22 30 30
22 30 30 4 2 4 5 7 5 3 22 30 30 4
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7 5
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7 5 3
ID 2
30
30 4
30 4 2
30 4 2 1
30 4 2 1 2
ideone上的生活演示。