C语言——文件操作

C语言——文件操作

1.文件拷贝

#include

int main(int argc,char** argv)
{
    FILE *src=NULL;
    FILE *new=NULL;
    int n_read=0;
    unsigned char buf[1024]={0};
    src=fopen("./file","r+");
    if(src==NULL)
    {
        printf("文件打开失败\n"); 
        return -1;
    }
    new=fopen("./file1","w+");
    if(new==NULL)
    {
        printf("文件打开失败\n");
        return -1;
    }
    while(!feof(src))
    {
        n_read=fread(buf,1,sizeof(buf),src);
        fwrite(buf,1,n_read,new);
    }
    fclose(src);
    fclose(new);
    printf("文件拷贝成功\n");
    return 0; 
}

2.文件加解密实现

2.1.实现方式一:

#include

void eneryFile(char* file1,char* file2,int key)
{
    FILE* fp1=fopen(file1,"r+");
    FILE* fp2=fopen(file2,"w+");
    int   data=0;
    int   n_read=0;
    while(!feof(fp1))
    {
        n_read=fread(&data,1,sizeof(int),fp1);
        data=data^key;
        fwrite(&data,1,n_read,fp2);
    }
    fclose(fp1);
    fclose(fp2);
}

void deneryFile(char* file1,char* file2,int key)
{
    eneryFile(file1,file2,key);
}

void printListInfo()
{
    printf("\n--------------菜单----------------\n");
    printf("1. 加密\n");
    printf("2. 解密\n");
    printf("请选择功能序号:");
}

int main(int argc,char** argv)
{
    char* src_file="./file";
    char* new_file="./file1";
    char* new2_file="./file2";
    int key=123;
    int number=0;
    while(1)
    {
        printListInfo();
        scanf("%d",&number);
        switch(number)
        {
            case 1:
                eneryFile(src_file,new_file,key);
                break;
            case 2:
                deneryFile(new_file,new2_file,key);
                break; 
            default:
                printf("输入错误\n");
                break;         
        }
    } 
    return 0;
}

3.fread

#include
#include
#include

int main(int argc,char** argv)
{
    FILE* file=NULL;
    int ret=0;
    char buf[100]={0};
    file=fopen("./text","r+");
    if(file==NULL)
    {
        printf("open file failed\n");
        return -1;
    }
    //fseek(file,0,SEEK_SET);
    ret=fread(buf,1,sizeof(buf),file);
    if(ret!=-1)
    {
        printf("read file success\n");
        printf("buf=%s\n",buf);
        return -1;
    }
    fclose(file);
    return 0;
}

4.fwrite

#include
#include

int main(int argc,char** argv)
{
    int i=0;
    for(i=0;i<argc;i++)
    {
        printf("argc[%d]=%s\n",i,argv[i]);
    }
    FILE *file=NULL;
    char *buf="xiewenhui";
    int ret=0;
    file=fopen("./text","w+");
    if(file==NULL)
    {
        printf("open file error\n");
        return -1;        
    }
    ret=fwrite(buf,1,strlen(buf),file);
    if(ret==-1)
    {
        printf("write failed\n");
        return -1;
    }
    fclose(file);
    return 0;
}

5.写入结构体到链表

void saveLink(Node* head)
{
    FILE *fp=NULL;
    Node* p=head;
    Node* new=(Node*)malloc(sizeof(Node));
    new->next=NULL;
    fp=fopen("./text.txt","w+");
    if(fp==NULL)
    {
        printf("file open failed\n");
        return;
    }
    while(p->next!=NULL)
    {
        fwrite(p,sizeof(Node),1,fp);
        p=p->next;
    }
    fclose(fp);
}

6.写入数据到结构体

#include

typedef struct Test  
{
    int a;
    int b;
    char c[100];
}Demo;

int main(int argc,char **argv)
{
    Demo stu={888,666,"xie"};
    FILE *file=NULL;
    int   ret=0;
    file=fopen("./text","w+");
    if(file==NULL)
    {
        printf("文件打开失败\n");
        return -1;
    }
    ret=fwrite(&stu,sizeof(Demo),1,file);
    if(ret==1)
    {
        printf("写入成功:%d\n",ret);
        return -1;
    }
    fclose(file);
    return 0;
}

7.从结构体中读取数据

#include

struct Test 
{
    int a;
    int b;
    char c[100];
};

int main(int argc,char** argv)
{
    FILE *file=NULL;
    int ret=0;
    struct  Test data={0};
    file=fopen("./text","r+");
    if(file==NULL)
    {
        printf("文件写入失败\n");
        return -1;
    }
    ret=fread(&data,sizeof(struct Test),1,file);
    if(ret==1)
    {
        printf("读取成功:%d,%d,%s\n",ret,data.a,data.b,data.c);
        return -1;
    }
    return  0;
}

8.文件读写链表

尾插法

#include
#include

typedef struct  Test 
{
    int data;
    struct Test *next;
}Node;

Node* insertListNode(Node* head)
{
    Node* p=head;
    Node* new=NULL;
    int i=0;
    for(i=0;i<5;i++)
    {
        new=(Node*)malloc(sizeof(Node));
        new->next=NULL;
        printf("please input %d data\n",i+1);
        scanf("%d",&(new->data));
        if(head==NULL)
        {
            head=new;
        }
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=new;
    }
    return head;
}

