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;
    }
  • 相关阅读:
    LeetCode题解之Flipping an Image
    LeetCode 之Find Minimum in Rotated Sorted Array
    LeetCode题解Transpose Matrix
    LeetCode 题解之Minimum Index Sum of Two Lists
    LeetCode题解之Intersection of Two Linked Lists
    LeetCode 题解之Add Two Numbers II
    LeetCode题解之Add two numbers
    href="#"与href="javascript:void(0)"的区别
    有关ie9 以下不支持placeholder属性以及获得焦点placeholder的移除
    ie7下属性书写不规范造成的easyui 弹窗布局紊乱
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11207012.html
Copyright © 2011-2022 走看看