69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
FILE *fp1;
char ch;
int a=0;
if ((fp1=fopen("C:\\wenjian/alpha.txt","a+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while (!feof(fp1))
{
ch=fgetc(fp1); //从fp1所指的文件中读取一个字符
if (ch !=EOF)
{
if (ch>='a'&&ch<='z')
{
ch -=32;
}
if (ch=='\n')
{
a++;
}
printf("%c",ch);
}
}
printf("\na=%d",a);
if (fclose(fp1)) //关闭文件
{
printf("Can not close the file!\n");
exit(0);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
FILE *fp1,*fp2;
char ch;
int a=0;
if ((fp1=fopen("alpha.txt","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
if((fp2=fopen("alpha.txt","a"))==NULL)
{
printf("打开文件dest.txt失败!\n");
exit(0);
}
while (!feof(fp1))
{
ch=fgetc(fp1); //从fp1所指的文件中读取一个字符
if (ch !=EOF)
{
if (ch>='a'&&ch<='z'&&ch!='\n')
{
ch -=32;
fputc(ch,fp2);
}
}
}
if (fclose(fp1)) //关闭文件
{
printf("Can not close the file!\n");
exit(0);
}
if (fclose(fp2)) //关闭文件
{
printf("Can not close the file!\n");
exit(0);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
FILE *fp1, *fp2;
int ch;
int a=0;
if ((fp1=fopen("d:alpha.txt", "r+"))==NULL)
{
printf("File1 open error!\n");
exit(0);
}
if ((fp2=fopen("d:temp.txt", "w"))==NULL)
{
printf("File2 open error!\n");
exit(0);
}
while ( !feof(fp1) )
{
if ( (ch= fgetc(fp1)) != EOF ) //从fp1所指的文件中读取一个字符
{
if ( ch>='a' && ch<='z' )
{ ch -= 'a'-'A';
}
if (ch=='\n')
{ a++;
}
fputc(ch, fp2); //写入文件2
}
}
printf("\na=%d",a);
freopen("d:/chensy/2019c/alpha.txt", "a+", fp1); //重新打开文件1
freopen("d:/chensy/2019c/temp.txt", "r",fp2); //重新打开文件2
while ( !feof(fp2) )
{
ch = fgetc(fp2); //从fp2所指的文件中读取一个字符
if ( ch != EOF )
{ fputc(ch, fp1); //追加到文件1中
}
}
if ( fclose(fp1) ) //关闭文件1
{
printf("Can not close the file1!\n");
exit(0);
}
if ( fclose(fp2) ) //关闭文件2
{
printf("Can not close the file2!\n");
exit(0);
}
return 0;
}