求知:C语言程序能处理非txt格式的文件吗?
各位达人,请问C语言做的程序除了处理txt文件之外,还能处理其他常见文件格式吗?例如,doc, xls, pdf, bmp,jpg等等。我看到的所有C语言处理文件的例子中,几乎全是处理txt格式,而且还是ANSI字符集的。虽然也有二进制读写的,但却没有谈如何处理特殊格式的文档,只是说特殊格式的文档中那些格式或特征性的东西也都是由二进制数据控制的,至于这些格式和特征如何在C语言的程序中进行读或写却没有涉及。
我想C语言应该有办法处理这些特殊格式的文档,否则如果只是跟txt打交道,那它的有用性在这方面还是要打折扣的。C语言诞生于Unix系统,该系统上的文档最初也都是ANSI字符集之内的,但现在的操作系统应该能够处理不同格式的文件,C语言在这方面应该也会有所更新吧。 哦?!原来我一直是横着走的?
[解决办法]
#pragma comment(lib,"gdi32")
#include <windows.h>
#include <stdio.h>
int main() {
const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;
PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);
pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = uWidth;
pbmi->bmiHeader.biHeight = uHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
pbmi->bmiColors[0].rgbBlue = 0;
pbmi->bmiColors[0].rgbGreen = 0;
pbmi->bmiColors[0].rgbRed = 0;
pbmi->bmiColors[1].rgbBlue = 255;
pbmi->bmiColors[1].rgbGreen = 255;
pbmi->bmiColors[1].rgbRed = 255;
HDC hDC = CreateCompatibleDC (0);
void * pvBits;
HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);
SelectObject (hDC, hBitmap);
HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
// HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, "宋体");
SelectObject (hDC, hFont);
BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);
char c[4];
int i, j;
for (i = 128; i < 256; i++) {
sprintf (c, "%02X", i);
TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);
}
for (j = 0; j < 256; j++) {
sprintf (c, "%02X", j);
TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);
}
for (i = 128; i < 256; i++) {
for (j = 0; j < 256; j++) {
c[0] = (char) i;
c[1] = (char) j;
TextOut (hDC, (j + 1) * 17 + 1, (i - 127) * 17 + 1, c, 2);
}
}
for (i = 0; i < 130; i++) {
MoveToEx (hDC, 0, i * 17, NULL);
LineTo (hDC, uWidth, i * 17);
}
for (j = 0; j < 258; j++) {
MoveToEx (hDC, j * 17, 0, NULL);
LineTo (hDC, j * 17, uHeight);
}
BITMAPFILEHEADER bmfh;
bmfh.bfType = *(PWORD) "BM";
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;
bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;
HANDLE hFile = CreateFile ("goal.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD dwWritten;
WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);
WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);
CloseHandle (hFile);
}
DeleteObject (hFont);
DeleteObject (hBitmap);
DeleteDC (hDC);
LocalFree (pbmi);
return 0;
}
A file, in the computer world, is a self contained piece of information available to the operating system and any number of individual programs. Information inside the file could consist of essentially anything but whatever the file contains is likely related somehow.
A computer file can be thought of much like a traditional file that one would find in an office's file cabinet.
Whatever program uses an individual file is responsible for understanding its contents. Similar types of files are said to be of a common "format." In most cases, the easiest way to determine a file's format is to look at the file's extension.
Each individual file in Windows will also have a file attribute which sets a condition to the specific file.
Files in any operating system are stored on hard drives, optical drives, and other storage devices. The specific way a file is stored and organized is referred to as a file system.
Examples:
"For years I had a text file that I stored important telephones and addresses in. A few years ago, when I first started using Microsoft Excel, I moved the information that text file to a new Excel file I created. In this new file format, I can sort names and display the information in new ways."