C 文件读取返回并重复两次
我试图创建一个函数,读取.txt文件并返回特定给定行的行值 我的问题是,当我将变量设置为存储配置文件的第一行和第二行时,它只返回两个变量的第二行 我的职能:C 文件读取返回并重复两次,c,C,我试图创建一个函数,读取.txt文件并返回特定给定行的行值 我的问题是,当我将变量设置为存储配置文件的第一行和第二行时,它只返回两个变量的第二行 我的职能: char *get_config(char *fileName, int lineNumber, char *dst, int dstlen) { FILE *file; file = fopen(fileName, "r"); int count = 0; if (file != NULL) { static char sin
char *get_config(char *fileName, int lineNumber, char *dst, int dstlen)
{
FILE *file;
file = fopen(fileName, "r");
int count = 0;
if (file != NULL)
{
static char singleLine[150];
while (fgets(singleLine, sizeof singleLine, file) != NULL)
{
if (count == lineNumber - 1)
{
char *line;
if ( (line=strchr(singleLine, '\n')) != NULL)
{
*line = '\0';
strncpy(dst, singleLine, dstlen);
}
//strcpy(dst, singleLine); // NOT SAFE!!! should use strncpy
strncpy(dst, singleLine, dstlen);
}
else
{
count++;
}
}
}
else
{
printf("Configuration file is empty.");
}
fclose(file);
}
int main(){
char server_url[150];
char api_key[150];
get_config("config.txt", 1, server_url, sizeof server_url);
get_config("config.txt", 2, api_key, sizeof api_key);
printf("%s\n", server_url);
printf("%s\n", api_key);
}
这就是我调用函数的方式:
char *get_config(char *fileName, int lineNumber, char *dst, int dstlen)
{
FILE *file;
file = fopen(fileName, "r");
int count = 0;
if (file != NULL)
{
static char singleLine[150];
while (fgets(singleLine, sizeof singleLine, file) != NULL)
{
if (count == lineNumber - 1)
{
char *line;
if ( (line=strchr(singleLine, '\n')) != NULL)
{
*line = '\0';
strncpy(dst, singleLine, dstlen);
}
//strcpy(dst, singleLine); // NOT SAFE!!! should use strncpy
strncpy(dst, singleLine, dstlen);
}
else
{
count++;
}
}
}
else
{
printf("Configuration file is empty.");
}
fclose(file);
}
int main(){
char server_url[150];
char api_key[150];
get_config("config.txt", 1, server_url, sizeof server_url);
get_config("config.txt", 2, api_key, sizeof api_key);
printf("%s\n", server_url);
printf("%s\n", api_key);
}
config.txt文件:
asdasdsad6aa2afd2-ab74-43df-87ed-460441d4a5ad
输出:
deojeff@linux:~/DeojeffFolder/rms-datalogger$ ./read_config
6aa2afd2-ab74-43df-87ed-460441d4a5ad
6aa2afd2-ab74-43df-87ed-460441d4a5ad
为什么会这样?我怎样才能解决这个问题?谢谢我不明白为什么您的
get\u config
函数返回指向char
的指针,但是,我发现有两种方法可以在不重写循环的情况下解决问题:
添加break代码>
while (fgets(singleLine, sizeof singleLine, file) != NULL)
{
if (count == lineNumber - 1)
{
char *line;
if ( (line=strchr(singleLine, '\n')) != NULL)
{
*line = '\0';
strncpy(dst, singleLine, dstlen);
}
//strcpy(dst, singleLine); // NOT SAFE!!! should use strncpy
strncpy(dst, singleLine, dstlen);
break;
}
else
{
count++;
}
}
或将计数的增量移动到条件之外:
while (fgets(singleLine, sizeof singleLine, file) != NULL)
{
if (count == lineNumber - 1)
{
char *line;
if ( (line=strchr(singleLine, '\n')) != NULL)
{
*line = '\0';
strncpy(dst, singleLine, dstlen);
}
//strcpy(dst, singleLine); // NOT SAFE!!! should use strncpy
strncpy(dst, singleLine, dstlen);
}
count++;
}
对不起,英语对我来说不是坚实的基础。希望有帮助 似乎您也应该在复制所需的行后增加计数,否则下次读取行时它的值相同。当你得到你想要的那条线时,你可能会考虑打破这个循环,这样你就不会阅读第1行的整个文件。非常感谢。:'-)<代码>strncpy
不会做你认为它会做的事情。你关于“不安全”的评论很讽刺