gpt4 book ai didi

C - 读取文件和 strtok 到数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:55 26 4
gpt4 key购买 nike

我正在尝试标记文件并将标记保存到名为 tokenize 的函数内的数组中。然后我想获取要在 main 或其他函数中使用的 tokenize 函数的输出,以执行某些操作。

问题是:我不太确定如何在标记文件行后在 main 函数中向前移动指针。目标是将标记化的行保持组合在一起而不是分开,这样单词的意思就不会丢失。

file.txt 看起来像(为了便于阅读,在\t 之间添加了空格):

948213843 \t 644321498 \t 16549816514 \t 13616131216 \t 1646312132 \t 13468486

我的问题:我如何才能访问从 tokenize 函数返回的数组信息?

谢谢!

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define SIZE 1024

char *tokenize(char *buffer, char *pattern)
{
int i = 0;
int j;
char *arr[15];

char *token = strtok_r(buffer, "\t", &buffer);
for (j=0; j < 15; j++)
arr[j] = malloc(strlen(buffer) * sizeof(char));

// pattern matching removed to focus only on tokenization
while (token != NULL)
{
strcpy(arr[i], token);
printf("%s\n", token);
token = strtok_r(NULL, "\t", &buffer);
i++;
}

// test to verify array data --- good here
for (i=0; i < 15; i++)
fprintf(stdout, "test: %s\n", arr[i]);

return *arr;
}

int main(int argc, char *argv[])
{
FILE *filename;
static char buffer[SIZE];

filename = fopen("file_name.txt", "rb+");
if (filename != NULL)
{
while (fgets(buffer, SIZE, filename) != NULL)
{
if (buffer[strlen(buffer) - 1] == '\n')
{
buffer[strlen(buffer) - 1] = '\0';

// the matching search pattern will grab the line of data to be tokenized
char *token = tokenize(buffer, "948213843");


// test print -- not good here
if (token != NULL)
{
for (int i=1; i < 15; i++)
fprintf(stdout, "sucks: %s\n", token);
}

// do something with the tokens
// doSomethingWithToken(token);
}
}
}
}

最佳答案

您的代码中有许多错误。我已在下面的代码中修复了我可以找到的那些,我在其中使用了三重斜杠 (///) 来标记我所做的更改。

但是,主要问题是您的 arr[] 被定义为字符串数组(字符指针),但您正试图处理它(在 main ) 作为单个字符串。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define SIZE 1024

char** tokenize(char* buffer, char* pattern) /// You want to return an ARRAY of pointers (array of strings)!
{
int i = 0;
int j;
static char* arr[15]; /// Without the static keyword, the array is lost when the function returns

char* token = strtok_r(buffer, "\t", &buffer);
for (j = 0; j < 15; j++) {///
arr[j] = malloc((strlen(buffer) + 1) * sizeof(char)); /// Need to add 1 char for nul terminator
strcpy(arr[j], ""); /// Put in an empty string!
}///
// pattern matching removed to focus only on tokenization
while (token != NULL)
{
strcpy(arr[i], token);
printf("%s\n", token);
token = strtok_r(NULL, "\t", &buffer);/// Oops! I've undone this change.
i++;
}

// test to verify array data --- good here
for (i = 0; i < 15; i++)
fprintf(stdout, "test: %s\n", arr[i]);

return arr; /// This now returns the array of string pointers!
}

int main(int argc, char* argv[])
{
FILE* filename;
static char buffer[SIZE];

filename = fopen("file_name.txt", "rb+");
if (filename != NULL)
{
while (fgets(buffer, SIZE, filename) != NULL)
{
if (buffer[strlen(buffer) - 1] == '\n')
{
buffer[strlen(buffer) - 1] = '\0';

// the matching search pattern will grab the line of data to be tokenized
char** token = tokenize(buffer, "948213843"); /// Change this to match new function definition!

// test print -- not good here
if (token != NULL)
{
for (int i = 1; i < 15; i++)
fprintf(stdout, "sucks: %s\n", token[i]); /// Need to give index to each string!
}

// do something with the tokens
// doSomethingWithToken(token);

/// Clean-up: Free the arrays...
if (token != NULL)
{
for (int i = 1; i < 15; i++)
free(token[i]);
}

}
}
}
return 0;///
}

没有可用的数据文件,我无法测试它;另外,我的 MSVC 系统没有 strtok_r 函数,但我认为您调用的是正确的。

随时要求进一步澄清和/或解释。

关于C - 读取文件和 strtok 到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com