C++ 从制表符分隔的文件读取二维数据并存储在向量C++;

C++ 从制表符分隔的文件读取二维数据并存储在向量C++;,c++,vector,text-files,fstream,C++,Vector,Text Files,Fstream,我正在尝试读取以下格式的文本文件: 5 1.00 0.00 0.75 0.25 0.50 0.50 0.25 0.75 0.00 1.00 代码是: #include <iostream> #include <fstream> #include <vector> #include <string> int totalDataPoints; // this should be the first line of textfil

我正在尝试读取以下格式的文本文件:

5
1.00   0.00
0.75   0.25
0.50   0.50
0.25   0.75
0.00   1.00
代码是:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int totalDataPoints; // this should be the first line of textfile i.e. 5
std::vector<double> xCoord(0); //starts from 2nd line, first col
std::vector<double> yCoord(0); //starts from 2nd line, second col
double tmp1, tmp2;

int main(int argc, char **argv)
{
    std::fstream inFile;

    inFile.open("file.txt", std::ios::in);

    if (inFile.fail()) {
        std::cout << "Could not open file" << std::endl;
        return(0);
    } 

    int count = 0;

     while (!inFile.eof()) { 
         inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
         count++;
     }

     for (int i = 0; i < totalDataPoints; ++i) {
         std::cout << xCoord[i] << "    " << yCoord[i] << std::endl;
     }
    return 0;
}
#包括
#包括
#包括
#包括
int totalDataPoints;//这应该是文本文件的第一行,即5
std::向量xCoord(0)//从第二行第一列开始
std::向量yCoord(0)//从第二行第二列开始
双tmp1,tmp2;
int main(int argc,字符**argv)
{
std::fstream-infle;
open(“file.txt”,std::ios::in);
if(infle.fail()){
std::couttmp1;
xCoord.推回(tmp1);
填充>>tmp2;
yCoord.推回(tmp2);
计数++;
}
对于(int i=0;istd::cout
int totalDataPoints;
是一个全局变量,由于您没有使用值初始化它,因此它将被初始化为0。然后在for循环中

for (int i = 0; i < totalDataPoints; ++i) {
     std::cout << xCoord[i] << "    " << yCoord[i] << std::endl;
}
或者

totalDataPoints = count;
在for循环之前

我还建议您不要使用
而(!infle.eof())
来控制文件的读取。要修复它,您可以使用

 while (inFile >> tmp1 && inFile >> tmp2) { 
     xCoord.push_back(tmp1);
     yCoord.push_back(tmp2);
     count++;
 }

这将确保循环仅在有数据可读取时运行。有关更多信息,请参阅:

只需对代码进行简单更改。您不能在第一行中获取从
file.txt
提供的
totalDataPoints
。然后获取每一行,直到到达最后一行

int count = 0;

    inFile>>totalDataPoints;

     while (!inFile.eof()) {
         inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
         count++;
     }
通过for loop您可以这样做,这里不需要
int count=0

inFile>>totalDataPoints;

    for (int i=0; i<totalDataPoints; i++)
    {
        inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
    }
infle>>总数据点;
对于(int i=0;i>tmp1;
xCoord.推回(tmp1);
填充>>tmp2;
yCoord.推回(tmp2);
}

您从未将
totalDataPoints
设置为任何值。类似:请不要测试
if(stream.fail())
if(stream.eof())
,除非您知道自己在做什么。只需测试
if(stream)
。可能与您在调试器中单步执行此代码时发生的情况重复?这是您所期望的吗?是的,它可以工作。for totalDataPoints=xCoord.size();我从文本文件(即5)中删除了总分。现在我可以看到结果了。我如何才能为此创建一个类文件?@TheCoder类文件?那是什么?@TheCoder:它不是根据您的输入进行维护的,它接受file.txt文件中的cz第一行是totalDataPoints 5。根据此代码,您不能在循环被改进或转换时使用此文件因为我已经知道向量的大小,所以在这个特殊的情况下,我将其转换为for循环?@代码员:是的。当然。你现在可以更改这个答案。我只是根据你的代码进行更改。@代码员:如果你喜欢这个答案,你可以接受这个并投票给我。
inFile>>totalDataPoints;

    for (int i=0; i<totalDataPoints; i++)
    {
        inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
    }