1

I have the following section of code in C:

int main(int argc, char *argv[]) 
{
    char *input;
    int lines;
    int p;


    input = (char*)malloc(10);
    input = argv[0];
    for(p=0;p<10;p++)
    {
        printf("%c",input[p]);
    }

On my Unix system I make the following call:

./program_name.exe < inputfile

where inputfile is a file that contains the following: 000000010Z

The output I receive to the previous commands is:

./program_

What am I missing?

4
  • If the goal is to read stdin, you may want to read about getc(), fgets() or scanf(). Commented Sep 19, 2013 at 23:19
  • 3
    the malloc is useless, you aren't using that allocated buffer, the < inputfile isn't a parameter to your program, its redirecting the stdin into your program Commented Sep 19, 2013 at 23:20
  • 1
    argv[0] is the program name. argv[1] is the first command line parameter. If you redirect a file into stdin you either need to open stdin as a file inside your program and read from it (fopen, fgets, fclose) or read ketstrokes (getchar, gets) which will return the same thing. Commented Sep 19, 2013 at 23:20
  • It looks like you're missing "name.exe". Try looping to 18 instead of 10! Commented Sep 19, 2013 at 23:27

1 Answer 1

1

argv[0] holds name of program being executed. And that's the only thing you print.

What you're trying to do is to read from your file as it was stdin. But you still have to read it. Use getchar(), or any other function that reads input.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, thats it. Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.