使用C ++写入.PGM文件

时间:2016-10-07 02:14:41

标签: c++ pgm

出于某种原因,我无法向.PGM文件写任何内容。以下代码编译时没有错误,但没有任何内容写入它创建的.PGM文件。我对C ++很陌生,并且不熟悉在这种语法中使用字符串。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>

int main(){
// Initialize variables.
    const int ncols = 30;
    const int nrows = 20;
    const int maxval = 255;
    std::string filename;

// Prompt user for filename.
    std::cout << "What would you like to name the file of the PGM image? Please include .PGM at the end of your name." << std::endl;
// Uses getline() function to retrieve input from user into a string.
    std::getline(std::cin, filename);
// Creates output stream object to use with managing the file.      
    std::ofstream fileOut(filename.c_str(),std::ios_base::out
                          |std::ios_base::binary
                          |std::ios_base::trunc
               );
    fileOut.open(filename.c_str());
    fileOut << "P2" << " " << ncols << " " << nrows << " " << maxval << "\n";

    fileOut.close();
}

我知道还有另外一个与此类似的问题,但我用这个答案来到这里。我甚至无法写出标题部分,甚至不是作业的重点。有人可以帮忙吗?

0 个答案:

没有答案