Thread: Backward fstream?

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    Backward fstream?

    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Project24bmp hd\Makefile.win"
    Executing  make...
    make.exe -f "C:\Dev-Cpp\Project24bmp hd\Makefile.win" all
    g++.exe -c loadbmp.cpp -o loadbmp.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/fstream.h:31,
                     from loadbmp.cpp:7:
    C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    loadbmp.cpp: In member function `int CRaster::LoadBMP(char*)':
    loadbmp.cpp:106: error: `ios' has not been declared
    loadbmp.cpp:106: error: `in' undeclared (first use this function)
    loadbmp.cpp:106: error: (Each undeclared identifier is reported only once for each function it appears in.)
    loadbmp.cpp:106: error: `ios' has not been declared
    loadbmp.cpp:106: error: `binary' undeclared (first use this function)
    
    loadbmp.cpp:114:19: warning: multi-character character constant
    loadbmp.cpp:134: error: `ios' has not been declared
    loadbmp.cpp:134: error: `beg' undeclared (first use this function)
    
    make.exe: *** [loadbmp.o] Error 1
    
    Execution terminated
    example code from c++

    Code:
    // *************************************************************
    //                       BMP LOAD EXAMPLE
    //         Written by Juan Soulie <[email protected]>
    // *************************************************************
    
    #include <windows.h>
    #include <fstream.h>
    
    char szAppName [] = "BMPLoad";
    
    LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
    
    // **********
    // class CRaster
    //   - Generic class for BMP raster images.
    class CRaster {
    	public:
    		int Width,Height;		// Dimensions
    		int BPP;				// Bits Per Pixel.
    		char * Raster;			// Bits of the Image.
    		RGBQUAD * Palette;		// RGB Palette for the image.
    		int BytesPerRow;		// Row Width (in bytes).
    		BITMAPINFO * pbmi;		// BITMAPINFO structure
    
    		// Member functions (defined later):
    		int LoadBMP (char * szFile);
    		int GDIPaint (HDC hdc,int x,int y);
    };
    
    // **********
    // Windows Main Function. 
    //   - Here starts our demo program
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR lpCmdLine, int nCmdShow )
    {
    	HWND hwnd;
    	MSG msg;
    
    	WNDCLASS wc;
    	wc.style = CS_HREDRAW|CS_VREDRAW;
    	wc.lpfnWndProc = WindowProc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    	wc.lpszMenuName = NULL;
    	wc.lpszClassName = szAppName;
    
    	RegisterClass (&wc);
    
    	hwnd = CreateWindow (szAppName,"BMP Load",WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
    		0,0,hInstance,0);
    
    	ShowWindow (hwnd,nCmdShow);
    	UpdateWindow (hwnd);
    
    	while (GetMessage(&msg,0,0,0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    
    	return msg.wParam;
    }
    
    // **********
    // Main Window Procedure.
    //   - Processes Window Messages
    LRESULT CALLBACK WindowProc
    	(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static CRaster bmp;
    	HDC hdc;
    	PAINTSTRUCT ps;
    
    	switch (message)
    	{
    		case WM_CREATE:
    			bmp.LoadBMP ("example.bmp");
    			return 0;
    		case WM_PAINT:
    			hdc=BeginPaint (hwnd,&ps);
    			bmp.GDIPaint (hdc,10,10);
    			EndPaint (hwnd,&ps);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage (0);
    			return 0;
    	}
    	return DefWindowProc (hwnd,message,wParam,lParam);
    }
    
    // **********
    // CRaster::LoadBMPFile (FileName);
    //   - loads a BMP file into a CRaster object
    //   * supports non-RLE-compressed files of 1, 2, 4, 8 & 24 bits-per-pixel
    int CRaster::LoadBMP (char * szFile)
    {
    	BITMAPFILEHEADER bmfh;
    	BITMAPINFOHEADER bmih;
    
    	// Open file.
    	ifstream bmpfile (szFile , ios::in | ios::binary);
    	if (! bmpfile.is_open()) return 1;		// Error opening file
    
    	// Load bitmap fileheader & infoheader
    	bmpfile.read ((char*)&bmfh,sizeof (BITMAPFILEHEADER));
    	bmpfile.read ((char*)&bmih,sizeof (BITMAPINFOHEADER));
    
    	// Check filetype signature
    	if (bmfh.bfType!='MB') return 2;		// File is not BMP
    
    	// Assign some short variables:
    	BPP=bmih.biBitCount;
    	Width=bmih.biWidth;
    	Height= (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absoulte value
    	BytesPerRow = Width * BPP / 8;
    	BytesPerRow += (4-BytesPerRow%4) % 4;	// int alignment
    
    	// If BPP aren't 24, load Palette:
    	if (BPP==24) pbmi=(BITMAPINFO*)new char [sizeof(BITMAPINFO)];
    	else
    	{
    		pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
    		Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
    		bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));
    	}
    	pbmi->bmiHeader=bmih;
    
    	// Load Raster
    	bmpfile.seekg (bmfh.bfOffBits,ios::beg);
    
    	Raster= new char[BytesPerRow*Height];
    
    	// (if height is positive the bmp is bottom-up, read it reversed)
    	if (bmih.biHeight>0)
    		for (int n=Height-1;n>=0;n--)
    			bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);
    	else
    		bmpfile.read (Raster,BytesPerRow*Height);
    
    	// so, we always have a up-bottom raster (that is negative height for windows):
    	pbmi->bmiHeader.biHeight=-Height;
    
    	bmpfile.close();
    
    	return 0;
    }
    
    // **********
    // CRaster::GDIPaint (hdc,x,y);
    // * Paints Raster to a Windows DC.
    int CRaster::GDIPaint (HDC hdc,int x=0,int y=0)
    {
    	// Paint the image to the device.
    	return SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
    		0,Height,(LPVOID)Raster,pbmi,0);
    }
    More curious about the error. I can load bitmaps and display them. I downloaded this demo bitmap file from c++. Added <iostream> and this prog still gave the same error of backward fstream.

    Also could you explain why “class CRaster {“ is a class being used instead of a struct? Which is a better way and why?

    What does it take to “fix” this prog to compile and run with dev-cpp? meow.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Try including <fstream> instead of <fstream.h>. Also, you should be including <iostream>

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You also need to add this after your #include statements:
    Code:
    using namespace std;
    or prefix all C++ classes with std:: like this:
    Code:
    std::cout << "blah" << std::endl;
    std::ifstream file( "filename", std::ios::in | std::ios::binary );

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Project24bmp hd\Makefile.win"
    Executing  make...
    make.exe -f "C:\Dev-Cpp\Project24bmp hd\Makefile.win" all
    g++.exe -c loadbmp4.cpp -o loadbmp4.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    loadbmp4.cpp:117:19: warning: multi-character character constant
    
    g++.exe loadbmp4.o  -o "Project24bmp hd.exe" -L"C:/Dev-Cpp/lib"  
    
    loadbmp4.o(.text+0x89a):loadbmp4.cpp: undefined reference to `SetDIBitsToDevice@48'
    collect2: ld returned 1 exit status
    
    make.exe: *** ["Project24bmp] Error 1
    
    Execution terminated
    what is this “ undefined reference to `SetDIBitsToDevice@48' “ ?
    And this “ warning: multi-character character constant “ ?

    Code:
    // *************************************************************
    //                       BMP LOAD EXAMPLE
    //         Written by Juan Soulie <[email protected]>
    // *************************************************************
    
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    char szAppName [] = "BMPLoad";
    
    LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
    
    // **********
    // class CRaster
    //   - Generic class for BMP raster images.
    class CRaster {
    	public:
    		int Width,Height;		// Dimensions
    		int BPP;				// Bits Per Pixel.
    		char * Raster;			// Bits of the Image.
    		RGBQUAD * Palette;		// RGB Palette for the image.
    		int BytesPerRow;		// Row Width (in bytes).
    		BITMAPINFO * pbmi;		// BITMAPINFO structure
    
    		// Member functions (defined later):
    		int LoadBMP (char * szFile);
    		int GDIPaint (HDC hdc,int x,int y);
    };
    
    // **********
    // Windows Main Function. 
    //   - Here starts our demo program
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR lpCmdLine, int nCmdShow )
    {
    	HWND hwnd;
    	MSG msg;
    
    	WNDCLASS wc;
    	wc.style = CS_HREDRAW|CS_VREDRAW;
    	wc.lpfnWndProc = WindowProc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    	wc.lpszMenuName = NULL;
    	wc.lpszClassName = szAppName;
    
    	RegisterClass (&wc);
    
    	hwnd = CreateWindow (szAppName,"BMP Load",WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
    		0,0,hInstance,0);
    
    	ShowWindow (hwnd,nCmdShow);
    	UpdateWindow (hwnd);
    
    	while (GetMessage(&msg,0,0,0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    
    	return msg.wParam;
    }
    
    // **********
    // Main Window Procedure.
    //   - Processes Window Messages
    LRESULT CALLBACK WindowProc
    	(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static CRaster bmp;
    	HDC hdc;
    	PAINTSTRUCT ps;
    
    	switch (message)
    	{
    		case WM_CREATE:
    			bmp.LoadBMP ("example.bmp");
    			return 0;
    		case WM_PAINT:
    			hdc=BeginPaint (hwnd,&ps);
    			bmp.GDIPaint (hdc,10,10);
    			EndPaint (hwnd,&ps);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage (0);
    			return 0;
    	}
    	return DefWindowProc (hwnd,message,wParam,lParam);
    }
    
    // **********
    // CRaster::LoadBMPFile (FileName);
    //   - loads a BMP file into a CRaster object
    //   * supports non-RLE-compressed files of 1, 2, 4, 8 & 24 bits-per-pixel
    int CRaster::LoadBMP (char * szFile)
    {
    	BITMAPFILEHEADER bmfh;
    	BITMAPINFOHEADER bmih;
    
    	// Open file.
    	ifstream bmpfile (szFile , ios::in | ios::binary);
    	if (! bmpfile.is_open()) return 1;		// Error opening file
    
    	// Load bitmap fileheader & infoheader
    	bmpfile.read ((char*)&bmfh,sizeof (BITMAPFILEHEADER));
    	bmpfile.read ((char*)&bmih,sizeof (BITMAPINFOHEADER));
    
    	// Check filetype signature
    	if (bmfh.bfType!='MB') return 2;		// File is not BMP
    
    	// Assign some short variables:
    	BPP=bmih.biBitCount;
    	Width=bmih.biWidth;
    	Height= (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absoulte value
    	BytesPerRow = Width * BPP / 8;
    	BytesPerRow += (4-BytesPerRow%4) % 4;	// int alignment
    
    	// If BPP aren't 24, load Palette:
    	if (BPP==24) pbmi=(BITMAPINFO*)new char [sizeof(BITMAPINFO)];
    	else
    	{
    		pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
    		Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
    		bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));
    	}
    	pbmi->bmiHeader=bmih;
    
    	// Load Raster
    	bmpfile.seekg (bmfh.bfOffBits,ios::beg);
    
    	Raster= new char[BytesPerRow*Height];
    
    	// (if height is positive the bmp is bottom-up, read it reversed)
    	if (bmih.biHeight>0)
    		for (int n=Height-1;n>=0;n--)
    			bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);
    	else
    		bmpfile.read (Raster,BytesPerRow*Height);
    
    	// so, we always have a up-bottom raster (that is negative height for windows):
    	pbmi->bmiHeader.biHeight=-Height;
    
    	bmpfile.close();
    
    	return 0;
    }
    
    // **********
    // CRaster::GDIPaint (hdc,x,y);
    // * Paints Raster to a Windows DC.
    int CRaster::GDIPaint (HDC hdc,int x=0,int y=0)
    {
    	// Paint the image to the device.
    	return SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
    		0,Height,(LPVOID)Raster,pbmi,0);
    }
    oh and thank you for the help. still do not understand the backward error .... is it saying that it is not backward compatible or just simply wrong header?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This
    Code:
    'MB'
    is a multi-character character constant.

    According to the MSDN page for SetDIBitsToDevice , you need to link with Gdi32.lib. "-lgdi32" or some such added to the link stage of the build process [I'm sure there is a nice way to actually ask for that in the Dev-C++, but I'm not familiar with how you go about doing that.]

    The "backward" comes from another include within the compiler specific version of the STL library support files [and "backward" itself is part of the PATH to the file, not part of the actual message from the compiler], and essentially says "You are using old style <x.h> instead of <x>" - it is most likely that you get the SAME content in both x.h and x, and the compiler is recommending that you switch to the newer form, because sooner or later, the old form will be obsolete.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    ok thank you. that helped. i need to find the .a equivalent lib for dev cpp . and link to that .


    edit add

    libgdi32.a got it working!
    Last edited by kryptkat; 11-28-2007 at 07:33 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kryptkat View Post
    ok thank you. that helped. i need to find the .a equivalent lib for dev cpp . and link to that .
    I'm pretty sure it's called the same thing - in fact, I thought dev-cpp was using the standard MS libraries [as it's using the mingw version of gcc - and unless I've completely misunderstood, you just specify the library you want with that]. Try just adding -lgdi32 to your link command.

    something like this:
    g++.exe loadbmp4.o -lgdi32 -o "Project24bmp hd.exe" -L"C:/Dev-Cpp/lib"

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. using fstream
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 04:53 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM