我有一個函數,從給定的文件中讀取,查找給定的關鍵字,返回一個由給定字符串與單詞分離的keyvalue。 我的函數返回值現在是char *,就我的理解而言,這是錯誤處理的欠佳值。 另一方面,我不會用指針來存儲值的指針。至少在這種情況下。從int函數返回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));
}
希望這件事被整理出來快。即使這意味着我必須在最後使用指針方法。提前致謝!
一種方法是創建一個同時包含'char *'和'int'的結構;然後讓你的函數返回。這在C中是很常見的做法,但是如果你想返回一個'char *',只需要傳遞'char **'作爲返回參數。 – merlin2011
C中的另一種方式可能是在函數被調用時傳遞的指針中返回char *結果。函數的返回值將是一個int值,以指示它是否成功。 – EmbedWise
我的建議是在你的函數中使用一些printf語句來追蹤發生的事情。當談到許多標準庫函數時,C可能會很有趣。他們可能無法按照您的想法工作。 – FernandoZ