注意描红部分的读法:
#include <Windows.h>
#include "myConsole.h"
void ShowStatusBar(void);
const SHORT MaxRows = 25;
const SHORT MaxCols = 80;
myConsole Console(GetStdHandle(STD_OUTPUT_HANDLE));
int wmain(int argc, wchar_t argv[])
{
Console.SetTitle(L"整数读取并统计");
Console.Cls(FOREGROUND_WHITE | BACKGROUND_BLUE);
ShowStatusBar();
const char file_name[] = "e:\\Projects\\test\\test\\zhengshu.dat";
FILE* file;
errno_t error = fopen_s(&file, file_name, "
rt");
if (error == 0)
{
COORD pos = { 0, 0 };
Console.MoveCursor(pos).WriteText(L"文件中的数据:");
int data;
while (!feof(file) && (fscanf_s(file, "%d", &data) > 0))
{
++pos.Y;
WCHAR buffer[80];
swprintf_s(buffer, L"%d", data);
Console.MoveCursor(pos).WriteText(buffer);
}
fclose(file);
}
else
{
WCHAR msg[1024];
swprintf_s(msg, L"文件%s打开错误,错误码:%d", file_name, error);
Console.MoveCursor(0, 0).WriteText(msg);
}
while (Console.ReadKey() != myConsole::K_ESC)
{
;
}
return EXIT_SUCCESS;
}
void ShowStatusBar(void)
{
WORD attr(Console.GetTextAttribute());
SMALL_RECT rect = { 0, MaxRows - 1, MaxCols - 1, MaxRows - 1 };
Console.SetTextAttribute(FOREGROUND_YELLOW | BACKGROUND_RED);
Console.Clear(rect);
Console.MoveCursor(rect.Left, rect.Top).WriteText(L"按<Esc>键结束程序");
Console.SetTextAttribute(attr);
}
[
本帖最后由 TonyDeng 于 2012-3-25 14:10 编辑 ]