1
votes

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]);
}

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

I have a warning saying :

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

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

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

My try.txt file is just :

Hi
Quit
3
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]) --> printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]) BTW I don't know if this instruction does what you want.LPs
printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]); doesn't look right. Maybe you want printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]);? And don't free(line); as you haven't malloced it.Spikatrix
@Cool Guy Now it prints me HQ and Q... but no compile error ;)B_PRIEUR
I think you are misunderstanding what your code does. That printf print a NULL terminated string starting from index 0 of aMsg array and a NULL terminated string starting from index 1 of aMsg array. Probably you want char *aMsg[512]; changing your code according to array of pointers.LPs
This post did not ask a question. The answer selected provide working code and no explanation. Other answers explained and provided working snippets of code. Appears "fish" were wanted and not the fishing lesson".chux - Reinstate Monica

3 Answers

5
votes

The contents of your text file have nothing to do with the warning, which is generated by the compiler before your program ever runs. It is complaining about this statement:

printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);

Global variable aMsg is an array of char, so aMsg[HI] designates a single char. In this context its value is promoted to int before being passed to printf(). The %s field descriptor expects an argument of type char *, however, and GCC is smart enough to recognize that what you are passing is incompatible.

Perhaps you had in mind

printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]);

or the even the equivalent

printf("%s\n%s\n", aMsg + HI, aMsg + QUIT);

but though those are valid, I suspect they won't produce the result you actually want. In particular, given the input data you specified and the rest of your program, I would expect the output to be

HQ
Q

If you wanted to read in and echo back the whole contents of the input file then you need an altogether different approach to both reading in and writing out the data.

2
votes

Let's take a closer look on the problematic line:

printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);

The string you would like to print expects 2 string parameters. You have aMsg[HI] and aMsg[QUIT]. These two are pointing to a char, so the result is one character for each. All char variables can be interpreted as a character or as a number - the character's ID number. So I assume the compiler resolves these as int types, thus providing you that error message.
As one solution you merely use %c instead of %s.

However, I suspect you want to achieve something else.

1
votes

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