您的位置:首页 > 编程语言 > C语言/C++

c语言文件操作

2016-06-13 18:30 603 查看
#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include "string.h"

#include <malloc.h>

#include <stdio.h>

#include <stdlib.h>

#define i2a(j)  if( j>=0 && j<=9 ){  \

                    j=j+'0';       \

                }else{               \

                    j = j-0x0A+'A'; \

                }                    

//数字转换到字符

#define a2i(j)  if( j>='0' && j<='9' ){  \

                    j=j-'0';       \

                }else{               \

                    j = j+0x0A-'A'; \

                }    

//字符转换到数字

//r+ 具有读写属性,从文件头开始写,保留原文件中没有被覆盖的内容

//w+ 具有读写属性,写的时候如果文件存在,会被清空,从头开始写

//r  打开只读文件,该文件必须存在。 

//r+ 打开可读写的文件,该文件必须存在。 

//w  打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。 

//w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 

//a  以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。 

//a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 

//上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,

//加入b字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX系统,包含Linux都会忽略该字符

int read_char_cnt;

int read_char_off;

int write_char_cnt;

int write_char_off;

char *read_cancel_str;

char *read_match_str;

char *write_match_str;

char *parse_file_name_txt;

char *parse_file_name_bin;

int file_size(char* filename)  

{  

    struct stat statbuf;  

    stat(filename,&statbuf);  

    int size=statbuf.st_size;  

  

    return size;  

}

//从没有0x的文本文件到有0x的文本文件

int main_add_0x( void )

{  

    int  len;
int tmp_strlen;

    int  c,j,i;

    char line[4096];

    char tmpbuf[1024];

    FILE *fp;

    FILE *fp_temp;

    int ret,filesize,index;

    char *p;

    char kh,kl;
const char *src0 = "const u8 buf[XXXX] = {\r\n";
const char *src1 = "};\r\n";

int num = 9;

    char str[64];

    fp = fopen("testadd.txt", "r+");

    //以普通的方式打开文件

    if( fp == NULL ){

        printf("%s is not exist.\n", parse_file_name_txt ); 

        return -1;

    }

    memset(line,0x00,1024);

    

    j = 0x00;  

    fp_temp = fopen("testadd_result.txt", "w+");

    if( fp_temp == NULL ){

        printf("2.file is not exist.\n" ); 

        return -1;

    }

    //seek to the beginning of the file

    fseek(fp, 0, SEEK_SET);  

    fseek(fp_temp, 0, SEEK_SET);

    len = 0;

    //filesize = file_size("testadd.txt");
//printf("filesize = %d.\n", filesize ); 

fputs(src0, fp_temp);
index = 0x00;
while(1){

      c = fgetc(fp);

      if( feof(fp) ){

          //check for EOF 

          break;  

      }
 if( c == ' ' ){         

          continue;         
 }

 if( c== '\r'){         

          continue;         
 } 

      if(c== '\n'){         

          continue;         
 } 
 

      //printf("c = %x.\n", c ); 

      line[0]='0';

      line[1]='x';

      

      

      line[2] = c;
 c = fgetc(fp);
 

      line[3] = c;

      

      line[4]=',';
 //加入,

      line[5]=' ';

      //加入空格

 if( len%16 == 0x00 ){
   sprintf(str, "    ");
 fputs(str, fp_temp);
 }

      ++len;
 index++;

                                   

      if( len%16 == 0x00 ){

          fwrite( &line[0], 5, 1, fp_temp );
 //每16字节换行
   sprintf(str, "\r\n");
 fputs(str, fp_temp);

      }else{

          fwrite( &line[0], 6, 1, fp_temp );
     //每个字符写一次文件
 }

    }

                                                                
sprintf(str, "const u8 buf[%4d] = {\r\n", index);

    fseek(fp_temp, 0L, SEEK_SET);
fputs(str, fp_temp);
//覆盖开头位置

if( index%16 != 0x00 ){
fseek(fp_temp, -1, SEEK_END);

        sprintf(str, "\r\n");
fputs(str, fp_temp);
}else{

        fseek(fp_temp, 0L, SEEK_END);
}
fputs(src1, fp_temp);

    fclose(fp);

    fclose(fp_temp);

    //清除工作         

}

//文本文件到bin文件 根据匹配条件过滤

