Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:以特定格式从文件中读取内容_C++ - Fatal编程技术网

C++ C++;:以特定格式从文件中读取内容

C++ C++;:以特定格式从文件中读取内容,c++,C++,我有一个包含以下格式像素坐标的文件: 234 324 126 345 264 345 我不知道我的文件里有多少对坐标 如何将它们读入向量文件?我是初学者,在C++中使用阅读功能。 我试过这个,但似乎不起作用: vector<Point> iP, iiP; ifstream pFile, rFile; pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt"); rFile.open("D:\\MATLAB\\WORKSPACE_MA

我有一个包含以下格式像素坐标的文件:

234 324
126 345
264 345
我不知道我的文件里有多少对坐标

如何将它们读入
向量
文件?我是初学者,在C++中使用阅读功能。 我试过这个,但似乎不起作用:

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");

string rBuffer, pBuffer;
Point rPoint, pPoint;

while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    sscanf(rBuffer.c_str(), "%d %d", rPoint.x, rPoint.y);
    sscanf(pBuffer.c_str(), "%d %d", pPoint.x, pPoint.y);

    iP.push_back(pPoint);
    iiP.push_back(rPoint);
}
向量iP,iiP;
ifstream pFile,rFile;
打开(“D:\\MATLAB\\WORKSPACE\u MATLAB\\pData.txt”);
rFile.open(“D:\\MATLAB\\WORKSPACE\u MATLAB\\rData.txt”);
字符串rBuffer,pBuffer;
点,点;
while(getline(pFile,pBuffer))
{
getline(rFile,rBuffer);
sscanf(rBuffer.c_str(),%d%d),rPoint.x,rPoint.y);
sscanf(pBuffer.c_str(),%d%d',pPoint.x,pPoint.y);
iP.推回(点);
iiP.推回(rPoint);
}

我收到一些奇怪的内存错误。我做错什么了吗?如何修复代码以使其能够运行?

一种方法是为
类定义自定义输入运算符(
运算符>
),然后使用
istream\u迭代器
读取元素。下面是一个示例程序来演示该概念:

#include <iostream>
#include <iterator>
#include <vector>

struct Point {
    int x, y;
};

template <typename T>
std::basic_istream<T>& operator>>(std::basic_istream<T>& is, Point& p) {
    return is >> p.x >> p.y;
}

