Hi
I want to create a program that would read data from file data1.dat and would be run by typing this into the command line:
$ program.tsk data1.dat
how would I go about doing this?
I appreciate your help!
Hi
I want to create a program that would read data from file data1.dat and would be run by typing this into the command line:
$ program.tsk data1.dat
how would I go about doing this?
I appreciate your help!
Do you know anything at all about C and File I/O?
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
When executing a program on command line, you start with the program name, a space and then whatever arguments you like.
C:\ myprogram.exe "file.dat"
So your program will take "file.dat" as a string argument and you can process it as required.
argv[0] is the string entered into the command prompt to start the program. So if you type prog.exe into the prompt (assuming this compiled program's name is prog.exe), you will see the usage message. argv[1] is the first command-line argument. So if you type prog.exe file.txt into the prompt, the program will open file.txt for reading, and then you can use fscanf and such.Code:#include <stdio.h> int main(int argc, char *argv[]) { if (!argv[1]) { printf("USAGE: %s filename\n", argv[0]); return 1; } FILE *fp = fopen(argv[1], "r"); /* more code here... */ return 0; }
argc tells how many command-line arguments were entered. argv[] is an array of strings, each containing a command-line argument. Hope this helps.
Originally Posted by The Jargon File