C语言读写文件

1、读写文件,这种是把内容一欠copy到内存里。
#include <stdio.h>
#include <stdlib.h>
/**
*读文件
*/
int rFile(){
FILE *fp;
int flen;
char *p;
/* 以只读方式打开文件 */
if((fp = fopen ("e:\\1.txt","r"))==NULL)
{
printf("\nfile open error\n");
exit(0);
}
fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */
flen=ftell(fp); /* 得到文件大小 */
p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间 */
if(p==NULL){
fclose(fp);
return 0;
}
fseek(fp,0L,SEEK_SET); /* 定位到文件开头 */
fread(p,flen,1,fp); /* 一次性读取全部文件内容 */
p[flen]=0; /* 字符串结束标志 */
printf("%s",p);
fclose(fp);
free(p);
getch();
return 0;
}
/**
*写文件
*/
int wFile(){
FILE *stream;
stream = fopen("e:\\1.txt", "w+");
fprintf(stream, "hello world!");
printf("The file pointer is at byte \%ld\n", ftell(stream));
fclose(stream);
getch();
return 0;
}

int main(void){
/*rFile();*/
wFile();
return 0;
}


2、按行读。


#include <stdio.h>
#include <stdlib.h>

#define LineSize 80 /*定义存储一行字符数据的长度*/
#define Line 10
/**
*读取一行,并返回存放字符串的首地址。
*/
char *ReadData_(char *buff,FILE *fp){
return fgets(buff,LineSize,fp);
}

int main()
{

ReadFile();
/* int i=0;
for(i;i<Line;i++){//读n行
printf("%s\n",buff);
p=ReadData_(buff,fp);//读取下一行。
}
*/

return 0;
}
/**
*按行读文件。
*/
void ReadFile(){
FILE *fp=NULL;
char *buff,*p;
const char *filePath="E:/工行网银读不了U盾.txt";


if((fp=fopen(filePath,"r"))==NULL){//判断是否能打开指定文件。
printf("Conn't open File:[%s]",filePath);
exit(0);
}
buff=(char *)malloc(LineSize*sizeof(char));//给指针分配内存。

while(ReadData_(buff,fp)){//读取数据。直到读到文件结尾(也就是说,不能返回字符地址了。)
printf("%s\n",buff);
}
fclose(fp);
}





3、读二进制数据。

#include <stdio.h>
#include <stdlib.h>

/**
* function:二进制文件的复制。
* parameter:from 源文件路径
* :to 目标文件路径
* author:leson
* date:2011-12-5
*
*/
int copyFile(const char* from,const char* to){
FILE *in,*out;
int flen;
char *p;
/**
*读文件
*/
if((in = fopen (from,"rb"))==NULL){
printf("\nfile open error:1\n");
return 1;
}
fseek(in,0L,SEEK_END); /* 文件指针定位到文件末尾 */
flen=ftell(in); /* 得到文件大小 */
p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间--经典*/
if(p==NULL){
fclose(in);
return 2;
}
fseek(in,0L,SEEK_SET); /* 定位到文件开头 */
fread(p,flen,1,in); /* 一次性读取全部文件内容 */
p[flen]=0; /* 字符串结束标志 */
/*printf("%s",p);*/

/**
* 写文件
*/
if((out=fopen(to,"wb"))==NULL){
printf("\nFile open error:3");
return 3;
}
fwrite(p,flen,1,out);/*往文件里写*/
fclose(out);
fclose(in);
free(p); /*动态分配的内存一定要free*/
return 0;
}

int main(void){
int r=99;
if((r=copyFile("d:\\down.txt","d:\\up.txt"))!=0){
printf("%d",r);
}
getch();
return 0;

}



4、[url=http://note.youdao.com/share/?id=32e0a9f12484e5ad49ed8f67a9084c40&type=note]linux [/url]的三种文件
①:stdin(标准输入流)
②:stdout(标准输出流)
③:stderr(标准错误流)

这三种文件,在程序运行时默认提供。

#include <stdio.h>

int main(int argc, char const *argv[])
{
int input;
// scanf("这是封装标准输入流文件的形式后的;%d",&input);
fscanf(stdin,"%d",&input);

if (input<0){
// printf("这是封装fsprintf() 后的方式\n");
fprintf(stderr,"这是运用了标准错误流文件的形式。\n outerr=输入不能为负数。\n" );
return 1;
}else{
// printf("output=%d\n",input);
fprintf(stdout,"这是用了标准输出流文件的形式。\n output=%d\n",input);
}

int i=0;
for (i = 0; i < argc; ++i){
fprintf(stdout, "arguments counter:[%d];arguments vector:[%s];\n",i,argv[i]);
}

return 0;
}



小应用。
https://code.csdn.net/snippets/236105.js
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值