void saveListNode(Node* head)
{
    FILE *fp=NULL;
    Node* p=head;
    int  n_write=0;
    fp=fopen("./file","w+");
    if(fp==NULL)
    {
        printf("open file failed\n");
        return;
    }
    while(p->next!=NULL)
    {
        n_write=fwrite(p,sizeof(Node),1,fp);
        if(n_write==-1)
        {
            printf("write file failed\n");
        }
        p=p->next;
    }
    fclose(fp);
}

void readListNode(Node* head)
{
    FILE* fp=NULL;
    Node* p=head->next;
    int n_read=0;
    fp=fopen("./file","r+");
    if(fp==NULL)
    {
        printf("open file failed\n");
        return;
    }
    while(p!=NULL)
    {
        n_read=fread(&p->data,sizeof(Node),1,fp);
        if(n_read!=-1)
        {
            printf("%d ",p->data);
        }
        p=p->next;
    }
    putchar('\n');
    fclose(fp);
}

int main(int argc,char** argv)
{
    Node* head=(Node*)malloc(sizeof(Node));
    head=insertListNode(head);
    saveListNode(head);
    readListNode(head);
    return 0;
}

头插法

#include
#include

typedef struct Test
{
   int data;
   struct Test *next;
}Node;

Node* insertListNode(Node* head)
{
      Node* p=head;
      Node* new=NULL;
      int i=0;
      for(i=0;i<5;i++)
      {
           new=(Node*)malloc(sizeof(Node));
           new->next=NULL;
           printf("please input NO.%d number\n",i+1);
           scanf("%d",&(new->data));
           if(head==NULL)
           {
               head=new;
           }
           else
           {
              new->next=head;
              head=new;
           }
      }
      return head;
}

void saveListNode(Node* head)
{
    Node* p=head;
    FILE* fp=NULL;
    int n_write=0;
    fp=fopen("./file1","w+");
    if(fp==NULL)
    {
        printf("open file failed\n");
        return;
    }
    while(p!=NULL)
    {
        n_write=fwrite(p,sizeof(Node),1,fp);
        if(n_write==-1)
        {
            printf("write file failed\n");
            return;
        }
        p=p->next;
    }
    fclose(fp);
}

void readListNode(Node* head)
{
   Node* p=head;
   FILE* fp=NULL;
   int n_read=0;
   fp=fopen("./file1","r+");
   if(fp==NULL)
   {
      printf("open  file failed\n");
      return;
   }
   while(p!=NULL)
   {
      n_read=fread(&p->data,sizeof(Node),1,fp);
      if(n_read!=-1)
      {
         printf("%d ",p->data);
         
      }
      p=p->next;
   }
   putchar('\n');
   fclose(fp);     
}

int main(int argc,char** argv)
{ 
    Node* head=NULL;
    head=insertListNode(head);
    saveListNode(head);
    readListNode(head);
    return 0;
}
#include
#include

typedef struct Test 
{
    int data;
    char cdata;
    char str[100];
    struct Test *next;
}ListNode;

ListNode* insertListNodeTail(ListNode* head,ListNode* new)
{
      ListNode* p=head;
      if(head==NULL)
      {
         head=new;
         return head;
      }
      while(p->next!=NULL)
      {
          p=p->next; 
      }
      p->next=new;
      return head;
}

ListNode* insertListNode(ListNode* head)
{
       ListNode* p=head;
       ListNode* new=NULL;
       int i=0;
       for(i=0;i<2;i++)
       {
            new=(ListNode*)malloc(sizeof(ListNode));
            new->next=NULL;
            printf("please input NO.%d number,NO.%d cdata,NO.%d string\n",i+1,i+1,i+1);
            scanf("%d",&(new->data));
            getchar();
            scanf("%c",&(new->cdata));
            scanf("%s",(new->str));
            head=insertListNodeTail(head,new);
       }
       return head;
}

void saveListNode(ListNode* head)
{
     ListNode* p=head;
     FILE* fp=NULL;
     int n_write=0;
     fp=fopen("./text.txt","w+");
     if(fp==NULL)
     {
          printf("open file failed\n");
          return;
     }
     while(p!=NULL)
     {  
         n_write=fwrite(p,sizeof(ListNode),1,fp);
         if(n_write==-1)
         {
             printf("write file failed\n");
             return;
         }  
         p=p->next;
     }
     fclose(fp);
}

void readListNode(ListNode* head)
{
     ListNode* p=head;
     ListNode* new=NULL;
     FILE* fp=NULL;
     int ret=0;
     fp=fopen("./text.txt","r+");
     if(fp==NULL)
     {
        printf("open file failed\n");
        return;
     }
     while(p!=NULL)
     {
        ret=fread(p,sizeof(ListNode),1,fp);
        if(ret!=-1)
        {
           // printf("read success\n");
            printf("%d,%c,%s\n",p->data,p->cdata,p->str);
        }
        p=p->next;
     }
     fclose(fp);
}



int main(int argc,char** argv)
{
    ListNode* head=NULL;
    head=insertListNode(head);
    saveListNode(head);
    readListNode(head);
    return 0;
}

你可能感兴趣的:(c语言,算法,数据结构)