1

I know this is fairly basic, but I'm still stuck. So I have a function that needs to take in a variable n, so this is my main function

int main(int argc, char* argv){
  sort(argv[1]);
    }

And I'm calling the program like this:

    ./sort 4 <text.txt

But the number 4 doesnt get recognized or passed into the function. What am I doing wrong? I know that argv[0] should hold the name of program itself and each one from there on should hold the arguments.

skaffman
  • 398,947
  • 96
  • 818
  • 769
user1161080
  • 165
  • 1
  • 5
  • 11

3 Answers3

7

You should try to print them all.

#include <stdio.h>

int main(int argc, const char *argv[])
{
    int i = 0;
    for (; i < argc; ++i) {
        printf("argv[%d] = '%s'\n", i, argv[i]);
    }
    return 0;
}

Running that code with ./a.out 4 < /somefile gives me:

argv[0] = './a.out'
argv[1] = '4'

Eventually you'll have to remember that the '4' is a pointer to an array of characters, and you might have to parse it into an integer.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Rickard
  • 7,239
  • 1
  • 18
  • 11
  • I'm still confused, the code segment you posted shows the argument properly, but it still doesn't get passed into the function properly. Why is that? Why doesnt sort(argv[1]) work ? – user1161080 Feb 11 '12 at 17:57
  • @JonathanLeffler here `./a.out 4 < /somefile` should read the contents of `somefile` into `argv` right? – overexchange Jan 16 '17 at 23:59
  • @overexchange: No. If you want the contents of the file as arguments, you'd have to do something like `./a.out 4 $( – Jonathan Leffler Jan 17 '17 at 00:01
  • @JonathanLeffler I have records in `file.txt` each each record should be an element of `argv`. Currently am `fopen()`ing `file.txt` as shown [here](https://github.com/shamhub/Computing/blob/master/design2Code/sort.c) – overexchange Jan 17 '17 at 00:30
  • @overexchange: That gets very tricky very fast. Is a 'record' a line or part of a line or a collection of lines? How do you know when you've got an 'end of record'? Next problem will be making the shell split the arguments on the end of record. By default, the shell uses blank-or-tab-or-newline. To get the lines in a file split on newlines, you might use: `(IFS=$'\n'; printf '%s\n' $(cat file) )` where the outer parentheses create a sub-shell to protect you from changes to `IFS` inside the code. That preserves spaces — leading spaces, trailing spaces, multiple spaces — in the file's lines. – Jonathan Leffler Jan 17 '17 at 00:41
  • @overexchange: Please note that if you have a question to ask, you should ask it as a question. It isn't fair to those from whom you demand help to do so in comments where there are no rewards whatsoever for assisting you. – Jonathan Leffler Jan 17 '17 at 00:42
  • @JonathanLeffler Make sense, here is the [query](http://stackoverflow.com/questions/41687782/reading-a-file-using-argv). Please help me – overexchange Jan 17 '17 at 01:42
3

char *argv is not correct. You get passed an array of char* ("Strings"), so the correct way to declare main would be int main(int argc, char *argv[]) or equivalently int main(int argc, char **argv) (in the latter case the array-argument is effectively converted to a pointer to the first element of the array).

What you are retrieving in the current version of the code is the second char in the array of argument-pointers given to you by the environment reinterpreted as an array of characters, which is something else entirely.

fnl
  • 2,209
  • 19
  • 17
0

As described by other you know how to get all argument, but not the "<text.txt".

This is not an argument to your program, but to the shell. This is input redirection, meaning that the input of the file is coming to the program as if it come from stdin (the keyboard).

To prevent this, just call you program like this:

./sort 4 text.txt

I am not sure what the function "sort" is, that you are calling.

Jörg Beyer
  • 3,631
  • 21
  • 35