Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++_String_File Io_Format - Fatal编程技术网

C++ 如何在C+中读取文本文件时跳过特定列+;?

C++ 如何在C+中读取文本文件时跳过特定列+;?,c++,string,file-io,format,C++,String,File Io,Format,我有一个三列的文本文件。我只想读第一个和第三个。第二列由名称或日期组成 输入文件|数据读取 7.1 2000-01-01 3.4 | 7.1 3.4 1.2 2000-01-02 2.5 | 1.2 2.5 或 5.5未知3.9 | 5.5 3.9 1.1未知2.4 | 1.1 2.4 在C++中,有人能给我一个提示吗?< /P> 谢谢 谁能给我一个提示如何在C++中实现这一点 只需使用将跳过的数据放入虚拟变量,或使用跳过输入,直到指定下一个字段分隔符 最好的解决方法是逐行阅读(参见)使用 ST

我有一个三列的文本文件。我只想读第一个和第三个。第二列由名称或日期组成

输入文件|数据读取

7.1 2000-01-01 3.4 | 7.1 3.4

1.2 2000-01-02 2.5 | 1.2 2.5

5.5未知3.9 | 5.5 3.9

1.1未知2.4 | 1.1 2.4

在C++中,有人能给我一个提示吗?< /P> 谢谢

谁能给我一个提示如何在C++中实现这一点

只需使用将跳过的数据放入虚拟变量,或使用跳过输入,直到指定下一个字段分隔符

最好的解决方法是逐行阅读(参见)使用<代码> STD::IFSturis/Cuff>,然后逐行解析(并跳过列),使用<代码> STD::ISTIGRUNSWATE < /CUT>在输入文件中的所有行上。

有人能给我提示一下C++中如何做到这一点吗? 当然可以:

  • 使用
    std::getline
    逐行浏览您的文件,将每一行读入
    std::string行
  • 为每行构造一个临时
    std::istringstream
    对象
  • 在此流上使用
    >
    运算符填充类型为
    double
    (第1列)的变量
  • 再次使用
    >
    将第二列读入您实际上不会使用的
    std::string
  • 使用
    >
    读取另一个
    双精度
    (第3列)
  • i、 e.类似于:

    std::ifstream file;
    ...
    std::string line;
    while (std::getline(file, line)) {
        if (line.empty()) continue;     // skips empty lines
        std::istringstream is(line);    // construct temporary istringstream
        double col1, col3;
        std::string col2;
        if (is >> col1 >> col2 >> col3) {
            std::cout << "column 1: " << col1 << " column 3: " << col3 << std::endl;
        }
        else {
            std::cout << "This line didn't meet the expected format." << std::endl;
        }
    }
    
    std::ifstream文件;
    ...
    std::字符串行;
    while(std::getline(文件,行)){
    如果(line.empty())继续;//跳过空行
    std::istringstream是(行);//构造临时istringstream
    双col1,col3;
    std::字符串col2;
    如果(是>>col1>>col2>>col3){
    
    std::cout问题的解决方法如下:

    int main()
    {   
    ifstream file("lixo2.txt");
    string line; int nl=0; int nc = 0; double temp=0;
    
    vector<vector<double> > matrix;
    
    while (getline(file, line))
    {
    size_t found = line.find("Unknown");
    line.erase (found, 7);
    istringstream is(line);
    
    vector<double> myvector;
    
    while(is >> temp)
    {
        myvector.push_back(temp);
        nc = nc+1;
    }
    matrix.push_back(myvector);
    
     nl =nl+1;
    }
    
    return 0;
    }
    
    intmain()
    {   
    ifstream文件(“lixo2.txt”);
    字符串行;int nl=0;int nc=0;双温度=0;
    向量矩阵;
    while(getline(文件,行))
    {
    size_t found=line.find(“未知”);
    行。删除(找到,7);
    istringstream是(行);
    向量myvector;
    while(is>>temp)
    {
    myvector.push_back(温度);
    nc=nc+1;
    }
    矩阵。推回(myvector);
    nl=nl+1;
    }
    返回0;
    }
    

    谢谢大家!!

    LihO,谢谢你的帮助。不过,当我有一个包含多个列的文件时,我如何阅读它们呢?但总是跳过第二列。