通过一下例子认识一下argc, argv:
代码如下:
#include <iostream> using namespace std; int main(int argc, char * argv[]) { char filename[80]; if(argc ==1 || argc > 2) { cout << "input the filename you want to edit: " << endl; cin >> filename; } if(argc == 2) cout << "edit..." << argv[1] << endl; }
编译生成edit可执行文件 g++ edit.cpp -o edit
1. 若 ./edit,则此时argc为1, argv[0]为edit,则会执行if(argc==1 || argc > 2),结果为输出input the filename you want to edit:
2.若./edit 1.txt, 则此时argc为2, argv[0] 为edit, argv[1]为1.txt,则会执行if(argc == 2) , 结果为输出edit...
时间: 2024-10-23 18:10:08