Windows 编程
文件读写
以文本方式( "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(即第一列)排序的第一个图标即为应用程序的图标。
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
参见
外部链接
- theForger's Win32 API教程第二版(简体中文)
- System Error Codes (0-499)
- HANDLE为什么翻译为句柄 - C/C++ / C++ 语言
- Windows下的函数hook技术_VC文摘_开发文摘_软件开发_天新网(排版很乱)
- Python中的Ctypes库 | the 3rd. Place,结尾有涉及 stdcall 和 cdecl 的区别。
- The wild west of Windows command line parsing
- How Command Line Parameters Are Parsed
参考资料
- ↑ http://connect.microsoft.com/VisualStudio/feedback/details/100600/getc-fgetc-functions-fails-reading-binary-file
- ↑ 微软 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.」
- ↑ CPyUG: 不能在文件中间写?
- ↑ MSDN: fopen, _wfopen
- ↑ python写文件打开后是乱码 - SegmentFault
- ↑
man 3 fopen