/*
*
*
* Created on: 2014年9月3日
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
/*putc(c,stdout) 与putchar(c)的作用是一样的。
getc(stdin) 与getchar(c)的作用是一样的。
可以方便的将stdout或者stdin改为文件指针fp,实现了从文件的读取
和向文件的输出*/
// int a;
// a = getc(stdin);
// putc(a, stdout);
/*--------------------------------*/
/*创建文件test.txt,"w+"表示文件既可读又可写,如果文件不存在就创建之,
如果文件已经存在,就清空并重新开始写*/
FILE *fp;
fp = fopen("c:\\users\\arvin\\test.txt", "w+");
/*能否顺利创建文件的测试,exit(0)表示正常退出,exit(非零)表示异常退出。*/
if(fp == NULL) //必须是fp == NULL, 测试if(fp)似乎有问题啊!!
{
printf("Can not open the file");
exit(1);
}
int b;
b = getchar();
while (b !='\n')
{
putc(b,fp);
b = getchar();
}
rewind(fp);//文件指针返回第一个字符的位置
/*把写入test.txt中的文字打印到标准输出(屏幕)*/
b = getc(fp);
while (b != EOF)
{
putchar(b);
b = getc(fp);
}
if (fclose(fp) != 0)
printf("Error in closing files"); //别忘了,要关掉fp的链接。正常情况下fclose(fp)返回0;
return 0;
}
文件的输入输出 part1
最新推荐文章于 2022-07-17 14:33:12 发布