如何在while循环中正确使用fscanf?

时间:2019-01-22 06:26:13

标签: c scanf

当我尝试使用fscanf读取文本文件zip_code_sample.txt时,它无法正常工作。看来fscanf没做任何事情,因为当我之后尝试打印变量时,我什么也没得到。甚至没有进入while循环。

有人可以帮助我解决这个问题吗?我已经尝试了几个小时,并在Stack Overflow上阅读了其他帖子,以及在线上的许多文章,但没有一个能帮助我解决此问题。

来自zip_code_sample.txt的示例代码:

64720   Allenton
63730   Annada
64401   Alexandria
64830   Anabel
64402   Arbela

源代码:

#include <errno.h>
#include <stdlib.h>   // For _MAX_PATH definition
#include <stdio.h>
#include <string.h>

typedef struct cityStruct { unsigned int zip; char * town; } city;
typedef struct zipTownsStruct {
    int * zips;      // indexes to main array cities sorted by zip
    city * * towns;  // pointers to main array cities sorted by town name
    city * cities;   // main array of cities in order from file not sorted
} zipTowns;

void getArrs(zipTowns * arrs, int size) {  
    if((arrs->zips = (int *) malloc(sizeof(int) * size)) == NULL) {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(errno);
    }
    if((arrs->towns = (city **) malloc(sizeof(city*) * size)) == NULL) {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(errno);
    }
    if((arrs->cities = (city *) malloc(sizeof(city) * size)) == NULL) {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(errno);
    }
}

int getArgsInfoOpenFile(int argc, char * argv[], FILE ** infile, int * size) {
    int retval = 0;
    if(argc != 3) { // test for correct arguments number 3: exename, filename, size
        return -1;
    }
    if ((*infile = fopen("zip_code_sample.txt", "r")) == NULL) { // attempt to open file
        fprintf(stderr, "%s\n", strerror(errno));
        exit(errno);
    }
    return retval;
}

void readFile(zipTowns arrs, FILE * infile, int * length) {
    char * zip;
    char * town;
    while(fscanf(infile,"%s %s", zip, arrs.cities[*length].town) == 2) {
        arrs.cities[*length].zip = atoi(zip);
        printf("Zip: %s City: %s\n", arrs.cities[*length].zip, arrs.cities[*length].town); 
        printf("Zip: %s City: %s\n", zip, town); 
        length++;
    }
}

int main(int argc, char * argv[]) {
    zipTowns arrs; // all the arrays in one struct
    int length = 0; // current count of items in arrays 
    FILE * infile = NULL;
    int ret = 0, size = atoi(argv[2]);
    if(size > 999999 || size < 1) {
        printf("Illegal array size. Choose a size less than one million and greater than 0.\n");
        return -1;
    }

    if (getArgsInfoOpenFile(argc, argv, &infile, &size)) {
        printf("error in command line arguments\n");
        ret = -1;
    }else {
        getArrs(&arrs, size);
        readFile(arrs, infile, &length);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

在函数readFile中,

length++;

应该是

(*length)++;

length++不会增加length的值,而是会增加指针使其导致指向错误的内存位置,并且所有这些都应该从那里下坡。

其他问题:

char * zip;
char * town;

您只有一个char *。您实际上应该为这些变量分配一些内存。

typedef struct zipTownsStruct {
    int * zips;      // indexes to main array cities sorted by zip
    city * * towns;  // pointers to main array cities sorted by town name
    city * cities;   // main array of cities in order from file not sorted
} zipTowns;

不清楚您为什么拥有int *zip。在下面的代码中,您只是将atoi的输出直接分配给此变量。在这种情况下,它不应是指针。