英文:
C read strings from a file with complext format
问题
Here's the translated code snippet with the corrected format string:
typedef struct {
char key[20];
char value[20];
} Dictionary;
int main() {
FILE *fin = fopen("../dizionario.txt", "r");
if (fin == NULL) {
printf("error");
return 1;
}
int n;
fscanf(fin, "%d", &n);
Dictionary dict[n];
for (int i = 0; i < n; i++)
fscanf(fin, "$%19[^$]$ %19s\n", dict[i].key, dict[i].value);
fclose(fin);
for (int i = 0; i < n; i++) {
printf("Key: %s, Value: %s\n", dict[i].key, dict[i].value);
}
return 0;
}
I've corrected the format string in the fscanf
function to properly read the key and value pairs from the input file.
英文:
So I have a file that looks like this:
9
$11$ pelle
$2$ pollo
$333$ palla
$41$ alla
$5078$ tta
$6$ tti
$7$ ll
$81$ er
$900$ ere
n = 9 is the number of key value pairs I want to read in my program.
so for key = "11", value = "pelle"
and so on...
typedef struct {
char key[20];
char value[20];
} Dictionary;
int main() {
FILE *fin = fopen("../dizionario.txt", "r");
if (fin == NULL) {
printf("error");
return 1;
}
int n;
fscanf(fin, "%d", &n);
Dictionary dict[n];
for (int i = 0; i < n; i++)
fscanf(fin, "$%[^$]$ %s\n", dict[i].key, dict[i].value);
fclose(fin);
for (int i = 0; i < n; i++) {
printf("Key: %s, Value: %s\n", dict[i].key, dict[i].value);
}
return 0;
}
I have adopted the following approach to read each key-value pair and store them in a dict struct which I believe to be a decent choice. When I print to the console to check the values are all over the place, the output being:
Key: , Value:
Key: , Value:
Key: , Value:
Key: , Value:
Key: , Value:
Key: , Value:
Key: , Value: å
Key: <0¶[÷, Value:
Key: , Value: °
I believe the problem to with the format string which I can't seem to get right:
"$%[^$]$ %s\n"
答案1
得分: 1
- 这里最简单的修复方法是在第2个格式字符串前添加一个空格
- 为你的大小使用符号常量,并且在使用
scanf()
读取字符串时始终使用最大字段宽度的值。 - 考虑在
n
上添加一个检查,确保它足够小(在我的系统上,默认堆栈大小为8 MB;或者更好地使用malloc()
并检查它是否成功)。
英文:
- The easiest fix here is add a whitespace
- Use symbolic constants for your size, and always use maximum field with values when reading strings with
scanf()
. - Consider adding a check on
n
to ensure it's sufficiently small (on my system the default stack is 8 MB; or even better usemalloc()
and check that it was successful).
#include <stdio.h>
#define KEY_LEN 19
#define VALUE_LEN 19
#define str(s) str2(s)
#define str2(s) #s
typedef struct {
char key[KEY_LEN + 1];
char value[VALUE_LEN + 1];
} Dictionary;
int main() {
FILE *fin = fopen("../dizionario.txt", "r");
if (!fin) {
printf("error");
return 1;
}
int n;
fscanf(fin, "%d", &n);
Dictionary dict[n];
for (int i = 0; i < n; i++)
fscanf(fin, " $%" str(KEY_LEN) "[^$]$ %" str(VALUE_LEN) "s", dict[i].key, dict[i].value);
fclose(fin);
for (int i = 0; i < n; i++)
printf("Key: %s, Value: %s\n", dict[i].key, dict[i].value);
}
and corresponding output:
Key: 11, Value: pelle
Key: 2, Value: pollo
Key: 333, Value: palla
Key: 41, Value: alla
Key: 5078, Value: tta
Key: 6, Value: tti
Key: 7, Value: ll
Key: 81, Value: er
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论