int main_r( void )

{  

    int  len;
int tmp_strlen;

    int  c,j,i;

    char line[4096];

    char tmpbuf[1024];

    FILE *fp;

    FILE *fp_temp;

    int ret;

    char *p;

    char kh,kl;

    fp = fopen(parse_file_name_txt, "r+");

    //以普通的方式打开文件

    if( fp == NULL ){

        printf("%s is not exist.\n", parse_file_name_txt ); 

        return -1;

    }

    memset(line,0x00,1024);

    

    j = 0x00;  

    fp_temp = fopen("parse_r.bin", "w+");

    if( fp_temp == NULL ){

        printf("2.file is not exist.\n" ); 

        return -1;

    }

    //seek to the beginning of the file

    fseek(fp, 0, SEEK_SET);  

    fseek(fp_temp, 0, SEEK_SET);

    len = 0;

    while( 1 ){
 p = fgets( line, sizeof(line), fp );
 //从输入文件读取一行字符串

      if( p == NULL ){

          //check for EOF 

          break;  

      }

      if( strncmp( read_match_str, line, strlen(read_match_str) ) == 0x00 ){

          ;

      }else{

          continue;
 }

      

      //printf("%s", line ); 

      //printf("strlen = %d.\n", strlen(line) );

      tmp_strlen = strlen(line);

      //i = 19;
 i = read_char_off;

      j = 0;

      while( 1 ){

      if( i >= tmp_strlen ){

         break;

      }                                                      

        if( line[i] == '\r' || line[i] == '\n' ){                       

      //printf("%d %x\n", i, line[i]);

         break;

      }

      if( line[i] == ' ' ){

      i++;
//忽略空格字符

         continue;

      }

     

      //printf("%d %x\n", i, line[i]); 

      kh = line[i];

      kl = line[i+1];

      a2i(kh);

      a2i(kl);

      tmpbuf[j] = (kh<<4) | (kl<<0);
//提取2个连续的ascll字符合并为一个16进制数

      //printf("%d %x\n", j, tmpbuf[j]);

      j++; 
//16进制数索引

      i = i + 2;
//ascll字符索引

      }
 if( j ){

          fwrite( &tmpbuf[0], read_char_cnt, 1, fp_temp );

          //把16进制数写入bin文件
 } 

      //break; 

    }

    fclose(fp);

    fclose(fp_temp);

    //清除工作         

}

//文本文件到bin文件 根据匹配条件过滤

int main_w( void )

{  

    int  len;
int tmp_strlen;

    int  c,j,i;

    char line[4096];

    char tmpbuf[1024];

    FILE *fp;

    FILE *fp_temp;

    int ret;

    char *p;

    char kh,kl;
char *parse_file_name;
parse_file_name = parse_file_name_txt;

    fp = fopen(parse_file_name, "r+");

    //以普通的方式打开文件

    if( fp == NULL ){

        printf("%s is not exist.\n", parse_file_name ); 

        return -1;

    }

    memset(line, 0x00, sizeof(line) );

    j = 0x00;  

    fp_temp = fopen("parse_w.bin", "wb+");

    //二进制方式

    if( fp_temp == NULL ){

        printf("test_w.bin is not open success.\n" ); 

        return -1;

    }

    //flush(fp);

    //seek to the beginning of the file

    fseek(fp, 0, SEEK_SET);  

    fseek(fp_temp, 0, SEEK_SET);

    len = 0;

    

    while( 1 ){
 p = fgets( line, sizeof(line), fp );
 //从输入文件读取一行字符串

      if( p == NULL ){

          //check for EOF 

          break;  

      }

      
 if( strncmp( write_match_str, line, strlen(write_match_str) ) == 0x00 ){

          ;

      }else{

          continue;
 }
 

      

      //printf("%s", line ); 

      //printf("strlen = %d.\n", strlen(line) );

      

      //printf("123%02x\n", line[0]);

      //printf("%02x\n", line[1]);

      //printf("%02x\n", line[2]);

      //printf("%02x\n", line[3]);

      //printf("%02x\n", line[4]);

            

      //i = 28;

      j = 0;
 i = write_char_off;
 tmp_strlen = strlen(line);

      while( 1 ){

      if( i >= tmp_strlen ){

         break;

      }

      if( line[i] == '\r' || line[i] == '\n' ){

      //printf("%d %x\n", i, line[i]);

         break;

      }

      if( line[i] == ' ' ){

      i++;

         continue;

      }

     

      //printf("%d %x\n", i, line[i]); 

      kh = line[i];

      kl = line[i+1];

      a2i(kh);

      a2i(kl);

      tmpbuf[j] = (kh<<4) | (kl<<0);

      //printf("%d %x\n", j, tmpbuf[j]);

      j++; 

      i = i + 2;

      }

      if( j ){

          fwrite( &tmpbuf[0], write_char_cnt, 1, fp_temp );

      }

      //break; 

    }

    fclose(fp);

    fclose(fp_temp);

    //清除工作         

}

