天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

文件的读写

Posted on 2012-03-12 16:53 hoshelly 阅读(164) 评论(0)  编辑 收藏 引用 所属分类: C
创建一个后缀名为txt的文件,并向该文件写入一个字符串,保存起来,再打开文件,读出文件中的内容。
代码如下:
#include<stdio.h>
#include
<string.h>
int main()
{
    FILE 
*fp;
    
char pathName[20],txt1[100]={'\0'},txt2[20]={'\0'};
    
int fileLen;
    printf(
"Please type the path name of the file\n");
    scanf(
"%s",pathName);
    fp
=fopen(pathName,"w");
    printf(
"Please input a string to this file\n");
    scanf(
"%s",txt1);
    fileLen
=strlen(txt1);
    fwrite(txt1,fileLen,
1,fp);
    fclose(fp);
    printf(
"The file has been saved\n");
    printf(
"The content of teh file: %s is\n",pathName);
    fp
=fopen(pathName,"r");
    fread(txt2,fileLen,
1,fp);
    printf(
"%s\n",txt2);
    
return 0;
}



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理