【发布时间】:2011-05-28 08:21:45
【问题描述】:
我有单独的函数用于读取文本文件(取决于它是 int、float 还是 double)。我只想要一个带有附加参数的 one 函数(不使用后续的 IF 语句)。有人有想法吗?
下面是我当前函数的形式。
float * read_column_f (char * file, int size_of_col){
...
col = (float*) malloc (height_row * sizeof(float));
... return(col);}
double * read_column_d (char * file, int size_of_col){
...
col = (double*) malloc (height_row * sizeof(double));
... return(col);}
int * read_column_i (char * file, int size_of_col){
...
col = (int*) malloc (height_row * sizeof(int));
... return(col);}
编辑:我想在 C++ 中实现这一点,使用的 C 样式语法是由于内存偏好。
【问题讨论】:
-
我删除了 C++ 标签,以及现存的 IOStreams 和函数重载,如果你想在没有它们的情况下执行此操作,再加上代码样式几乎可以保证这是纯 C 代码。跨度>
-
最有可能使用
const char *file。 -
size_of_col 是否指的是您传递给函数的值(float、double、int)的大小?
-
size_of_col = 列中的条目数,