#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char sharePath[100];
char logPath[100];
char motd[100];
}pathName_t;
void readConfig(const char *config){
FILE *fp = fopen(config, "r");
pathName_t pathpath;
int line = 0;
int index = 0;
int c;
c = getc(fp);
while( c != EOF){
if( c != '\n' ){ //if c not equal to new line, loop through the entire line
if( line == 0 ){ //line = 0
pathpath.sharePath[index] = c;
}else if( line == 1 ){ //line = 1
pathpath.logPath[index] = c;
}else{ //line = 2
pathpath.motd[index] = c;
}
index++;
}else{ //if c equal to new line, replace \0 from \n to the end of string
if( line == 0 ){ //line = 0
pathpath.sharePath[index] = '\0';
}else if( line == 1 ){ //line = 1
pathpath.logPath[index] = '\0';
}else{ //line = 2
pathpath.motd[index] = '\0';
}
index = 0;
line++;
} // else close
c = getc(fp);
} //while loop close
fclose(fp);
} //readConfig() close
int main(){
pathName_t pathName;
readConfig("settings.config");
printf("%s", pathName.motd);
printf("%s",pathName.sharePath);
}
这是我得到的结果:�z���
我打算创建一个函数以多行读取和检索此文件中的路径名存储,结束编译的struct,但结果似乎出错了。或者您中有人建议是否有其他方法可以从文件中读取内容并具有多个返回值?
答案 0 :(得分:0)
您的功能不执行任何操作。您不会将指针传递给本地变量,也不会为其分配返回值,而是打印出堆栈中的随机垃圾