-3

I've to run a program sample sample-code.cpp. It is taking a string like -create, 2 file names: one as input input.txt and one as output filename output.idxas parameters and one integer 10.

I should be able to run it from command line (mac Terminal/UNIX) using:

$sample-code -create input.txt output.txt 10

This is a snippet of main() code:

int main(int argc, char* argv[])
{

    // Creating New File
    if (strcmp(argv[1],"-create") == 0)
    {
        KeyFieldMax = atoi(argv[2]);
        InputFileName = argv[3];
        IndexFileName = argv[4];

when I type this command $sample-code -create input.txt output.txt 10 I get this error :

-bash: sample-code: command not found.

6
  • What exactly is the question or problem? What you have should work, though it looks like it's expecting the integer before the filenames. Commented Nov 14, 2016 at 22:17
  • 1
    KeyFieldMax = atoi(argv[2]); did you miss that in the command lin arguments? Commented Nov 14, 2016 at 22:18
  • What about using getopt or something like it? Argument parsing is surprisingly tricky. Commented Nov 14, 2016 at 22:18
  • when I type this command $sample-code -create input.txt output.txt 10 I get this error : -bash: sample-code: command not found. Maybe the program is not compiled. Commented Nov 14, 2016 at 22:26
  • @Dungeoun Suspected you are in the directory the sample-code was build to, just type ./sample-code. Commented Nov 14, 2016 at 22:32

1 Answer 1

1

bash: sample-code: command not found is telling you that sample-code is not in bash's command path. if you're in the directory you build the program in, type ./sample-code to run it.

Your error Segmentation fault: 11 is an out of bounds array. You need to run a sanity check on the arguments to verify they are actually there before calling their position in the array:

if (argc < 4)
{
    fprintf(stderr, "Urk! Not enough arguments!");
    return 1;
}
else
{
    KeyFieldMax = atoi(argv[2]);
    InputFileName = argv[3];
    IndexFileName = argv[4];
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

I checked it. My order of inputs was wrong and I was missing some arguments.

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.