Shamal Karunarathne
Shamal Karunarathne

Reputation: 1849

How to read command line inputs using a C program


How to read command line inputs using a C program.
by command line inputs, I don't mean command line arguments!!

example: * I have a text file 'inputfile.txt' with few lines of names.
* Assume my program name is names.exe.
* I have to run the program using windows command line using following command:
c:>names.exe < inputfile.txt

Thanks.

Upvotes: 0

Views: 1158

Answers (4)

krousey
krousey

Reputation: 1798

That instructs the system to replace your stdin file descriptor with a file descriptor to inputfile.txt. So just read from stdin like normal.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490128

That's redirecting standard input, so in your program you don't do anything special at all -- you just read from standard input and write to standard output. If the user has redirected those, so be it.

Upvotes: 3

Carl Norum
Carl Norum

Reputation: 224944

For your example, the input is going to come on the standard input. Just use fread or fgets.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798666

Read from the stdin FILE*.

Upvotes: 4

Related Questions