Lets assume that I have a file, that contains the following strings:
让我们假设我有一个包含以下字符串的文件:
option0 value0
option1 value1
....... ......
optionX valueX
I am trying to make a C program to read the file. I decided to create a 2D array of pointers to strings and store all options in the pointee(s) of the first row and all values in the pointee(s) of the second row. This way:
我正在尝试制作一个C程序来读取该文件。我决定创建一个指向字符串的指针的2D数组,并将所有选项存储在第一行的指针对象中,并将所有值存储在第二行的指针对象中。这条路:
pointer[0][0] should point to option0
pointer[1][0] should point to value0
pointer[0][1] should point to option1
pointer[1][1] should point to value1
It seems that the program reads the file successfully, but I can not access the strings to print them out. Here is a stripped down version of the code, which causes the problem:
程序似乎成功读取文件,但我无法访问字符串以将其打印出来。这是代码的精简版本,导致问题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
#define BUFF 1024
FILE *fd;
int i = 0;
char buff[BUFF];
char *options [2][20];
fd = fopen("foo.txt", "r");
while (!feof(fd)) {
if (fgets(buff, sizeof(buff), fd) != NULL) {
(options[0][i] = malloc(sizeof (char) * 512)
(options[1][i] = malloc(sizeof (char) * 512)
sscanf(buff, "%s %s", options[0][i], options[1][i]);
i++;
}
}
while (i >= 0) {
printf ("Option %i is %s its value is %s", i, *option[0][i], *option[1][i]);
i--;
}
fclose(fd);
return 0;
}
I played a few days with that, but I can not seem to make it work. I checked various * questions and other online resources, but even experimenting did not help me figure out that.
我玩了几天,但我似乎无法使它工作。我检查了各种*问题和其他在线资源,但即使是试验也没有帮我弄明白。
What I want to know is: Are the strings stored properly with malloc and sscanf? If 'no', why? Why am I not able to print the string with the printf statement? How to fix that?
我想知道的是:字符串是否与malloc和sscanf一起正确存储?如果'不',为什么?为什么我无法使用printf语句打印字符串?如何解决?
Please be aware that the code shown is only for testing purposes, so I am aware that there is no error checking, etc..
请注意,显示的代码仅用于测试目的,因此我知道没有错误检查等。
2 个解决方案
#1
2
You are dereferencing the pointers when giving them to printf
. It expects char*
, not char
for %s
.
在将它们提供给printf时,您正在取消引用指针。它期望char *,而不是%s的char。
printf ("Option %i is %s its value is %s", i, *option[0][i], *option[1][i]);
This should be
这应该是
printf ("Option %i is %s its value is %s", i, option[0][i], option[1][i]);
Another issue is you have to decrement i
before printing in the while loop. You increment it after each memory allocation, so in the end it points past the last element. Move the i--;
to be the first inside the while loop.
另一个问题是你必须在while循环中打印之前减少i。你在每次内存分配后递增它,所以最后它指向最后一个元素。移动i--;成为while循环中的第一个。
Also sizeof(char)
is always 1, so you don't need that in calculating sizes for malloc()
.
sizeof(char)也始终为1,因此在计算malloc()的大小时不需要它。
Please also adjust your compiler's warnings to a higher level. For example gcc with -Wall
will say:
还请将编译器的警告调整到更高级别。例如gcc with -Wall会说:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
警告:格式'%s'需要类型为'char *'的参数,但参数2的类型为'int'[-Wformat =]
#2
0
1) i--
has to come before the printf
1)我必须在printf之前来
2) As in the answer of Sami, dont dereference the pointers in printf
2)正如在Sami的答案中,不要取消引用printf中的指针
while (0 <=-- i)
printf ("Option %i is %s its value is %s", i, option[0][i], option[1][i]);
#1
2
You are dereferencing the pointers when giving them to printf
. It expects char*
, not char
for %s
.
在将它们提供给printf时,您正在取消引用指针。它期望char *,而不是%s的char。
printf ("Option %i is %s its value is %s", i, *option[0][i], *option[1][i]);
This should be
这应该是
printf ("Option %i is %s its value is %s", i, option[0][i], option[1][i]);
Another issue is you have to decrement i
before printing in the while loop. You increment it after each memory allocation, so in the end it points past the last element. Move the i--;
to be the first inside the while loop.
另一个问题是你必须在while循环中打印之前减少i。你在每次内存分配后递增它,所以最后它指向最后一个元素。移动i--;成为while循环中的第一个。
Also sizeof(char)
is always 1, so you don't need that in calculating sizes for malloc()
.
sizeof(char)也始终为1,因此在计算malloc()的大小时不需要它。
Please also adjust your compiler's warnings to a higher level. For example gcc with -Wall
will say:
还请将编译器的警告调整到更高级别。例如gcc with -Wall会说:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
警告:格式'%s'需要类型为'char *'的参数,但参数2的类型为'int'[-Wformat =]
#2
0
1) i--
has to come before the printf
1)我必须在printf之前来
2) As in the answer of Sami, dont dereference the pointers in printf
2)正如在Sami的答案中,不要取消引用printf中的指针
while (0 <=-- i)
printf ("Option %i is %s its value is %s", i, option[0][i], option[1][i]);