Error : format'%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
Error : format'%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
我目前正在尝试自己做 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 上,但我在 VM 上使用 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
您的文本文件的内容与警告无关,警告是编译器在您的程序运行之前生成的。它正在抱怨这个声明:
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
全局变量aMsg
是char
的数组,所以aMsg[HI]
指定单个char
。在这种情况下,它的值在传递给 printf()
之前被提升为 int
。然而,%s
字段描述符需要一个类型为 char *
的参数,并且 GCC 足够聪明,可以识别出您传递的内容是不兼容的。
也许你已经想到了
printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]);
或什至等价物
printf("%s\n%s\n", aMsg + HI, aMsg + QUIT);
虽然这些都是有效的,但我怀疑它们不会产生您真正想要的结果。特别是,给定您指定的输入数据和程序的其余部分,我希望输出为
HQ
Q
如果您想读入并回显输入文件的全部内容,那么您需要一种完全不同的方法来读入和写出数据。
让我们仔细看看有问题的行:
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
您要打印的字符串需要 2 个字符串参数。您有 aMsg[HI] 和 aMsg[QUIT]。这两个都指向一个char,所以结果是每个一个字符。所有 char 变量都可以解释为字符或数字 - 字符的 ID 号。所以我假设编译器将它们解析为 int 类型,从而为您提供该错误消息。
作为一种解决方案,您只需使用 %c 而不是 %s。
不过,我怀疑你想达到别的目的。
我完全是在猜测,但我想你想要的是:
#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] = malloc(strlen(line)+1);
strcpy(aMsg[i], line);
i++;
}
fclose(file);
}
enum nMsg {HI, QUIT};
int main(void) {
takeFile();
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
free(aMsg[HI]);
free(aMsg[QUIT]);
return 0;
}
我目前正在尝试自己做 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 上,但我在 VM 上使用 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
您的文本文件的内容与警告无关,警告是编译器在您的程序运行之前生成的。它正在抱怨这个声明:
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
全局变量aMsg
是char
的数组,所以aMsg[HI]
指定单个char
。在这种情况下,它的值在传递给 printf()
之前被提升为 int
。然而,%s
字段描述符需要一个类型为 char *
的参数,并且 GCC 足够聪明,可以识别出您传递的内容是不兼容的。
也许你已经想到了
printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]);
或什至等价物
printf("%s\n%s\n", aMsg + HI, aMsg + QUIT);
虽然这些都是有效的,但我怀疑它们不会产生您真正想要的结果。特别是,给定您指定的输入数据和程序的其余部分,我希望输出为
HQ
Q
如果您想读入并回显输入文件的全部内容,那么您需要一种完全不同的方法来读入和写出数据。
让我们仔细看看有问题的行:
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
您要打印的字符串需要 2 个字符串参数。您有 aMsg[HI] 和 aMsg[QUIT]。这两个都指向一个char,所以结果是每个一个字符。所有 char 变量都可以解释为字符或数字 - 字符的 ID 号。所以我假设编译器将它们解析为 int 类型,从而为您提供该错误消息。
作为一种解决方案,您只需使用 %c 而不是 %s。
不过,我怀疑你想达到别的目的。
我完全是在猜测,但我想你想要的是:
#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] = malloc(strlen(line)+1);
strcpy(aMsg[i], line);
i++;
}
fclose(file);
}
enum nMsg {HI, QUIT};
int main(void) {
takeFile();
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
free(aMsg[HI]);
free(aMsg[QUIT]);
return 0;
}