1
votes

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.

1
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?Some programmer dude
Umm, why are you doing gcc main.c input.txt?Oliver Charlesworth
don't use gcc after compiling... you have executable file after it - run it: ./a.out input.txtvp_arth
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
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...Elias Van Ootegem

1 Answers

4
votes

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