尝试将文件的行扫描到 C 中的不规则数组(指针数组)中
通过 putty 运行 UNIX,并使用 vi 和 gcc。 我正在尝试读取文件的一行并使用不规则数组存储这些行。 我不会提供整个代码,因为它是不必要的,
main(){
char func[51];
char * functions[201];
FILE * inf;
if (( inf = fopen("test.txt", "r")) == NULL){
printf("can't open so exiting\n");
exit(EXIT_SUCCESS)
int i;
i = 0;
while( fscanf(inf, "%s", func) != EOF){
functions[i] = func;
i++;
}
printf("%s\n", *functions); /* this is just for me to check if its working*/
}
包括 stdio.h、stdlib.h 和 string.h
所以代码的工作原理是它运行在文件中,并在 while 循环期间存储该行,但我希望如此函数[0]存储指向第一行的指针,函数[1]存储指向第二行的指针,依此类推。我不知道该怎么做,任何帮助将不胜感激,谢谢:)
running UNIX through putty, and using vi and gcc.
I am attempting to read in a line of a file and using a ragged array store the lines.
I won't provide the whole code because it is unnecessary,
main(){
char func[51];
char * functions[201];
FILE * inf;
if (( inf = fopen("test.txt", "r")) == NULL){
printf("can't open so exiting\n");
exit(EXIT_SUCCESS)
int i;
i = 0;
while( fscanf(inf, "%s", func) != EOF){
functions[i] = func;
i++;
}
printf("%s\n", *functions); /* this is just for me to check if its working*/
}
incldues stdio.h, stdlib.h and string.h
SO the code works in that it runs through the file and during the while loop it stores the line but I want it so that functions[0] stores a pointer leading to the first line and functions[1] stores a pointer leading to the second line and so on. I'm not sure how to do this, any help would be appreciated thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将
func
的副本分配给functions[i]
,如functions[i] = strdup(func)< /code>,如果您有
strdup
。You need to assign a copy of
func
tofunctions[i]
, as infunctions[i] = strdup(func)
, if you havestrdup
.您的问题是您正在重用 func 数组 - 所有
functions[i]
都指向同一位置,因此将包含相同的字符串(您读到的最后一篇)。您需要为所有不同的字符串分配足够的内存。看看 这个类似的问题。
Your problem is that you are reusing the
func
array - allfunctions[i]
point to the same place and therefore will contain the same string (the last one you read). You will need to allocate enough memory for all your different strings.Have a look at this similar question.
请修复您的格式/代码块,现在当您无法打开文件并退出程序时,您似乎正在阅读
test.txt
。您必须记住,变量
func
指向数据结构func[51]
现在,当您fscanf
时,您会覆盖func [51]
始终换行。由于您将指向func[51]
的地址存储在数组functions
中,因此您实际上并未存储 char 数组的内容(func[ 51]
)。您可以通过将函数数组设置为二维数组
char Functions[201][51]
来解决此问题,然后使用 strcpy 复制func
指向的数据结构到functions[i]
和strcpy(functions[i], func);
这将导致类似的结果:
这也可能是:
Please fix your formating / code blocks now it seems like you are reading
test.txt
when you have failed to open the file and exited the program.You have to keep in mind that the variable
func
points to the data structurefunc[51]
now when youfscanf
you overwritefunc[51]
all the time with a new line. And since your are storing the address pointing tofunc[51]
to in the arrayfunctions
you don't actually store the contents of the char array (func[51]
).You could solve this by make your functions array an two-dimensional array
char functions[201][51]
and then use a strcpy to copy the data structurefunc
is pointing to tofunctions[i]
withstrcpy(functions[i], func);
this will result in something like:
This could also be: