用C ++打开2个数据文件

时间:2013-08-08 10:43:31

标签: c++ macos file gcc file-io

我根据从输入文件中读取的数据编写了一个程序来创建gnuplot命令。我手动执行此操作需要太多命令。这非常好用,但我现在需要从两个不同的数据文件中读取数据并使用它们来创建gnuplot命令。不幸的是,阅读这两个文件似乎出现了问题。从数据文件中读取所涉及的代码部分如下:

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int n_snapshots;

int main () {

    std::cout << "Enter number of snapshots" << "\n";
    std::cin >> n_snapshots;

    int snap_cell_count[n_snapshots];

    std::ifstream in_file_count("data/snapshot_data");
    std::ifstream in_file_fates("data/cell_fates_data_final");

    for (int i=0; i<n_snapshots; i++) {
        in_file_count >> snap_cell_count[i];
    }
    in_file_count.close();

    int cell_fates[snap_cell_count[n_snapshots-1]];
    for (int i=0; i<snap_cell_count[n_snapshots-1]; i++) {
        in_file_fates >> cell_fates[i];
    }
    in_file_fates.close(); 

n_snapshots只是一些整数,snap_cell_count []是一个带有“n_shapshots”元素的数组,每个元素都从数据文件“snapshot_data”中读取一个值。 cell_fates []是一个数组,其中的元素数等于snap_cell_count []中最后一个元素的值,并且从文件中读取其元素的值,在本例中为“cell_fates_data_final”。要读取的数据文件存储在名为“data”的文件夹中。

不幸的是,编译器返回以下错误。

Undefined symbols for architecture x86_64:
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
  _main in ccrpSAv5.o
  "std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::close()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::close()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()", referenced from:
  _main in ccrpSAv5.o
  "std::ios_base::Init::Init()", referenced from:
  __static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
  "std::ios_base::Init::~Init()", referenced from:
  __static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
  "std::cin", referenced from:
  _main in ccrpSAv5.o
  "std::cout", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
  _main in ccrpSAv5.o
  "___gxx_personality_v0", referenced from:
  Dwarf Exception Unwind Info (__eh_frame) in ccrpSAv5.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

这是使用Mac OSX Mountain Lion上的gcc编译器编译的。

有没有人知道这里有什么问题?

1 个答案:

答案 0 :(得分:2)

嗯,对于初学者,你不能只说int snap_cell_count[n_snapshots],而n_snapshots的值是在运行时确定的。你必须做类似

的事情
int *snap_cell_count = new int[n_snapshots];
// do some stuff
delete[] snap_cell_count;

cell_fates

也是如此

至于你的链接器错误,也许this question解决了你的问题......