Thread: Question about reading a file through the command line.

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    9

    Question about reading a file through the command line.

    Hey guys. I'm currently working on making a program that is run through a GUI run through the command line. The program basically takes an app file and a boot file and runs it through a bunch of functions and generates a new outfile. Anyway I'm new to C and can't figure out how to code it so I can type the two file paths into the command line and read them into the function. Is it possible to do this within the "if else" statement?

    Code:
    int main(int argc, char *argv[])
    {
    const char * const SrcFilePath;
    const char * const SRecordPath;
    const char * const FopIspFilePath;
    
    int i = 0;
    
    for(i < argc; i++;)
    
    {
    
    if(0 == stricmp("Interrogator", argv[i]))
    {
    InterrogatorProtocol1553Flag = TRUE;
    }
    
    else if(0 == stricmp("SRecord", argv[i]))
    {
    Interrogator_OFP_Select = INT_OFP_DMV_183;
    CreateInterrogatorFlashImage(SrcFilePath, SRecordPath, FopIspFilePath);
    }
    
    else
    {
    fprintf(stderr, "Unknown parameter: %s", argv[i]);
    }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1

    Code:
    const char * const SrcFilePath;
    const char * const SRecordPath;
    const char * const FopIspFilePath;
    I applaud your attempt at const correctness here but the second const in the above variable declarations means that the pointer itself is constant and cannot be changed outside of the point of declaration. Visual Studio 2010 won't even compile this, as written, it gives an error with following message:
    Code:
    1>test2.cpp(8): error C2734: 'SrcFilePath' : const object must be initialized if not extern
    1>test2.cpp(9): error C2734: 'SRecordPath' : const object must be initialized if not extern
    1>test2.cpp(10): error C2734: 'FopIspFilePath' : const object must be initialized if not extern
    If you want to declare the variable here in main but assign something to them later on in the course of the program then you must at least remove the second const as follows:
    Code:
    const char * SrcFilePath;
    const char * SRecordPath;
    const char * FopIspFilePath;



    #2
    Code:
    int i = 0;
    
    for(i < argc; i++;)
    What you probably meant here is"
    Code:
    int i = 0;
    
    for(; i < argc; i++)
    But, you can probably skip the first argument and start i at 1 instead of 0.




    #3 On to your question, when you run the program from the command prompt you can simply add the arguments after the program name. If you are talking about running this for testing purposes within an IDE, then we'd have to know which IDE and could then provide instruction as to how the project would be configured to provide some values as if the program had been run from the command prompt with some form of default command line parameters.

    Beyond handling the argv elements as you are doing there is nothing you need to do specifically to code to allow command-line parameters to be entered. That's handled at a scope outside your program.

    Any question about if/else statements has nothing to do with allowing one to enter in arguments at the command line but rather what to do with the arguments (how to process the input) after they've already been entered.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you want to declare the variable here in main but assign something to them later on in the course of the program then you must at least remove the second const as follows
    Well this later on should be before line 21 where the vars are passed to the function.

    Probably they should point to some argv[] members if paths are given in the command line
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    You need to make SrcFilePath, SRecordPath and FopIspFilePath into buffers, of size MAX_PATH. (Or just use 1024). So char SrcFilePath[1024] etc.
    Then in the else, print out a prompt to get the user to enter the source file name, then call fgets() to read the result. Remove the trailing newline (you can use strlen to get the length of the string, it will be at the position N-1). Do that for all the file paths.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading input from command line
    By ueg1990 in forum C Programming
    Replies: 4
    Last Post: 01-26-2012, 11:29 PM
  2. Reading a file from command line
    By Enig.Ma in forum C Programming
    Replies: 2
    Last Post: 09-04-2010, 07:21 PM
  3. reading command line or text file...expression tree (sort of)
    By mathwork orange in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2008, 09:52 AM
  4. reading in command line arguments from a file?
    By g1i7ch in forum C Programming
    Replies: 30
    Last Post: 06-22-2006, 01:35 PM
  5. Need help with reading numbers from the command line
    By Nterpol in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 01:40 AM

Tags for this Thread