用 C++ 打开 2 个数据文件
Posted
技术标签:
【中文标题】用 C++ 打开 2 个数据文件【英文标题】:Opening 2 data files with C++ 【发布时间】:2013-08-08 10:43:31 【问题描述】:我编写了一个程序来根据从输入文件中读取的数据创建 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】:嗯,对于初学者来说,你不能只说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 可以解决您的问题...
【讨论】:
谢谢,链接的问题似乎已经解决了编译错误。以后我会记住在运行时设置数组大小的提示。以上是关于用 C++ 打开 2 个数据文件的主要内容,如果未能解决你的问题,请参考以下文章