尝试将文件的行扫描到 C 中的不规则数组(指针数组)中

发布于 2024-11-07 17:54:37 字数 642 浏览 0 评论 0原文

通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞄了个咪的 2024-11-14 17:54:37

您需要将 func副本分配给 functions[i],如 functions[i] = strdup(func)< /code>,如果您有 strdup

You need to assign a copy of func to functions[i], as in functions[i] = strdup(func), if you have strdup.

隔纱相望 2024-11-14 17:54:37

您的问题是您正在重用 func 数组 - 所有functions[i] 都指向同一位置,因此将包含相同的字符串(您读到的最后一篇)。您需要为所有不同的字符串分配足够的内存。

看看 这个类似的问题。

Your problem is that you are reusing the func array - all functions[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.

栩栩如生 2024-11-14 17:54:37

请修复您的格式/代码块,现在当您无法打开文件并退出程序时,您似乎正在阅读 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);

这将导致类似的结果:

main() {
    char func[51];
    char functions[201][51];
    FILE * inf;
    if (( inf = fopen("test.txt", "r")) == NULL) {
        printf("can't open so exiting\n");
        exit(EXIT_SUCCESS)
    } // <-- you are missing this one

    int i = 0;
    while( fscanf(inf, "%s", func) != EOF){
        strcpy(functions[i++], func);
    }
    printf("%s\n", *functions); /* this is just for me to check if its working*/
}

这也可能是:

while (fscanf(inf, "%s", functions[i++]) != EOF);

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 structure func[51] now when you fscanf you overwrite func[51] all the time with a new line. And since your are storing the address pointing to func[51] to in the array functions 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 structure func is pointing to to functions[i] with strcpy(functions[i], func);

this will result in something like:

main() {
    char func[51];
    char functions[201][51];
    FILE * inf;
    if (( inf = fopen("test.txt", "r")) == NULL) {
        printf("can't open so exiting\n");
        exit(EXIT_SUCCESS)
    } // <-- you are missing this one

    int i = 0;
    while( fscanf(inf, "%s", func) != EOF){
        strcpy(functions[i++], func);
    }
    printf("%s\n", *functions); /* this is just for me to check if its working*/
}

This could also be:

while (fscanf(inf, "%s", functions[i++]) != EOF);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文