|
回复 #9 system888net 的帖子
其实那上面是我发错的程序!
抱歉哦!
正确的是这样的:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *in, *out;
char infile[20],outfile[20],ch,string[100],c;
int num = 0, word = 0, i = 0;
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("%s\n",infile);
printf("Enter the outfile name:\n");
scanf("%s",outfile);
printf("%s\n",outfile);
if ((in = fopen(infile,"rb"))==NULL)
{
printf("cannot open infile\n");
exit(-1);
}
if ((out=fopen(outfile,"wb"))==NULL)
{
printf("cannot open outfile\n");
exit(0);
}
for(i = 0; !feof(in); i++)
{
ch = fgetc(in);
printf("%c",ch);
fputc(ch,out);
string[i] = ch;
printf("%c\n",string[i]);
}
for (i = 0; (c = string[i]) != '\0'; i++)
{
if( c == ' ' && c == '\n' )
word = 0;
else if(word == 0)
{
word = 1;
num++;
}
}
printf("There are %d words in the %s\n",num,infile);
fclose(in);
fclose(out);
}
但结果怎么还是一样呢????
谢谢!
|