Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > Read from a file and print to shell
【标  题】:Read from a file and print to shell
【关键字】:Read,from,file,and,print,to,shell
【来  源】:http://blog.chinaunix.net/article.php?articleId=54848&blogId=11234

Read from a file and print to shell

Your Ad Here

Test how to read a file and print to shell.and next I extended the program to a copy program.

#include


char buff[300];
void readFromFile(){
  FILE *fp;
  if((fp=fopen("5.03.c","rb"))==NULL){
   printf("fopen error");
  }
  int i;
  while(!feof(fp)){
       fread(buff,sizeof(struct stu),1,fp);
      printf("%s",buff);
      memset(buff,0,300);
  }
  if(fclose(fp)!=0){
    printf("fclose error");
  }
  printf("\n");

}
int main(){
 readFromFile();
}

//下面一个文件复制函数,其中需要注意的地方已经指出:)

#include

#define SIZE 4


char buff[300];
void cpFile(char *from,char *to){
  FILE *fromfp,*tofp;
 
  if((fromfp=fopen(from,"rb"))==NULL){
   printf("fopen error");
  }

  if((tofp=fopen(to,"wb"))==NULL){
   printf("fopen error");
  }
  int i;
  while(!feof(fromfp)){
       int len=fread(buff,sizeof(char),sizeof(buff),fromfp);
       //int len=strlen(buff);//控制实际读到buff中的内容 把它写到文件中 这里对二进制文件不好使:) 这样也不能很好工作 后来采用了上面的方法;
      //fwrite(buff,sizeof(buff),1,tofp);
      fwrite(buff,len,1,tofp);
      memset(buff,0,300);
  }
  if(fclose(fromfp)!=0){
    printf("fclose error");
  }
   if(fclose(tofp)!=0){
    printf("fclose error");
  }

}
int main(int arg,char *arv[]){
 
 if(arg!=3){
  printf(" 参数个数不够,需要两个 源文件,目的文件\n");
  return;
 }
 cpFile(arv[1],arv[2]);
}

C中浮点精度带来的误差:【上一篇】
Linux下用fread,fwrite进行文件读写笔记:【下一篇】
【相关文章】
  • printf() 函数
  • Named Device和Protocol Device的区别
  • getopt()用法
  • boost::thread::condition 原理篇(上)
  • Makefile(GNU)
  • Embedding Python in Multi-Threaded C/C++ Applicat
  • RubyScript2Exe, EEE(W) and AllInOneRuby
  • unix shell 脚本 (最简单的一个)
  • tutorial on pack and unpack
  • [Perl]实用性很强的模块Getopt::Std和Getopt::Long
  • 【随机文章】
  • 用ASP实现文档资料管理
  • 测试流程
  • 传奇3G 挑战职业极限
  • windows 消息大全
  • redhat9.0 文件系统挂载
  • 互联速度慢?网卡要设置!
  • Painter 8 教程:铅笔素描画(1)
  • 年轻的中国软件业有赶超的潜力
  • foxpro 刷新(Refreshing) 离线视图
  • 用指针处理C语言中不定数目的函数参数
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.