1

I've never used,

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

For most programs, I usually just compile in terminal (using mac) using two separate C files, example…

gcc functions.c main.c

But now I need to use int main(int argc, const char * argv[])… I just don't know if I'm using it correctly. Heres some code…

I compile in the command line doing…

gcc main.c input.txt

terminal tells me…

ld: file too small for architecture x86_64

collect2: ld returned 1 exit status

NOTE my functions work (i tested without using file input) and are in main.c also… i just didn't include them in this post. Also, node is just a basic node struct to a linked list.

int main(int argc, const char * argv[])
{
FILE *input;


input = fopen(argv[1], "r");


node *list = malloc(sizeof(node));
char *string = malloc(sizeof(char)*1023);

fscanf(input, "%s", string);

//convert a string to linked list
list= sTol(string);

//print the linked list
printList(list);

return 0;

} // end main()

Am i completely wrong? the input simply contains one line that says 'hello'. All I'm trying to do is read that into my program and print it just to verify I'm reading my input correctly.

8
  • When you say you've never used main(int argc, const char * argv[]) I hope you mean you never used a main function taking command line arguments? I.e. you have only used int main(void) before? Commented Mar 30, 2014 at 18:21
  • 4
    Umm, why are you doing gcc main.c input.txt? Commented Mar 30, 2014 at 18:21
  • don't use gcc after compiling... you have executable file after it - run it: ./a.out input.txt
    – vp_arth
    Commented Mar 30, 2014 at 18:21
  • 1
    It sounds like you're confusing compiling your program (which is what gcc does) with running your program (which is when your program's main function gets command-line arguments).
    – jamesdlin
    Commented Mar 30, 2014 at 18:22
  • int argc is the function argument that tells the main function how many arguments your program received when it was started (like a command ls -l. You can then find those arguments in char **argv. The names seem abstract, but just read them as saying ARGumentCount and ARGumentValues, hence argc and argv. Passing a text file to a compile isn't going to work... Commented Mar 30, 2014 at 18:25

1 Answer 1

5

This is not like a perl script or shell script, where you run

perl main.pl input.txt

With a compiled language like C, you first compile the program into an executable

gcc main.c -o myprogram

and then run the executable with the input file

./myprogram input.txt

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.