//按输入列表在屏幕上显示文件内容
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int i = 0;
char ch;
printf(“argc is %d\n”,argc);
printf(“argv[0] is %s\n”,argv[0]);
while(i<argc)
{
if((fp = fopen(argv[i],”r”))==NULL)
{
printf(“can not open %s”,argv[i]);
exit(1);
}
while((ch = getc(fp))!=EOF)
{
putc(ch,stdout);
}
i++;
fp = NULL; //重置fp指针为NULL以便打开下一个文件
}
return 0;
}
高手们问一下输出怎么会有乱码MZ
cmd输出如下:
\my C code\practice\uint 13>13.11.4.exe 13.11.4.c 13.11.3.c
argc is 3
argv[0] is 13.11.4.exe
MZ (这里为什么还显示了MZ)
//按输入列表在屏幕上显示文件内容
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int i = 0;
char ch;
printf(“argc is %d\n”,argc);
printf(“argv[0] is %s\n”,argv[0]);
while(i<argc)
{
if((fp = fopen(argv[i],”r”))==NULL)
{
printf(“can not open %s”,argv[i]);
exit(1);
}
while((ch = getc(fp))!=EOF)
{
putc(ch,stdout);
}
i++;
fp = NULL; //重置fp指针为NULL以便打开下一个文件
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 256
int main(void)
{
FILE *s,*t; //定义输入文件指针和输出文件指针
char source[MAX]; //存放源文件名
char target[MAX]; //存方目的文件名
int ch;
printf(“please enter your source filename!\n”);
scanf(“%s”,source);
if((s = fopen(source,”r”))==NULL)
{
printf(“can not open %s file”,source);
exit(1);
}
printf(“please enter your target filename!\n”);
scanf(“%s”,target);
if((t = fopen(target,”w”))==NULL)
{
printf(“can not open %s file”,target);
exit(1);
}
while((ch=toupper(getc(s)))!=EOF)
{
putc(ch,t);
putc(ch,stdout);
}
return 0;
}
F:\my C code\practice\uint 13>
40
argv[0]是可执行文件本身,就是那个13.11.4.exe,DOS的exe文件不是文本文件,内容按字符格式输出就是乱码,但它是以十六进制4D5A开头的,按ASCII码解释就是MZ