[C] 纯文本查看 复制代码 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int write()
{
// 文件路径
const char *filepath = "/Users/yusian/Desktop/hello.txt";
// 定义文件指针
FILE *fp = fopen(filepath, "wb");
if (fp == NULL){
printf("Faild to open file!\n");
return -1;
}
{ // code...
char ip[] = "192.168.1.254";
int port = 8080;
fprintf(fp, "ip=%s\n", ip);
fprintf(fp, "port=%d", port);
}
// end
fclose(fp);
return 0;
}
int load()
{
const char *filepath = "/Users/yusian/Desktop/hello.txt";
FILE *fp = fopen(filepath, "rw");
if (fp == NULL){
printf("Faild to open file!\n");
return -1;
}
// code...
char bf[512];
while(!feof(fp)){
// 按行读取
char *line = fgets(bf, 512, fp);
if(line){
printf("%s", line);
}
}
// end
fclose(fp);
return 0;
}
int main()
{
write();
load();
return 0;
} 运行结果:[C] 纯文本查看 复制代码 ip=192.168.1.254
port=8080Program ended with exit code: 0
|