简体   繁体   English

int main(int argc, const char * argv[]) AND 文件输入

[英]int main(int argc, const char * argv[]) AND file input

I've never used,我没用过,

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

For most programs, I usually just compile in terminal (using mac) using two separate C files, example…对于大多数程序,我通常只是使用两个单独的 C 文件在终端(使用 mac)中编译,例如……

gcc functions.c main.c gcc 函数.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.但是现在我需要使用 int main(int argc, const char * argv[]) ......我只是不知道我是否正确使用它。 Heres some code…继承人一些代码...

I compile in the command line doing…我在命令行中编译...

gcc main.c input.txt gcc main.c input.txt

terminal tells me…终端告诉我……

ld: file too small for architecture x86_64 ld:对于架构 x86_64 文件太小

collect2: ld returned 1 exit status collect2: ld 返回 1 个退出状态

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.注意我的函数可以工作(我在不使用文件输入的情况下进行了测试)并且也在 main.c 中......我只是没有在这篇文章中包含它们。 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.我要做的就是将其读入我的程序并打印出来,以验证我是否正确读取了我的输入。

This is not like a perl script or shell script, where you run这不像你运行的 perl 脚本或 shell 脚本

perl main.pl input.txt

With a compiled language like C, you first compile the program into an executable使用像 C 这样的编译语言,您首先将程序编译为可执行文件

gcc main.c -o myprogram

and then run the executable with the input file然后使用输入文件运行可执行文件

./myprogram input.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以这种格式将argv变量传递给main的结果main(int argc,char const * argv) - result of passing argv variable to main in this format main( int argc, char const * argv ) 使用main(int argc,char * argv [])的问题 - Problems using main(int argc,char *argv[]) 用常量参数定义main(const int argc,const char * const argv [])? - Defining main with constant arguments (const int argc, const char * const argv[])? 函数main中的参数argv(int argc,char * argv []) - The parameter argv in the function main(int argc, char *argv[]) 如何在主函数中实现int argc和char * argv []? - How do I implement int argc and char *argv[] in the main function? 使用main(int argc,char ** argv){}作为函数重写程序 - Rewrite a program with main(int argc,char **argv) {} as a function 在 C 中以编程方式调用 main(int argc, char **argv) - Programmatically calling main(int argc, char **argv) in C 这个声明来自哪里:main _2a((argc,argv),int argc,char * argv []) - Where does this declaration come from: main _2a((argc,argv), int argc, char * argv[]) 定义“int main (void)”和“int main (int argc, char *argv[])”有什么区别,什么时候使用? - What is the difference between the definitions “int main (void)” and “int main (int argc, char *argv[])” and when to use which? 是否必须编写int main(int argc,char * argv []),不能使用其他名称而不是argc和* argv []? - Is it compulsory to write int main(int argc, char *argv[]), can't use some other names instead of argc and *argv[]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM