我有一个函数,它从给定的文件中读取,查找给定的关键字,返回一个由该单词中给定字符串分隔的键值。 我的函数的返回值目前是char *,就错误处理而言,只要我理解了整个问题,它就是次要的。 另一方面,我没有用魔杖来摆弄存储值的指针。至少在这种情况下。
有没有办法只通过函数返回字符串值,同时返回成功/失败的int值?
我的char *代码:
#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));
}
希望这件事能快速整理出来。即使这意味着我必须在最后使用指针方法。提前致谢!
答案 0 :(得分:0)
变化:
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
1}}。
您也可以返回value
,如果您的配置文件中没有密钥,请返回char*
。这是一个简单的案例,仅适用于“No such key”类型错误。在这种情况下,您只需检查NULL
是否返回NULL。
答案 1 :(得分:0)
你已经得到了一些很好的答案,但我很惊讶没有人在这里提到过errno。 (https://en.wikipedia.org/wiki/Errno.h)
Errno是C管理错误的常用方法。例如,您可以在发生错误的函数中自己设置errno并返回NULL。这将告知调用函数出错的地方。