ITeye博客

C语言读写文件

ileson · 2011-05-31 23:01 · C/C++ · 编程语言 · Markdown

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、linux 的三种文件 ①: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)

暂无评论。

评论功能维护中,当前仅展示历史评论。