错误:格式为'%s'的参数应为类型'char *',但参数2的类型为'int'[-Wformat =] [英] Error : format'%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]

查看:214
本文介绍了错误:格式为'%s'的参数应为类型'char *',但参数2的类型为'int'[-Wformat =]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试制作自己的shell,它必须是多语言的. 因此,我尝试实现一个读取.txt文件中的行的功能.

I am currently trying to do my own shell, and it has to be polyglot. So I tryed to implement a function that reads the lines in a .txt file.

#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进行编译.

I am on win7 but I compile with gcc on a VM.

我有个警告:

format'%s' expects argument of type 'char *', but argument 2 (and 3) has type 'int' [-Wformat=]

我试图用%d而不是%s执行该编,它会打印数字.

I tried to execute the prog with %d instead of %s and it prints numbers.

我不明白是什么将我的aMsg转换为int.

I don't understand what converts my aMsg into a int.

我的try.txt文件就是:

My try.txt file is just :

Hi
Quit

推荐答案

我完全在猜测,但是我认为您想要的是:

I'm completely guessing, but I think what you want is:

#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;
}

这篇关于错误:格式为'%s'的参数应为类型'char *',但参数2的类型为'int'[-Wformat =]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