简体   繁体   English

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

[英]Opening 2 data files with C++

I wrote a program to create gnuplot commands according to data that was read from an input file. 我编写了一个程序,根据从输入文件中读取的数据创建gnuplot命令。 I required far too many commands to do this by hand. 我需要太多命令才能手动执行此操作。 This worked quite nicely, but I now need to read data from two different data files and use these to create the gnuplot commands. 这工作得很好,但是我现在需要从两个不同的数据文件中读取数据,并使用它们来创建gnuplot命令。 Unfortunately something now seems to be going wrong with the reading of the two files. 不幸的是,现在两个文件的读取似乎出了问题。 The portion of code involved in reading from the data files is as follows: 读取数据文件涉及的部分代码如下:

#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 is simply some integer, snap_cell_count[] is an array with "n_shapshots" elements, each of which takes a value read from the data file "snapshot_data". n_snapshots只是一个整数,snap_cell_count []是一个包含“ n_shapshots”元素的数组,每个元素都从数据文件“ snapshot_data”中读取一个值。 cell_fates[] is an array with a number of elements that is equal to the value of the last element in snap_cell_count[], and again the values of its elements are read from a file, in this case "cell_fates_data_final". cell_fates []是一个数组,其元素数等于snap_cell_count []中的最后一个元素的值,并且再次从文件(在这种情况下为“ cell_fates_data_final”)中读取其元素的值。 The data files to be read from are stored in a folder called "data". 要读取的数据文件存储在名为“ data”的文件夹中。

Unfortunately, the compiler returns the following errors. 不幸的是,编译器返回以下错误。

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

This is being compiled with the gcc compiler on Mac OSX Mountain Lion. 这是使用Mac OSX Mountain Lion上的gcc编译器进行编译的。

Does anyone have any idea what's wrong here? 有人知道这是怎么回事吗?

Well, for starters you can't just say int snap_cell_count[n_snapshots] with the value of n_snapshots being determined at runtime. 好吧,对于初学者来说,您不能只说int snap_cell_count[n_snapshots] ,而n_snapshots的值是在运行时确定的。 You have to do something like 你必须做类似的事情

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

Same thing for cell_fates cell_fates

As for your linker errors, maybe this question solves your issues... 至于您的链接器错误,也许这个问题可以解决您的问题...

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

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