C ++:可能的Xcode(Mac)问题.无法使用getline()从外部文件中读取数字 [英] C++: possible Xcode (Mac) problem. Can't read in numbers from external file using getline()

查看:161
本文介绍了C ++:可能的Xcode(Mac)问题.无法使用getline()从外部文件中读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a Mac Xcode (3.2.2) user, working with the GCC 4.2 compiler, fairly inexperienced with C++ and completely ignorant about compilers.

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

I need to read in numbers from an external file and store them in a vector of ints. I wrote code using streams and getline(). I can compile the code and it runs without errors, but there is no output. It works for a friend (non-Mac and non-GCC 4.2 compiler) though.

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

I did some googling and found several posts about a possible compiler (gcc, Apple) issue in Xcode 3.2.1 that might relate to my problem (this one, for example: C++ using getline() prints: pointer being freed was not allocated in XCode. However, I have Xcode 3.2.2. I tried the suggested fixes (related to adding _GLIBCXX_FULLY_DYNAMIC_STRING to (1) the target settings window or (2) as preprocessor macros before the includes), but my problem is still not solved.

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

So I don't know what my problem is at this point--whether it is the same compiler issue that people had in Xcode 3.2.1 but a different fix is required in 3.2.2. Or if there is some other problem in my code.

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

It would be great if someone could offer some advice. No advice is too simple. If there is another way to import numbers from an external file, I would love to know about it. Also, does anyone know if there would be any reason it would be possible to import data from a .dat file, but not a .txt file on a Mac?

谢谢.

自从首次发布以来,我就包含了威廉姆德尔的编辑内容.

Since first posting, I have included wilhelmtell's edits.

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

Goal: Read numbers from a text file into a vector of ints called "results."

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

1) The data in the data file test.dat initially looked like,

//测试

测试

'1''2''3'

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

// There are also labels and comments in the text file that I don't want to read in but can't delete from the file.

#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,但使用'\'字符作为行定界符.忽略一切,直到第一个'.假定所有内容,直到下一个'是数字的一部分.继续直到逻辑失败(大概是由于文件结尾).

This might work for you. It uses getline as you were trying, but uses the '\'' character to serve as the line delimiter. Ignore everything up to the first '. Assume everything until the next ' is part of a number. Continue until the logic fails (presumably due to end of file).

#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"));
}

这篇关于C ++:可能的Xcode(Mac)问题.无法使用getline()从外部文件中读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