繁体   English   中英

C ++:可能的Xcode(Mac)问题。 无法使用getline()从外部文件中读取数字

[英]C++: possible Xcode (Mac) problem. Can't read in numbers from external file using getline()

我是Mac Xcode(3.2.2)用户,使用GCC 4.2编译器,对C ++经验不足,对编译器一无所知。

我需要从外部文件中读取数字,并将其存储在整数向量中。 我使用流和getline()编写了代码。 我可以编译代码,并且运行时不会出错,但是没有输出。 不过,它适用于朋友(非Mac和非GCC 4.2编译器)。

我进行了一些谷歌搜索,发现了几篇有关Xcode 3.2.1中可能的编译器(gcc,Apple)问题的帖子,这些问题可能与我的问题有关(例如,此问题:使用getline()的C ++打印:释放的指针未分配在XCode。但是,我有Xcode 3.2.2,我尝试了建议的修复程序(与在_GLIBCXX_FULLY_DYNAMIC_STRING中添加_GLIBCXX_FULLY_DYNAMIC_STRING到(1)目标设置窗口或(2)作为预处理器宏之前)有关,但是我的问题仍然没有解决。

所以我现在不知道我的问题是什么-是否与人们在Xcode 3.2.1中遇到的相同的编译器问题,但是在3.2.2中需要不同的修复程序。 或者我的代码中是否还有其他问题。

如果有人可以提供一些建议,那就太好了。 没有建议太简单了。 如果还有另一种从外部文件导入数字的方法,我很想知道。 此外,有人知道是否有可能从.dat文件而不是Mac上的.txt文件导入数据吗?

谢谢。

自从第一次发布以来,我已经包括了wilhelmtell的编辑。

目标:将文本文件中的数字读入称为“结果”的整数向量。

1)数据文件test.dat中的数据最初看起来像,

//测试

测试

'1''2''3'

//文本文件中还有一些标签和注释,这些标签和注释是我不想读的,但无法从文件中删除。

#include <algorithm>
#include <cctype>
#include <iterator>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

// Edited: added struct (wilhelmtell's advice)
struct not_digit_and_not_whitespace {
    bool operator()(char c) const {
        return ! std::isdigit(c) && ! std::isspace(c);
    }
};

int main()
{
    system("pwd");

    std::string line;
    const std::string fileName = "/Users/me/test.dat";  

    ifstream theStream( fileName.c_str() ); 

    if( ! theStream )
          std::cerr << "Error opening file test.dat\n";

    std::getline( theStream, line );
    // line.erase(remove( line.begin(), line.end(), '\'' ), line.end() ); // Edited (wilhelmtell's advice)
    line.erase(remove_if( line.begin(), line.end(),not_digit_and_not_whitespace()), line.end() );  // Edited: added (wilhelmtell's advice)

    std::istringstream myStream( line );
    std::vector<int> numbers((std::istream_iterator<int>(myStream)), std::istream_iterator<int>());

    std::copy(numbers.begin(), numbers.end(),
              std::ostream_iterator<int>(std::cout, "\n"));

    /* int temp;  // Edited: wilhemtell suggested using iterators instead.
    while ( myStream >> temp ){
        numbers.push_back( temp );
     std::cout << temp << std::endl;
    }*/
    return 0;
}

这可能对您有用。 它在尝试时使用getline,但使用'\\'字符作为行定界符。 忽略一切,直到第一个'。 假设所有内容,直到下一个'是数字的一部分。 继续直到逻辑失败(可能是由于文件结尾)。

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

int main()
{
    system("pwd");

    std::string line;
    const std::string fileName = "test.dat";  

    std::ifstream theStream( fileName.c_str() ); 

    if( ! theStream )
          std::cerr << "Error opening file test.dat\n";

    std::vector<int> numbers;

    while (true)
    {
        // get first '
        std::getline(theStream, line, '\'');
        // read until second '
        std::getline(theStream, line, '\'');
        std::istringstream myStream( line );
        int i;
        myStream >> i;
        if (myStream.fail())
            break;
        numbers.push_back(i);
    }
    std::copy(numbers.begin(), numbers.end(),
              std::ostream_iterator<int>(std::cout, "\n"));
}

我的猜测是您的test.dat文件不在工作目录中。 要验证您可以测试流,请执行以下操作:

if( ! theStream )
    std::cerr << "Error opening file test.dat\n";

在XCode中检查项目属性,以查看工作目录是什么,然后将文件放在此处。

如果文件中包含的数据不能解析为整数,则流的int解析将失败。 确保您读取的行除整数和空格外没有其他内容:

#include <cctype>  // algorithm iterator ...

struct not_digit_and_not_whitespace {
    bool operator()(char c) const {
        return ! std::isdigit(c) && ! std::isspace(c);
    }
};

// ...
line.erase(remove_if( line.begin(), line.end(),
                     not_digit_and_not_whitespace()),
           line.end() );

另外,您可以简化代码。 首先,如果包含<iostream>则不需要<istream> 其次,可以使用std::vector的范围构造函数获取整数。

#include <algorithm>  // iterator fstream iostream vector string sstream

int main()
{
    std::string line;
    std::ifstream theStream("test.dat");
    std::getline(theStream, line);
    line.erase(std::remove(line.begin(), line.end(), '\'' ),
               line.end());
    std::istringstream myStream(line);
    std::vector<int> numbers((std::istream_iterator<int>(myStream)),
                             std::istream_iterator<int>());
    std::copy(numbers.begin(), numbers.end(),
              std::ostream_iterator<int>(std::cout, "\n"));
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM