我有一个处理 Ascii 和二进制 PGM 图像的程序。我使用以下方法打开文件打开模式:
ifstream istr(argv[i]);
读入 PGM 文件的标头后,确定我处理的是二进制数据还是 ASCII。那么有没有办法将istr更改为二进制模式?
没有成员函数std::ifstream可以更改模式。因此,您必须以二进制模式close()重新open()生成文件。
1> You would have first clear the stream by calling Clear method to reset the mode of the stream(required if your stream has hit an end-of-file or has encountered an error)
2> You would have to close the stream by calling close method
3> Finally you have to call the open method to open the new file with the desired filemode
不,至少不是便携的。您将不得不以二进制模式打开并手动处理行尾。