int main() {
    std::vector<Point> points(std::istream_iterator<Point>(std::cin),
            std::istream_iterator<Point>());
    for (std::vector<Point>::const_iterator cur(points.begin()), end(points.end());
            cur != end; ++cur) {
        std::cout << "(" << cur->x << ", " << cur->y << ")\n";
    }
}
#包括
#包括
#包括
结构点{
int x,y;
};
模板
std::basic\U istream&operator>>(std::basic\U istream&is,Point&p){
回报率为>>p.x>>p.y;
}
int main(){
std::矢量点(std::istream_迭代器(std::cin),
std::istreamu迭代器();
对于(std::vector::const_迭代器cur(points.begin()),end(points.end());
cur!=结束;++cur){

std::cout一种方法是为
类定义一个自定义输入操作符(
操作符>
),然后使用
istream\u迭代器
读取元素。下面是一个示例程序来演示这个概念:

#include <iostream>
#include <iterator>
#include <vector>

struct Point {
    int x, y;
};

template <typename T>
std::basic_istream<T>& operator>>(std::basic_istream<T>& is, Point& p) {
    return is >> p.x >> p.y;
}

int main() {
    std::vector<Point> points(std::istream_iterator<Point>(std::cin),
            std::istream_iterator<Point>());
    for (std::vector<Point>::const_iterator cur(points.begin()), end(points.end());
            cur != end; ++cur) {
        std::cout << "(" << cur->x << ", " << cur->y << ")\n";
    }
}
#包括
#包括
#包括
结构点{
int x,y;
};
模板
std::basic\U istream&operator>>(std::basic\U istream&is,Point&p){
回报率为>>p.x>>p.y;
}
int main(){
std::矢量点(std::istream_迭代器(std::cin),
std::istreamu迭代器();
对于(std::vector::const_迭代器cur(points.begin()),end(points.end());
cur!=结束;++cur){

多亏了ChrisJester Young和enobayram,我成功地解决了我的问题。我添加了下面的代码

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");
stringstream ss (stringstream::in | stringstream::out);

string rBuffer, pBuffer;


while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    Point bufferRPoint, bufferPPoint;

    ss << pBuffer;
    ss >> bufferPPoint.x >> bufferPPoint.y;

    ss << rBuffer;
    ss >> bufferRPoint.x >> bufferRPoint.y;

    //sscanf(rBuffer.c_str(), "%i %i", bufferRPoint.x, bufferRPoint.y);
    //sscanf(pBuffer.c_str(), "%i %i", bufferPPoint.x, bufferPPoint.y);

    iP.push_back(bufferPPoint);
    iiP.push_back(bufferRPoint);
}
向量iP,iiP;
ifstream pFile,rFile;
打开(“D:\\MATLAB\\WORKSPACE\u MATLAB\\pData.txt”);
rFile.open(“D:\\MATLAB\\WORKSPACE\u MATLAB\\rData.txt”);
stringstream ss(stringstream::in | stringstream::out);
字符串rBuffer,pBuffer;
while(getline(pFile,pBuffer))
{
getline(rFile,rBuffer);
点缓冲点,点缓冲点;
ss>bufferPPoint.x>>bufferPPoint.y;
ss>bufferpoint.x>>bufferpoint.y;
//sscanf(rBuffer.c_str(),“%i%i”,bufferpoint.x,bufferpoint.y);
//sscanf(pBuffer.c_str(),“%i%i”,bufferPPoint.x,bufferPPoint.y);
iP.推回(缓冲点);
iiP.推回(缓冲点);
}

多亏了克里斯·杰斯特·杨(Chris Jester Young)和伊诺巴伊拉姆(enobayram),我成功地解决了我的问题。我在代码中添加了贝娄

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");
stringstream ss (stringstream::in | stringstream::out);

string rBuffer, pBuffer;


while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    Point bufferRPoint, bufferPPoint;

    ss << pBuffer;
    ss >> bufferPPoint.x >> bufferPPoint.y;

    ss << rBuffer;
    ss >> bufferRPoint.x >> bufferRPoint.y;

    //sscanf(rBuffer.c_str(), "%i %i", bufferRPoint.x, bufferRPoint.y);
    //sscanf(pBuffer.c_str(), "%i %i", bufferPPoint.x, bufferPPoint.y);

    iP.push_back(bufferPPoint);
    iiP.push_back(bufferRPoint);
}
向量iP,iiP;
ifstream pFile,rFile;
打开(“D:\\MATLAB\\WORKSPACE\u MATLAB\\pData.txt”);
rFile.open(“D:\\MATLAB\\WORKSPACE\u MATLAB\\rData.txt”);
stringstream ss(stringstream::in | stringstream::out);
字符串rBuffer,pBuffer;
while(getline(pFile,pBuffer))
{
getline(rFile,rBuffer);
点缓冲点,点缓冲点;
ss>bufferPPoint.x>>bufferPPoint.y;
ss>bufferpoint.x>>bufferpoint.y;
//sscanf(rBuffer.c_str(),“%i%i”,bufferpoint.x,bufferpoint.y);
//sscanf(pBuffer.c_str(),“%i%i”,bufferPPoint.x,bufferPPoint.y);
iP.推回(缓冲点);
iiP.推回(缓冲点);
}

<代码>不要在C++中使用Prtff/ScNf,它们不是,使用I/O流(%的时间99)。我在
sscanf
接收到错误。我如何从字符串中读取数据,以便获得相应的
x
y
值?就像@enobayram所说的,不要使用
sscanf
。使用我回答中提到的iostreams方法。这对你的理智有好处。但是如果你真的坚持使用
> SCANFF ,你需要说“代码>和rPo.x/CODE”,以及<代码>和rPo.y < /代码>(和同上,代码为< > pPo.x>代码>和<>代码> pPo.y>代码>,不要在C++中使用Prtff/ScNf,它们不使用I/O流(%的99的时间)。我在
sscanf
接收到错误。我如何从字符串中读取数据,以便获得相应的
x
y
值?就像@enobayram所说的,不要使用
sscanf
。使用我回答中提到的iostreams方法。这对你的理智有好处。但是如果你真的坚持使用
>sscanf
,您需要说
&rPoint.x
&rPoint.y
(与
&pPoint.x
&pPoint.y
相同)。