Solved

loading bmp from file to TImage

Posted on 2002-04-20
2
610 Views
Last Modified: 2010-05-18
I am using the code below to try and load a filepath to a bmp file from a txt file. It compiles fine but when I get to the LoadFromFile stmt I get a runtime error "EFOpenError Cannot open file" and then it displays the path it should open so it is reading it correctly. I am using Borland C++ Builder.  

Can anyone tell me where I have gone wrong please


ifstream in ("Test.txt");    
char s[_MAX_PATH];

if( ! in )
{
  Application->MessageBox("Cannot open file.",
   "MessageBox",MB_OK + MB_DEFBUTTON1);
}
else
{
  Graphics::TBitmap *Bitmap1 = new Graphics::TBitmap();
  in.getline(s,_MAX_PATH);
  Bitmap1->LoadFromFile(s);
  Image1->Picture->Bitmap = Bitmap1;
}
in.close();
0
Comment
Question by:sylvrynne
2 Comments
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 100 total points
ID: 6955829
Maybe, s contains some spaces before or (after)image file. For example,  s = "   c:\x.bmp" is wrong filename

For sure, the filename is correct, you can use access(...) . The following is demo code

    ifstream in ("Test.txt");
    char s[_MAX_PATH];

    if( ! in )
    {
        Application->MessageBox("Cannot open file.",
            "MessageBox",MB_OK + MB_DEFBUTTON1);
    }
    else
    {
        Graphics::TBitmap *Bitmap1 = new Graphics::TBitmap();
        in.getline(s,_MAX_PATH);
        if( access(s,0) != 0 )
        {
            String msg = String("\"") + s + "\"";
            Application->MessageBox("Cannot open image.",
                msg.c_str(),MB_OK );
            return;
        }

        Bitmap1->LoadFromFile(s);
        Image1->Picture->Bitmap = Bitmap1;
    }
    in.close();

0
 

Author Comment

by:sylvrynne
ID: 6956921
You will never believe what it was ... the path had " " marks around it .... just too late last night to see it in the text file  thanx for your help
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
SetCurrentDirectory path limit 7 131
C++ get user from AD  (VS6) 7 87
C++ Language error 28 250
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 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