saving file bitmap

Hey guys i have a header file and now i am writing the implementation file and defining the constructors..i am having trouble with 1 but..

void bitmap:save (string filename)
{


}

//The bitmap representation is written to filename. The bitmap file will consist of two positive integers R and C representing the number of rows and columns followed by R rows of C columns of zeroes and ones. For example :

3 5
00000
00000
01100
//i have to save it into string file name
can anyone plz help me
closed account (z05DSL3A)
Start reading here:
http://www.cplusplus.com/reference/iostream/fstream/

1
2
3
4
5
6
7
8
9
10
Open the file
write R
write C
for each row
   for each colum
     write value
  end
end
close the file
Last edited on
You can do it his way or do it like this:

1
2
3
4
Open file
Read in Row, Column
Allocate size of bitmap (which is Row*Column*BitmapChannels)
read( buffer, size )
closed account (z05DSL3A)
It is a *save* function!
man ima retard give me a break i have no experience in c++
i tryed using this

void bitmap::save (string filename) const
{
char ch;
int size;
ofstream infile(filename.c_str());
if (infile.fail())
{
cout <<"Failed to open file" << cout;
}

for (int r = 0; r < numrows; r++)
for (int c = 0; c < numcols; c++)
{
infile.write(ch, size);
}
infile.close();
}


error says invalid conversion from 'char' to const char*
closed account (z05DSL3A)
You could try somthing along the lines of this: (untested code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void bitmap::save (string filename) const
{
    ofstream outFile(filename.c_str());
    if (myfile.is_open())
    {
        outFile << numrows;
        outFile << numcols;
        for (int r = 0; r < numrows; r++)
        {
            for (int c = 0; c < numcols; c++)
            { 
                //asuming you have your data in a 2D array
                //I'll also assume that the data member is called data
                outFile << data[r][c]; 
            }
        }
    }
    else
    {
        cout <<"Failed to open file" << cout;
    }
    outFile.close();
} 
Topic archived. No new replies allowed.