关于int main(int argc, char *argv[])的函数

来源:百度知道 编辑:UC知道 时间:2024/05/29 23:39:05
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int byte;
FILE * source;
int filect;

if (argc == 1)
{
printf("Usage: %s filename[s]\n", argv[0]);
exit(EXIT_FAILURE);
}

for (filect = 1; filect < argc; filect++)
{
if ((source = fopen(argv[filect], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[filect]);
continue;
}
while ((byte = getc(source)) != EOF)
{
putchar(byte);
}
if (fclose(source) != 0)
printf("Could not close file %s\n", argv[1]);
}

return 0;
}

以上是编写的程序,可以确认是正确的。 这样的程序,如何才能正常运行一下? 我曾想过做成执行文件,然后运行,但是

可执行文件比如叫xxx.exe 那就命令行运行呗

xxx.exe abc.txt

当然当前目录下得有这个 abc.txt

估计用的 运行平台不对,可以换个试试

这好像是个文件复制的程序,但我不喜欢用argv[filect]来表示名字,直接写个文件名字不好吗?
#include"stdio.h"
main()
{
FILE*in,*out;
in=fopen("10.bin","rb");
out=fopen("1.bin","wb");
while(!feof(in))
{
fputc(fgetc(in),out);
}

fclose(in);
fclose(out);
getch();
;
}这是我的文件复制,名字指定的

这个程序的那2个文件名需要通过参数方式传入
你生成执行程序后

开始-》运行-》
输入 “可执行程序 -参数”