如果我进入命令行 C: 我的程序我的文件.txt
如何在我的程序中使用 myfile。我是否必须扫描它或者是否有任意访问它的方式。
我的问题是如何在我的程序中使用 myfile.txt。
int
main(){
/* So in this area how do I access the myfile.txt
to then be able to read from it./*
最佳答案
您可以使用 int main(int argc, char **argv)
作为主函数。
argc
- 将是您程序的输入参数的计数。
argv
- 将是指向所有输入参数的指针。
因此,如果您输入 C:\myprogram myfile.txt
来运行您的程序:
argc
将为 2argv[0]
将是myprogram
。argv[1]
将是myfile.txt
。
更多详情 can be found here
读取文件:
FILE *f = fopen(argv[1], "r");//"r"表示读取
要以其他模式打开文件,read this .
关于命令行参数,读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16869467/