gpt4 book ai didi

c - 解析命令并将文件路径传递给 fopen

转载 作者:行者123 更新时间:2023-11-30 20:58:26 25 4
gpt4 key购买 nike

我猜问题出在指针上。请建议任何方法来接收“cat serverpath 或 filepath”等命令并解析该命令并将路径传递到 fopen(filepath, "r")

这是服务器的代码:

time_t  start;
char buf[BUFSIZ], *command, content;
int n,i;
FILE *fptr;

start = time(0);
(void) pthread_mutex_lock(&stats.st_mutex);
stats.st_concount++;
(void) pthread_mutex_unlock(&stats.st_mutex);
while (1) {
bzero(buf, BUFSIZ);
n = recv(fd, buf, BUFSIZ, 0);
if (n < 0){
errexit("echo read: %s\n", strerror(errno));
}
else{
printf("The client message: %s\n",buf);
}

strtok_r (buf, " ", &command);
printf("%s\n", buf);
printf("%s\n",command);

//char filename[100], c;

// printf("Enter the filename to open \n");
// scanf("%s", command);

// Open file
fptr = fopen(command, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}

// Read contents from file
content = fgetc(fptr);
while (content != EOF)
{
printf ("%c", content);
content = fgetc(fptr);
}

我在 fopen 中遇到错误。

最佳答案

您正在使用 strtok_r 内部数据项,实际上不应使用该数据项,而只是在对 strtok_r 的连续调用中传递。

command = strtok(buf," ");
if (command) {
....

相反。

关于c - 解析命令并将文件路径传递给 fopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52745942/

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