69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<ctype.h> //在vc6.0运行,为什么没有这个头文件toupper()函数还可以继续使用
#include<stdlib.h> //exit()
int main()
{
FILE *sourcef,*aimf;
int ch;
int count = 0;
int flag = 0;
char *argv[2]; //为什么将这里的二维数组改成一维指针数组( 即 *argv[2]),后面程序不变,就会出现运行错误?
//在编译时出现warning C4700: local variable 'argv' used without having been initialized
while(flag == 0 )
{
puts("input source file name:");
gets(argv[0]);
puts("input aim file name:");
gets(argv[1]);
if((sourcef = fopen(argv[0],"r")) == NULL) //open two file
{
printf("open %s error.\n",argv[0]);
continue;
}
if((aimf = fopen(argv[1],"w")) == NULL)
{
printf("open %s error.\n",argv[1]);
continue;
}
flag = 1;
}
//copy file
while((ch = getc(sourcef)) != EOF)
{
putc(toupper(ch),aimf);
count++;
}
//judging EOF
if(feof(sourcef) == 0)
{
fprintf(stdout,"error in reading file %s \n.",argv[0]);
exit (1);
}
if(ferror(sourcef) != 0)
{
fprintf(stdout,"error in writing file %s \n.",argv[0]);
exit(1);
}
fputs("copy successfuly\n,",stdout);
fprintf(stdout,"file %s has %d characters.\n",argv[0],count);
fclose(sourcef);
fclose(aimf);
return 0;
}
#include<ctype.h> //在vc6.0运行,为什么没有这个头文件toupper()函数还可以继续使用
while(flag == 0 )
{
puts("input source file name:");
gets(argv[0]);//这里argv[0]没有分配内存空间是个野指针,二维数组会自动分配空间
puts("input aim file name:");
gets(argv[1]);//同上
if((sourcef = fopen(argv[0],"r")) == NULL) //open two file
{
printf("open %s error.\n",argv[0]);
continue;
}
if((aimf = fopen(argv[1],"w")) == NULL)
{
printf("open %s error.\n",argv[1]);
continue;
}
flag = 1;
}
char *argv[2]; //为什么将这里的二维数组改成一维指针数组( 即 *argv[2]),后面程序不变,就会出现运行错误?
argv[0] = (char*)malloc(sizeof(char)*128);
argv[1] = (char*)malloc(sizeof(char)*128);