Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2508847
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2011-03-24 11:45:07

    创建一个后缀名为txt的文件,并向该文件中写入一个字符串,保存起来。再打开该文件,读出文件中内容。
    其实这个是使用C语言提供的读写文件的API的应用,多多练习应该就会用了。代码如下:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(int argc, char *argv[])
  4. {
  5.   FILE *fp;
  6.   char pathName[50], txt1[100] = {'\0'}, txt2[100] = {'\0'};
  7.   int fileLen;
  8.   printf("please type the path name of the file\n");
  9.   scanf("%s",pathName);
  10.   fp = fopen(pathName,"w");
  11.   printf("please input a string to this file\n");
  12.   scanf("%s",txt1);
  13.   fileLen = strlen(txt1);
  14.   fwrite(txt1, fileLen, 1, fp);
  15.   fclose(fp);
  16.   printf("the file has been saved\n");
  17.   printf("the content of the file: %s is \n",pathName);
  18.   fp = fopen(pathName, "r");
  19.   fread(txt2, fileLen, 1,fp);
  20.   printf("%s\n",txt2);
  21.   return 0;
  22. }
阅读(843) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~