Reading from **argv

Hello.
I'm trying to read the third string(argument) from argv one character at a time.
The code below results in a warning saying that argument 1 of fgetc makes pointer
from integer without a cast.
What integer? c is a char! argv is an array of pointers to C-strings!
What integer is it talking about?

Can anyone help me to properly dereference the argv?
I don't know how to handle this one.
Thank you.

1
2
3
4
5
6
7
  int i;
  char c;
  do{
     c=fgetc(argv[2][i]);
     printf("%c", c);
     i++;
  }while(c!=EOF);
Last edited on
A char is an integer type (or integral type, if you prefer). It's a one byte integer. See the fundamental data types section here: http://www.cplusplus.com/doc/tutorial/variables/

fgetc, in fact, returns a character as an int, not a char. But the issue here is that fgetc reads from a file stream, not from a c-string. Here is the description: http://www.cplusplus.com/reference/cstdio/fgetc/

You don't need fgetc to access the characters one by one from argv. Just access the character one by one using your index, i. But be sure to initialize it to zero before your do loop.

Also, your loop test condition will need to be changed. The strlen function would come in handy here.


I'll add one other comment, in case I misunderstood what you were trying to do. If the argv string is the name of a file, and you are trying to read the characters from the file and not just the characters of the argv string itself, then using fgetc or cin or getc or read would work, but you need to open the file first. If you want to use fgetc, then you specify the file stream pointer there, not the file name.
Last edited on
1
2
3
4
5
6
7
        int i=0;
        char c;      
        
        while (c = argv[2][i++])
        {
            printf("%c", c);
        }
I'm trying to read the text that is entered by user, which can be anything and of any length.

user would type:

./program_name numerical_key Users' message(which is to be encoded)

Thanks to your help I do have a 'do while' loop now which successfully grabs the characters from argv[2] one by one but...
The loop should execute a finite amount of times based on the length of the typed in text.
I'm using strlen() to get the length but it stops at the fist 'space' character and cuts off the rest of text.

I've tried to execute the loop simply till it encounters EOF and that didn't work.
It would spit out the text(with spaces REMOVED)and than a bunch of stuff like
my current path and some file listings followed by a segmentation fault.

I'm really confused here by what is happening with spaces. Why do they disappear?
According to the description of strlen() they shouldn't!
Last edited on
The code I gave does execute the correct number of times. It executes until the character c is equal to zero. That is, it finds the null terminator which marks the end of the string. (If you try to use strlen() you get the same result because that also looks for the null terminator).

Rather than changing the code, you simply need to enclose the parameter in quotes when you run the program.
./program_name numerical_key "Users message which is to be encoded"


Note, if the message contains spaces but is not enclosed in quotes, then each word will be treated as a separate parameter. You would have to check argc to see how many words there are, and read each parameter one after the other. The problem in this case is if the user types multiple spaces between words there would be no way of knowing that.
Last edited on
Thank you very much.
Very exhaustive and clear answer.
I understand that there is still more to learn about this topic but
now I have much clearer picture of it.
Topic archived. No new replies allowed.