Windows 编程

来自百合仙子's Wiki
跳转到导航 跳转到搜索

文件读写

文本方式"r" )读取二进制文件时, Ctrl-Z 会被当作 EOF ,从而造成提前读到 EOF[1]

读写方式"w+" 等)打开文件时,在读写切换时必须进行刷新或者定位操作。[2]Linux 下不需要。[3]

对于 fopen, fread, fwrite 系列函数,在使用读写方式打开文件时,从读操作转到写操作时,必须调用文件定位函数,而从写操作转到读操作时,必须调用文件定位函数或者 fflush() [4][5]

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file positioning function. The file positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file positioning function.

这是 ANSI C 标准中允许的。在 Linux 中通常不需要这样做[6]

Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek(3) or fgetpos(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.

应用程序图标

编写资源文件( *rc ),如:

1 ICON "icon.ico"

按 ID(即第一列)排序的第一个图标即为应用程序的图标。

编译示例(使用 gccMakefile):

P=i686-w64-mingw32-
CC=$(P)gcc
CFLAGS=-O2 -Wall
LDFLAGS=

all: run.exe

run.exe: rc.res runner.o
	$(CC) -mwindows -o $@ $^

rc.res: rc.rc icon.ico
	$(P)windres $< -O coff -o $@

%.ico: %.png
	convert -resize 48x48 $< $@

.PHONY: all clean

clean:
	-rm *.o *.ico *.res

参见

外部链接

参考资料

  1. http://connect.microsoft.com/VisualStudio/feedback/details/100600/getc-fgetc-functions-fails-reading-binary-file
  2. 微软 fopen 文档:「However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.」
  3. CPyUG: 不能在文件中间写?
  4. MSDN: fopen, _wfopen
  5. python写文件打开后是乱码 - SegmentFault
  6. man 3 fopen