void dump_line( char *buf )

{

    int i,len;

    if( buf == NULL ){

        return;

    }

    len = strlen( buf );
for( i=0; i<len; i++ ){

         printf("buf[%d] = 0x%02x.\n", i, buf[i] ); 

    }

}

int get_param_num( char *line, int index )

{

    int i;
int tmp_strlen;
unsigned long ul;
char *stopstring;

    tmp_strlen = strlen(line);

    i = index;
while( 1 ){
 if( i >= tmp_strlen ){

       break;

      }                                              

      if( line[i] == '\r' || line[i] == '\n' ){

       break;

      }

      if( line[i] == ' ' ){

       i++;

       continue;

      }
 if( line[i] == '=' ){

       i++;

       continue;

      }

 //printf("%d %x\n", i, line[i]);
 
 ul = strtol( &line[i], &stopstring, 10 );              

      if( !stopstring ){

          printf("*stopstring %x\n", *stopstring);
 printf("stopstring %p\n", stopstring);
 return 0;

      }
 //printf("%ld\n", ul);
 break;
  

    }
return ul;

}

char * get_param_str( char *line, int index )

{

    int i,len,i_backup;
int tmp_strlen;
unsigned long ul;
char *stopstring;
char *ret_buff;

    tmp_strlen = strlen(line);

    i = index;
while( 1 ){
 if( i >= tmp_strlen ){

       break;

      }                                              

      if( line[i] == '\r' || line[i] == '\n' ){

       break;

      }

      if( line[i] == ' ' ){

       i++;

       continue;

      }
 if( line[i] == '=' ){

       i++;

       continue;

      }
 if( line[i] == '"' ){

          //printf("here\n"); 
 }
 len = 0x00;
 i++;
 i_backup = i;
 while( 1 ){

        if( line[i] == '"' ){

            break;
}else{

            len++;
i++;
if( len > 128 ){

                break;
}
}     
 }

      ret_buff = malloc(len+1);
 if( ret_buff == NULL ){

          printf("memory over!\n");
 return NULL;
 }
 
 memcpy(ret_buff, &line[i_backup], len );
 ret_buff[len] = '\0';
 //printf( "%d %s\n", len, ret_buff );
 
 //printf("%ld\n", ul);
 break;
  

    }
return ret_buff;

}

int main_cfg( char *cfg_file_name )

