zoukankan      html  css  js  c++  java
  • 文件操作函数(读写)

    文件文本排序:

    数组冒泡:

    #include<stdio.h>
    
    void swap(int *a,int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    void bubble(int *p,int n)
    {
        int i;
        int j;
        for(i = 0; i < n; i++)
        {
            for(j = 1; j < n - i; j++)
            {
                if(p[j - 1] > p[j])
                {
                    swap(&p[j-1],&p[j]);
                }
            }
        }
    }
    
    int main()
    {
            int arr[10] = {4,6,3,7,8,5,12,67,34,56};
            bubble(arr,10);
            
            int i;
            for(i = 0; i < 10 ; i++)
            {
                printf("%d ",arr[i]);
            }
            printf("
    ");
            return 0;
    }

    文件数据排序

    #include<stdio.h>
    #include<string.h>
    
    void swap(int *a,int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    void bubble(int *p,int n)
    {
        int i;
        int j;
        for(i = 0; i < n; i++)
        {
            for(j = 1; j < n - i; j++)
            {
                if(p[j - 1] > p[j])
                {
                    swap(&p[j-1],&p[j]);
                }
            }
        }
    }
    
    int main()
    {
            int array[10] = {0};
            int i;
            char buf[100];        
            int index = 0;
            FILE* p =fopen("./file.txt","r");
            if(NULL == p)
            {
                printf("error
    ");
            }
            else
            {
                while(!feof(p))
                {
                    memset(buf,0,sizeof(buf));
                    fgets(buf,sizeof(buf),p);
                    array[index] = atoi(buf);
                    index++;
                }
                fclose(p);
            }
            index--;
            bubble(array,index);
    
            p = fopen("./b.txt","w");
            printf("index = %d
    ",index);
            for(i = 0; i < index ; i++)
            {
                memset(buf,0,sizeof(buf));
                sprintf(buf,"%d
    ",array[i]);
                fputs(buf,p);
                //printf("%d ",array[i]);
            }
            fclose(p);
            printf("i = %d
    ",i);
            return 0;
    }

    如果文件数据过大,就不能在栈中建立一个数组,考虑使用堆

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void swap(int *a,int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    void bubble(int *p,int n)
    {
        int i;
        int j;
        for(i = 0; i < n; i++)
        {
            for(j = 1; j < n - i; j++)
            {
                if(p[j - 1] > p[j])
                {
                    swap(&p[j-1],&p[j]);
                }
            }
        }
    }
    
    int main()
    {
            int i;
            char buf[100];        
            int index = 0;
            FILE* p =fopen("./file.txt","r");
            if(NULL == p)
            {
                printf("error
    ");
            }
            //else
            {
                while(!feof(p))
                {
                    memset(buf,0,sizeof(buf));
                    fgets(buf,sizeof(buf),p);
                    index++;
                }
                fclose(p);
            }
    
            int *array = calloc(sizeof(int),index);
            index = 0;
    
             p =fopen("./file.txt","r");
            if(NULL == p)
            {
                printf("error
    ");
            }
            //else
            {
                while(!feof(p))
                {
                    memset(buf,0,sizeof(buf));
                    fgets(buf,sizeof(buf),p);
                    array[index] = atoi(buf);
                    index++;
                }
                fclose(p);
            }
    
            index--;
            bubble(array,index);
        
    
            p = fopen("./b.txt","w");
            printf("index = %d
    ",index);
            for(i = 0; i < index ; i++)
            {
                memset(buf,0,sizeof(buf));
                sprintf(buf,"%d
    ",array[i]);
                fputs(buf,p);
                //printf("%d ",array[i]);
            }
            fclose(p);
            printf("i = %d
    ",i);
            return 0;
    }
  • 相关阅读:
    linux weblogic的sh文件
    linux 安装weblogic(转载)
    linux 安装jdk
    linux 用户和用户组
    测试开发工程师必备软硬能力&高级测试开发工程师需要具备什么能力?
    postman强大的团队协作功能
    requests(一): 发送一个json格式的post请求
    python操作Excel模块openpyxl
    appium环境安装app自动化
    夜神模拟器怎么连接adb
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11207012.html
Copyright © 2011-2022 走看看