使用fscanf无法正常读取

时间:2015-11-28 13:27:49

标签: c arrays malloc scanf

我有以下困难。我正在尝试阅读此文件。

3
1 2 3
2
4 5

单独的数字是数组(3 and 2)的大小。以下数字是数组。 所以3 is the size of (1,2,3)2 is the size of (4,5)

我在C上编写了一个代码来读取这些数字并将它们存储在使用malloc()的数组中。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
    int i=0, j, *size, *vector;

    size=(int*)malloc(sizeof(int));
    vector=(int*)malloc(sizeof(int));

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }
    else {
        do{
            fscanf(file,"%d",&size[i]);
            for(j=0;j<=size[i];j++){
                fscanf(file,"%d",&vector[j]);
            }
            i++;

        }while(feof(file)==0);
    }
    fclose(file);

return 0;
}

正在正确阅读文件,但如果我打印出我得到的数字:

3
5
2
3
4
2

2 个答案:

答案 0 :(得分:1)

这里要提两件事。

  1. 您通过malloc()仅为一个变量分配了内存,但是您尝试在do...while循环中超出分配的内存进行访问。在需要时,您需要realloc()

  2. 请参阅Why is “while ( !feof (file) )” always wrong?

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i, j, size, *size_v, **vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;//scan file
    while(EOF != fscanf(file, "%d", &size)){
        for(j = 0; j < size; j++){
            fscanf(file, "%*d");
        }
        i++;
    }
    rewind(file);

    size_v = malloc(i * sizeof(int));
    vector = malloc(i * sizeof(int*));

    i = 0;//read file
    while(EOF != fscanf(file, "%d", &size_v[i])){
        vector[i] = malloc(size_v[i] * sizeof(int));

        for(j = 0; j < size_v[i]; j++){
            fscanf(file, "%d", &vector[i][j]);
        }
        i++;
    }
    fclose(file);

    //print & deallocate
    size = i;
    for(i = 0; i < size; ++i){
        printf("%d\n", size_v[i]);
        for(j = 0; j < size_v[i]; ++j){
            printf("%d ", vector[i][j]);
        }
        printf("\n");
        free(vector[i]);
    }
    free(vector);
    free(size_v);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i, j, size, *vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;
    while(EOF != fscanf(file, "%d", &size)){
        printf("%d\n", size);//each loop
        vector = malloc(size * sizeof(int));
        for(j = 0; j < size; j++){
            fscanf(file, "%d", &vector[j]);
        }
        for(j = 0; j < size; j++){
            printf("%d ", vector[j]);
        }
        printf("\n");
        free(vector);
        i++;
    }
    fclose(file);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i, j, k, size, size_sum, *size_v, *vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;//scan file
    size_sum = 0;
    while(EOF != fscanf(file, "%d", &size)){
        size_sum += size;
        for(j = 0; j < size; j++){
            fscanf(file, "%*d");
        }
        i++;
    }
    rewind(file);

    size_v = malloc(i * sizeof(int));
    vector = malloc(size_sum * sizeof(int));

    i = 0;//read file
    k = 0;
    while(EOF != fscanf(file, "%d", &size_v[i])){
        for(j = 0; j < size_v[i]; j++){
            fscanf(file, "%d", &vector[k++]);
        }
        i++;
    }
    fclose(file);

    //print & deallocate
    size = i;
    size_sum = 0;
    for(i = 0; i < size; ++i){
        printf("%d\n", size_v[i]);
        for(j = 0; j < size_v[i]; ++j){
            printf("%d ", vector[j + size_sum]);
        }
        printf("\n");
        size_sum += size_v[i];
    }
    free(vector);
    free(size_v);

    return 0;
}