{  

    int  len;

    int  c,i,j;

    char line[1024];

    FILE *fp;

    FILE *fp_temp;
char *p;
int tmp_strlen;
int get_line_offset;
char kh,kl;
char tmpbuf[12];
unsigned long ul;
char *stopstring;

fp = fopen( cfg_file_name, "r+" );

    //以普通的方式打开文件

    if( fp == NULL ){

        printf("cfg.file is not exist.\n" ); 

        return -1;

    }
memset( line, 0x00, sizeof(line) );           

                                                     

    j = 0x00;

    //seek to the beginning of the file

    fseek(fp, 0, SEEK_SET);  

    len = 0;
while( 1 ){

      p = fgets( line, 100, fp );
 //从输入文件读取一行字符串

      if( p == NULL ){

          //check for EOF 

          break;  

      }
 if( line[0] == ';' ){
   //注释行

          continue; 
 }

 //从配置文件获取相应的配置参数

      if( strncmp( "get_line_offset", line, 15 ) == 0x00 ){
     get_line_offset = get_param_num( &line[0], 15 );
 //printf( "get_line_offset = %d.\n", get_line_offset );

      }else if( strncmp( "read_char_cnt", line, 13 ) == 0x00 ){

          read_char_cnt = get_param_num( &line[0], 13 );   
 printf( "read_char_cnt = 0x%x.\n", read_char_cnt );
 }else if( strncmp( "write_char_cnt", line, 14 ) == 0x00 ){

          write_char_cnt = get_param_num( &line[0], 14 );   
 printf( "write_char_cnt = %d.\n", write_char_cnt );
 }else if( strncmp( "read_char_off", line, 13 ) == 0x00 ){

          read_char_off = get_param_num( &line[0], 13 );   
 printf( "read_char_off = %d.\n", read_char_off );
 }else if( strncmp( "write_char_off", line, 14 ) == 0x00 ){

          write_char_off = get_param_num( &line[0], 14 );   
 printf( "write_char_off = %d.\n", write_char_off );
 }else if( strncmp( "read_cancel_str", line, 15 ) == 0x00 ){

          read_cancel_str = get_param_str( &line[0], 15 );   
 //printf( "read_cancel_str = %s.\n", read_cancel_str );
 }else if( strncmp( "read_match_str", line, 14 ) == 0x00 ){

          read_match_str = get_param_str( &line[0], 14 );   
 printf( "read_match_str = %s.\n", read_match_str );
 }else if( strncmp( "write_match_str", line, 15 ) == 0x00 ){

          write_match_str = get_param_str( &line[0], 15 );   
 printf( "write_match_str = %s.\n", write_match_str );
 }else if( strncmp( "parse_file_name_txt", line, 19 ) == 0x00 ){

          parse_file_name_txt = get_param_str( &line[0], 19 );   
 printf( "parse_file_name_txt = %s.\n", parse_file_name_txt );
 }else if( strncmp( "parse_file_name_bin", line, 19 ) == 0x00 ){

          parse_file_name_bin = get_param_str( &line[0], 19 );   
 printf( "parse_file_name_bin = %s.\n", parse_file_name_bin );
 }

      

      
 
 


}

int main(int argc, char** argv) 

{

    char *cfg_file_name;
int result;
result = 0x00;
//printf( "argc = %d.\n", argc );

    if (argc < 2) {

      usage:

        printf(

            "usage: ./main cfg.txt"

            "\n"

            );

        return 2;

    }

    cfg_file_name = argv[1];

    //printf( "argv[0] = %s.\n", argv[0] );
//printf( "argv[1] = %s.\n", argv[1] );

    

    //main_cfg(cfg_file_name);

    //main_r();

    //main_w();
//main_bin2txt();

main_add_0x();

    

    return result;

}

//2进制文件到ascll数组 0x

int main_bin2txt( void )

{  

    int  len;

    int  c,j;

    char line[4096];

    FILE *fp;

    FILE *fp_temp;

    char *parse_file_name;

parse_file_name = parse_file_name_bin;

    fp = fopen(parse_file_name, "rb+");

    //以二进制的方式打开bin文件

    if( fp == NULL ){

        printf("%s is not exist1.\n", parse_file_name ); 

        return -1;

    }

    memset( line, 0x00, sizeof(line) );                           

                                                   

    j = 0x00;  

    fp_temp = fopen("bin2txt.txt", "w+");

    if( fp_temp == NULL ){

        printf("bin2txt.txt open failed.\n" ); 

        return -1;

    }

    //seek to the beginning of the file

    fseek(fp, 0, SEEK_SET);  

    fseek(fp_temp, 0, SEEK_SET);

    len = 0;

    while(1){

      c = fgetc(fp);

      if( feof(fp) ){

          //check for EOF 

          break;  

      }

      //printf("c = %x.\n", c ); 

      line[0]='0';

      line[1]='x';

      

      j = (c&0xf0) >> 4;

      i2a(j);

      line[2] = j;

      

      j = (c&0x0f) >> 0;

      i2a(j);

      line[3] = j;

      

      line[4]=',';
 //加入,

      line[5]=' ';

      //加入空格

      ++len;

      if( len%16 == 0x00 ){
   //每16字节换行

          line[6] = '\r'; 

          line[7] = '\n'; 

          fwrite( &line[0], 8, 1, fp_temp );
 //每个字符写一次文件

      }else{

          fwrite( &line[0], 6, 1, fp_temp );
 //每个字符写一次文件

      }

    }

    fclose(fp);

    fclose(fp_temp);

    //清除工作         

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: