我目前正在尝试做自己的 shell,它必须是多语言的。
所以我尝试实现一个读取 .txt 文件中的行的函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// globals
char lang[16] = {'t','r','y'};
char aMsg[512];
// functions
void takeFile() {
int i =0;
char namFil[32];
char lg[16];
FILE * file;
char tmp[255];
char * line = tmp;
size_t len = 0;
ssize_t read;
strcpy(namFil,"/media/sf_Projet_C/");
strcpy(lg,lang);
strcat(lg, ".txt");
strcat(namFil, lg);
file = fopen(namFil, "r");
printf("%s\n", namFil);
while((read = getline(&line,&len, file)) != -1) {
aMsg[i] = *line;
i++;
}
}
enum nMsg {HI, QUIT};
int main(void) {
takeFile();
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
}
我使用的是 win7,但我在虚拟机上使用 gcc 进行编译。
我有一个警告说:
format'%s' expects argument of type 'char *', but argument 2 (and 3) has type 'int' [-Wformat=]
我尝试使用 %d 而不是 %s 执行程序,它会打印数字。
我不明白是什么将我的 aMsg 转换为 int 。
我的 try.txt 文件只是:
Hi
Quit