f****n 发帖数: 723 | 1 如果有一个数据列:
1 2 3
4 5 6 7
1 2 3 4 5
223
3 445 6
。。。
总之,每一行的数据数目不尽相同。
如果想把这个文件中的数据读成array,怎么作?
多谢。 |
e****d 发帖数: 333 | 2 ifstream 里面重载了 operator>>.
ifstream::eof()判断结束。
array用new 实现。
完了。 |
f****n 发帖数: 723 | 3 Sorry, C 呢?
【在 e****d 的大作中提到】 : ifstream 里面重载了 operator>>. : ifstream::eof()判断结束。 : array用new 实现。 : 完了。
|
e****d 发帖数: 333 | 4 为什么一定用C呢?
【在 f****n 的大作中提到】 : Sorry, C 呢?
|
f****n 发帖数: 723 | 5 不太懂c++,而且其他的程序也是c的,汗……
【在 e****d 的大作中提到】 : 为什么一定用C呢?
|
e****d 发帖数: 333 | 6 一定要用C的话,我也没时间去看具体的。你自渡吧。 |
X****r 发帖数: 3557 | 7 以下程序没测试过,仅供参考。
/**
* @param input The input file.
* @param size A pointer to store result size.
* @return The pointer to the result data,
* or NULL for not enough memory.
*/
int *read_all(FILE *input, int *size) {
int n = 128, i;
int *data = (int *)malloc(n * sizeof(int));
for (i = 0; data && fscanf(input, "%d", data + i); i++) {
if (i == n) {
data = (int *)realloc(data, (n *= 2) * sizeof(int));
}
}
if (size) {
*size = i;
}
return data;
}
【在 f****n 的大作中提到】 : 不太懂c++,而且其他的程序也是c的,汗……
|
e****d 发帖数: 333 | 8 赞,资深IT。
【在 X****r 的大作中提到】 : 以下程序没测试过,仅供参考。 : /** : * @param input The input file. : * @param size A pointer to store result size. : * @return The pointer to the result data, : * or NULL for not enough memory. : */ : int *read_all(FILE *input, int *size) { : int n = 128, i; : int *data = (int *)malloc(n * sizeof(int));
|
f****n 发帖数: 723 | 9 多谢
【在 X****r 的大作中提到】 : 以下程序没测试过,仅供参考。 : /** : * @param input The input file. : * @param size A pointer to store result size. : * @return The pointer to the result data, : * or NULL for not enough memory. : */ : int *read_all(FILE *input, int *size) { : int n = 128, i; : int *data = (int *)malloc(n * sizeof(int));
|