Read from file in C with complex format.

huangapple go评论64阅读模式
英文:

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 = &quot;11&quot;, value = &quot;pelle&quot; and so on...

typedef struct {
    char key[20];
    char value[20];
} Dictionary;

int main() {
    FILE *fin = fopen(&quot;../dizionario.txt&quot;, &quot;r&quot;);
    if (fin == NULL) {
        printf(&quot;error&quot;);
        return 1;
    }

    int n;
    fscanf(fin, &quot;%d&quot;, &amp;n);

    Dictionary dict[n];
    for (int i = 0; i &lt; n; i++)
        fscanf(fin, &quot;$%[^$]$ %s\n&quot;, dict[i].key, dict[i].value);
    
    fclose(fin);

    for (int i = 0; i &lt; n; i++) {
        printf(&quot;Key: %s, Value: %s\n&quot;, 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: &#229;
Key: &lt;0&#182;[&#247;, Value:
Key:    , Value: &#176;

I believe the problem to with the format string which I can't seem to get right:

&quot;$%[^$]$ %s\n&quot;

答案1

得分: 1

  1. 这里最简单的修复方法是在第2个格式字符串前添加一个空格 ,这样它就会忽略前一行的尾随换行符。
  2. 为你的大小使用符号常量,并且在使用 scanf() 读取字符串时始终使用最大字段宽度的值。
  3. 考虑在 n 上添加一个检查,确保它足够小(在我的系统上,默认堆栈大小为8 MB;或者更好地使用 malloc() 并检查它是否成功)。
英文:
  1. The easiest fix here is add a whitespace before the 2nd format string so it ignores the trailing newline from the preceding line.
  2. Use symbolic constants for your size, and always use maximum field with values when reading strings with scanf().
  3. Consider adding a check on n to ensure it's sufficiently small (on my system the default stack is 8 MB; or even better use malloc() and check that it was successful).
#include &lt;stdio.h&gt;

#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(&quot;../dizionario.txt&quot;, &quot;r&quot;);
	if (!fin) {
		printf(&quot;error&quot;);
		return 1;
	}
	int n;
	fscanf(fin, &quot;%d&quot;, &amp;n);
	Dictionary dict[n];
	for (int i = 0; i &lt; n; i++)
		fscanf(fin, &quot; $%&quot; str(KEY_LEN) &quot;[^$]$ %&quot; str(VALUE_LEN) &quot;s&quot;, dict[i].key, dict[i].value);
	fclose(fin);

	for (int i = 0; i &lt; n; i++)
		printf(&quot;Key: %s, Value: %s\n&quot;, 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

huangapple
  • 本文由 发表于 2023年5月17日 23:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定