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.
main(int argc, const char * argv[])
I hope you mean you never used amain
function taking command line arguments? I.e. you have only usedint main(void)
before? – Some programmer dudegcc main.c input.txt
? – Oliver Charlesworth./a.out input.txt
– vp_arthgcc
does) with running your program (which is when your program'smain
function gets command-line arguments). – jamesdlinint argc
is the function argument that tells themain
function how many arguments your program received when it was started (like a commandls -l
. You can then find those arguments inchar **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