gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:42:49 26 4
gpt4 key购买 nike

当我尝试使用 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;
}

最佳答案

在函数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的输出直接分配给该变量。在这种情况下,它不应该是一个指针。

关于c - 如何在 while 循环中正确使用 fscanf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54302412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com