Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取PPM文件RGB值C++;_C++_Ppm - Fatal编程技术网

C++ 读取PPM文件RGB值C++;

C++ 读取PPM文件RGB值C++;,c++,ppm,C++,Ppm,我正在尝试读取PPM文件。 我想读每行的第一、第二和第三个数字,但我不知道如何读这些行 这就是我到目前为止所做的: for (int y = 4; y <= HEIGHT; y++) { // i think it has to start on row 4 for (int x = 1; x <= WIDTH; x++) { // and x from 1 int i = 4; int r = CurrentR(i);

我正在尝试读取PPM文件。 我想读每行的第一、第二和第三个数字,但我不知道如何读这些行

这就是我到目前为止所做的:

for (int y = 4; y <= HEIGHT; y++) { // i think it has to start on row 4 
    for (int x = 1; x <= WIDTH; x++) { // and x from 1
         int i = 4;

         int r = CurrentR(i);
         int g = CurrentG(i);
         int b = CurrentB(i);
         i++;
    }   
}

int CurrentR(int I) {
    return // the first number in row xy
}
int CurrentG(int I) {
    return // the second number in row xy
}
int CurrentB(int I) {
    return // the third number in row xy
}

for(int y=4;y我建议您这样做:

struct RGB {
    int R,B,G;
}
std::ifstream& operator>>(std::ifstream &in,RGB& rgb){
    in >> rgb.R;
    in >> rgb.G;
    in >> rgb.B;
    return in;
}
std::ostream& operator<<(std::ostream &out,RGB& rgb){
    out << rgb.R << " ";
    out << rgb.G << " ";
    out << rgb.B << std::endl;
    return out;
}


int main(){
    std::string filename = "test.txt";
    std::ifstream file(filename.c_str());
    if(file.is_open()) {
        std::string line;
        for (int i=0;i<4;i++) { std::getline(file,line); }
        RGB rgb;
        for (int i=0;i<LINES_TO_READ;i++) {
            file >> rgb;
            std::cout << rgb;
        }
    }
}
struct RGB{
int R,B,G;
}
std::ifstream和运算符>>(std::ifstream和in、RGB和RGB){
在>>rgb.R;
在>>rgb.G;
在>>rgb.B;
返回;
}

std::ostream&operator您想将PPM转换为BMP?@tobi303不,我想存储PPM文件的RGB值。您想将RGB值存储在
img
?什么是
img
?@tobi303不介意。我只需要知道CurrentR()、CurrentG()和CurrentB()中需要什么函数。我完全不理解你的代码,但它不能像这样工作。你为
x=5,y=6
x=6,y=5
传递相同的参数谢谢你的帮助,但这是“读取三个数字”我需要帮助。你的方式可能更好,但我是一个初学者,因此如果你能帮助ifstream运行,我想那会更好。我读了你发布的链接,但我不太了解。这也不能解释ppm文件开始时的格式信息。