gpt4 book ai didi

c - 从 int 函数返回 char * 而不使用指针?

转载 作者:行者123 更新时间:2023-11-30 16:47:00 25 4
gpt4 key购买 nike

我有一个函数,它从给定的文件中读取,查找给定的关键字,返回由给定字符串与单词分隔的键值。我的函数的返回值目前是 char * ,据我了解整个问题,这对于错误处理来说是低于标准的。另一方面,我不想摆弄指针来存储值。至少在这种情况下。

有没有办法只通过函数返回字符串值,同时返回成功/失败的 int 值?

我的字符*代码:

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

/*!
* @brief Loads the config file and returns value of requested parameter search
* @param char *file - Config file to search in
* @param char *seperator - Seperator to identify keyword and keyvalue
* @param char *search - Requested parameter to look for
* @return char * - Value of found keyword or error if search failed
*/

int confin(char *file, char *seperator, char *search)
{
FILE *conf;
char *buffer;
char *output;
char line[256];

char *returnerror = "Error!";


if((conf = fopen(file, "r")) == NULL) // Try to open file from path, return error if failed
{
fprintf(stderr, "Could not open config file \"%s\"!\n", file);
return returnerror;
}

while (fgets(line, sizeof(line), conf) != NULL) // Read lines of file until the end is reached
{
buffer = strtok(line, seperator); // Search for first appearance of seperator in line;
if((strcmp(buffer,search)) == 0) // If keyword is found,
{
buffer = strtok(NULL, seperator); // buffer the keyvalue,
output = malloc(sizeof(buffer));
strcpy(output, buffer); // copy it into the output string,
output[strcspn(output, "\n")] = 0; // replace the "\n" char from the end with terminating 0
fclose(conf);
return output; // and return the value of output.
}
}

fprintf(stderr, "Could not find config keyword \"%s\"!\n", search);
fclose(conf);
return returnerror;

}


int main ()
{
printf("%s\n",confin("test.conf","=","test"));
}

我尝试了以下代码,但返回值为(null)

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

/*!
* @brief Loads the config file and returns value of requested parameter search
* @param char *file - Config file to search in
* @param char *seperator - Seperator to identify keyword and keyvalue
* @param char *search - Requested parameter to look for
* @return int - 0 on success, 1 on failure
*/

int confin(char *file, char *seperator, char *search, char *value)
{
FILE *conf;

char *output;
char *buffer;
char line[256];


if((conf = fopen(file, "r")) == NULL) // Try to open file from path, return error if failed
{
fprintf(stderr, "Could not open config file \"%s\"!\n", file);
return 1;
}

while (fgets(line, sizeof(line), conf) != NULL) // Read lines of file until the end is reached
{
buffer = strtok(line, seperator); // Search for first appearance of seperator in line;
if((strcmp(buffer,search)) == 0) // If keyword is found,
{
buffer = strtok(NULL, seperator); // buffer the keyvalue,
output = malloc(sizeof(buffer));
strcpy(output, buffer); // copy it into the output string,
output[strcspn(output, "\n")] = 0; // replace the "\n" char from the end with terminating 0.
strcpy(value,output); // Store the new value in my return value
fclose(conf);
free (output);
return 0; // and return the value of output.
}
}

fprintf(stderr, "Could not find config keyword \"%s\"!\n", search);
fclose(conf);
return 1;

}


int main ()
{
char value[256] = "\0";
printf("%s\n",confin("test.conf","=","test",value));
}

希望此事尽快得到解决。即使这意味着我最后必须使用指针方法。提前致谢!

最佳答案

更改:

int main ()
{
char value[256] = "\0";
printf("%s\n",confin("test.conf","=","test",value));
}

至:

int main ()
{
char value[256] = "\0";
int retval = confin("test.conf","=","test",value);
if(retval)
// some error handling
printf("%s\n", value);
}

您所做的是调用 confin,它将字符串值分配给 value,但您尝试打印此函数返回的 int (作为字符串)而不是

您也可以只返回 char*,如果配置文件中不存在 key ,则返回 NULL。这是“No such key”类型错误的简单情况。在这种情况下,您只需检查 confin 是否返回 NULL。

关于c - 从 int 函数返回 char * 而不使用指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536419/

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