Solved

Need help on reading in Managed Class

Posted on 2004-04-27
9
289 Views
Last Modified: 2013-12-03
Hi all, Currently I have this code :
void read_file(char * filename)
            {
                  int width, height, maxColorVal, R, G, B;
                  unsigned char ch1, ch2, ch3;
                  char buf[100];
                  System::Drawing::Color mycolor;
                  ifstream infile("Dune.ppm", ios_base::in | ios_base::binary);
                  infile.getline(buf, 100);
                  if(infile.peek() == '#'){
                        infile.getline(buf,100);
                  }
                  infile>>width;
                  infile>>height;
                  infile>>maxColorVal;
                  Bitmap *mybmp = new Bitmap(width, height);
                  
                  for(int i = 0; i < height; i++){
                        for(int j = 0; j < width; j++){
                              infile>>ch1;
                              infile>>ch2;
                              infile>>ch3;
                              mycolor = System::Drawing::Color::FromArgb(ch1,ch2,ch3);
                              mybmp->SetPixel(j, i, mycolor);
                              R = G = B = 0;
                        }
                  }
                  infile.close();
                  pictureBox2->Image = dynamic_cast<Image*>(mybmp);
            }
=========================================================================
It seems that the resulting picture isn't correct, I am not sure why but i am guessing its the EVIL managed class thing thats reading the thing wrong..
I have heard that I should use FileStream , and BufferedStream
But I am not so sure how I can read 1 line from the file? I couldn't find any member function does that trick..
In the ppm file i am reading, i should be reading the first 3~4 lines of HEADER information, then everything after that is
RGBRGBRGBRGB.....etc

How do i read lines using FileStream or BufferedStream? (and how can i do the same job as if i were using infile>>ch)
thanks in advance!
0
Comment
Question by:lordiano
  • 7
  • 2
9 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 10928691
Can't you use Read to read the amount of bytes that makes the header and then use Read or ReadByte to read teh RGB bytes ?

public: int Read(
   [
   In,
   Out
] unsigned char array __gc[],
   int offset,
   int count
);

Reading lines from a binary files is not rational as there are no lines, its a stream of chars.
0
 

Author Comment

by:lordiano
ID: 10928716
the thing is the HeaderFile are not of the same size. Becuaes in ppm standard there can be comment line in the header file..
0
 
LVL 6

Accepted Solution

by:
Mafalda earned 250 total points
ID: 10928764
My previos comment addresed class FileStream.

Your code doesn't work mainly because you try to use getline that is used for text files to read information from binary files.
You can try to use an ifstream using the correct approach which is reading with read() char by char or readsome() for reading the header chunck at one go

Anyway if you are using Managed C++ I suggest that you stick to it otherwise you will be converting all the time back and forth ;o)
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10928788
The header file should contain its length somewhere in the first bytes.
You should read this bytes first and then use their contents to calculate and read the whole rest of the header chunk (at once or char by char)

Anyway using getline() is not appropriate.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:Mafalda
ID: 10928795
I ment "The header of the file "
sorry
0
 

Author Comment

by:lordiano
ID: 10928846
Hi Mafalda, thanks for input.
This is what the header file looks like
P6
#comment line (does not have to be here)
300 300
255
theRGB data goes here

I cannot really get the size of the header..
I think the data i am getting is Off because when the image is displayed, I can tell its of the same shape as the original PPM but the color is distorded for some reason.

So it must be the infile>>ch thing?
I tried using infile.get() and i get even crappier color.


0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10928945
You could try and read two chars for P6
Then read until the end of comment which will be an end of line or a '\0' char // this size varies
Then read the other bytes you expect (the 300 300 255) and then the RGB data.
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10929008
Use a read and not operator>>

Either FileStream.Read or fstream.read

I still dont understand if you changed the code to Managed C++ using FileStream or if you sticked to fstream

Example (for fstream)

fstream f("my file.ppm", ios_base::in);

char c;
while (!f.eof())
{
  f.read(&c, 1); // read one character to c;
  // use c
}
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10929041
When you use operator>> it reads for you as many characters as needed for each type.
In example

int i;
short s;
char c;
f >> i; // reads 4 bytes
f >> c; // read one byte
f >> s; // reads 2 bytes

This might cause your problem
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now