Thread: Inputting data in command line.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Inputting data in command line.

    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!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Kampala, Uganda
    Posts
    1

    Inputting data on command line

    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mimatech ug View Post
    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.
    You don't need the quotes and the space after the drive letter will cause that to fail...
    Like this .... c:\myprogram file.dat

    On Windows the only time you need quotes around a filename is when it has spaces in it...
    as in c:\myprogram "c:\my documents\file.dat"

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    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;
    }
    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.

    argc tells how many command-line arguments were entered. argv[] is an array of strings, each containing a command-line argument. Hope this helps.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-31-2011, 04:50 PM
  2. Inputting Data File Using Pointers
    By jl864405 in forum C Programming
    Replies: 8
    Last Post: 06-04-2009, 05:52 PM
  3. inputting data in a struct
    By raja9911 in forum C Programming
    Replies: 5
    Last Post: 02-02-2006, 04:15 AM
  4. hello, brand new here, need help with inputting data
    By Chrisab508 in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2003, 11:18 AM
  5. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM