[英]result of passing argv variable to main in this format main( int argc, char const * argv )
[英]int main(int argc, const char * argv[]) AND file input
我沒用過,
int main(int argc, const char * argv[])
對於大多數程序,我通常只是使用兩個單獨的 C 文件在終端(使用 mac)中編譯,例如……
gcc 函數.c main.c
但是現在我需要使用 int main(int argc, const char * argv[]) ......我只是不知道我是否正確使用它。 繼承人一些代碼...
我在命令行中編譯...
gcc main.c input.txt
終端告訴我……
ld:對於架構 x86_64 文件太小
collect2: ld 返回 1 個退出狀態
注意我的函數可以工作(我在不使用文件輸入的情況下進行了測試)並且也在 main.c 中......我只是沒有在這篇文章中包含它們。 此外,節點只是鏈接列表的基本節點結構。
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()
我完全錯了嗎? 輸入只包含一行“你好”。 我要做的就是將其讀入我的程序並打印出來,以驗證我是否正確讀取了我的輸入。
這不像你運行的 perl 腳本或 shell 腳本
perl main.pl input.txt
使用像 C 這樣的編譯語言,您首先將程序編譯為可執行文件
gcc main.c -o myprogram
然后使用輸入文件運行可執行文件
./myprogram input.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.