#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
int a=0;
FILE *pfile=NULL;
char filename[20];
printf("Please input a filename:");
gets(filename);/*这里输入文件名时,最好在末尾加上.txt 方便退出程序后查看*/
if(!(pfile=fopen(filename,"w")))
{
printf("I can't open the file!");
exit(1);
}
printf("enter the char\n");
ch=getchar();
while(ch!='q') /*用q来
跳出循环比较方便*/
{
fputc(toupper(ch),pfile);
putchar(toupper(ch));/*不明白这里为什么不是输入一个字符就立刻输出而是等到按下回车才全部一起输出!!! */
scanf("%c",&ch); /*经测试,输入一个字符输出对应的大写*/
}
fclose(pfile);
pfile=fopen(filename,"r");
rewind(pfile);{a++;}/*为什么这里用rewind()回到文件的 开头后运行是错的*/
printf("%d\n\n",a); /*这里的a只是测试之用,可知这里没有错误*/
ch=fgetc(pfile);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(pfile);
}
printf("the end of programing!\nenter any key to continue...");
getch();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
代码如下:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
FILE *pfile=NULL;
char filename[20];
printf("Please input a filename:");
gets(filename);
if(!(pfile=fopen(filename,"w")))
{
printf("I can't open the file!");
exit(1);
}
printf("enter the char\n");
ch=getchar();
while(ch!='q')
{
fputc(toupper(ch),pfile);
putchar(toupper(ch));
scanf("%c",&ch);
}
fclose(pfile);
pfile=fopen(filename,"r");
rewind(pfile);
ch=fgetc(pfile);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(pfile);
}
printf("the end of programing!\nenter any key to continue...");
getch();
}