Đọc ghi file nhị phân

Viết chương trình tạo tập tin nhị phân chứa 100 số nguyên bất kỳ ghi vào file nhị phân SONGUYEN.INP. Sau đó viết chương trình đọc file SONGUYEN.INP, sắp xếp theo thứ tự tăng dần và lưu kết quả vào file text SONGUYEN.OUT.

/************************************************************
* Author: VNCODING
* History                  
* 2014/04/28        first create    VNCODING
*************************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

bool WriteFileBinary();
bool ReadFileBinary(int **buff);
bool WriteFileText(int *buff);
void BubbleSort(int **buff);

#define MAX_LEN 100

void main()
{
    int buff[MAX_LEN];
    int *pBuf = buff;
    if (!WriteFileBinary())
    {
        goto EXIT;
    }
    if (!ReadFileBinary(&pBuf))
    {
        goto EXIT;
    }
    BubbleSort(&pBuf);
    if (!WriteFileText(buff))
    {
        goto EXIT;
    }
    printf("\nSucessfull!!!");
EXIT:
    getch();
}
/************************************************************
* Function: WriteFileBinary()
* Description: Generate randomize number by using rand(), then
write to binary file SONGUYEN.INP
* Return: true if write successfully, false if write fail
*************************************************************/
bool WriteFileBinary()
{
    FILE *fp = NULL;
    int idx;
    int buff[MAX_LEN];
    fp = fopen("D:\\SONGUYEN.INP", "wb");
    if (!fp)
    {
        printf("\nError in opening file");
        return false;
    }
    //Generate randomize number
    for (idx = 0; idx < MAX_LEN; idx++)
    {
        buff[idx] = rand();
    }
    // Write data to SONGUYEN.INP file
    if (fwrite(buff, sizeof(buff), 1, fp) < 1)
    {
        printf("\nError in writing SONGUYEN.INP file");
        return false;
    }
    fclose(fp);
    return true;
}
/************************************************************
* Function: ReadFileBinary()
* Description: Read data from SONGUYEN.INP file
* Return: true if read successfully, false if read fail
*************************************************************/
bool ReadFileBinary(int **buff)
{
    FILE *fp = NULL;
    fp = fopen("D:\\SONGUYEN.INP", "rb");
    if (!fp)
    {
        printf("\nError in opening file");
        return false;
    }
    if (fread(*buff, MAX_LEN*sizeof(int), 1, fp) < 1)
    {
        printf("\nError in reading SONGUYEN.INP file");
        return false;
    }
    fclose(fp);
    return true;
}
/************************************************************
* Function: WriteFileText()
* Description: Write data to text file SONGUYEN.OUT
* Return: true if write successfully, false if write fail
*************************************************************/
bool WriteFileText(int *buff)
{
    FILE *fp = NULL;
    int idx;
    fp = fopen("D:\\SONGUYEN.OUT", "w");
    if (!fp)
    {
        printf("\nError in opening file");
        return false;
    }
    // Write data to SONGUYEN.OUT file
    for (idx = 0; idx < MAX_LEN; idx++)
    {
        fprintf(fp, "%d\t", buff[idx]);
    }
    fclose(fp);
    return true;
}
/************************************************************
* Function: BubbleSort()
* Description: Sort number of array in ascending order by
using bubble sort
* Return: void
*************************************************************/
void BubbleSort(int **buff)
{
    int idx, idx1;
    int temp;
    for (idx = 0; idx < MAX_LEN - 1; idx++)
    {
        for (idx1 = idx + 1; idx1 < MAX_LEN; idx1++) { if ((*buff)[idx] > (*buff)[idx1])
            {
                temp = (*buff)[idx];
                (*buff)[idx] = (*buff)[idx1];
                (*buff)[idx1] = temp;
            }
        }
    }
}

Kết quả:

Đọc/ghi file
Đọc/ghi file

Be the first to comment

Leave a Reply

Your email address will not be published.

*