C模拟进度条


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>

void progressOfReadFile(const char* filename){
    FILE* fd = NULL;
    size_t sizeOfFile = 0;
    size_t readNum = 0;
    int progress = 20;
    char temp[100];
    fd = fopen(filename, "rb");
    if (fd == NULL){
        return;
    }
    if (0 != fseek(fd, 0, SEEK_END)){
        return ;
    }
    sizeOfFile = ftell(fd);
    fseek(fd, 0, SEEK_SET);
    system("color 0C");
    while (!feof(fd)){
        readNum += fread(temp, 1, sizeof(temp), fd);
        int i = 0;
        int current = readNum / (sizeOfFile/progress);
        for (; i <= current; ++i){
            printf("■");
        }
        for (; i <= progress; ++i){
            printf("  ");
        }       
        printf("\t%d%%\r", readNum*100 / sizeOfFile);
        Sleep(500);
    }
    system("color 0F");
}



#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "progress.h"
int main(){

    progressOfReadFile("./C语言实例.txt");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaolixi199311/article/details/79282638