exe runs from BCB IDE and on double-click, won't run from cmd line... help!

A

aldonnelley

Hi there,

I'm working with BCB6, and I'm having a strange error. I've tried
to find a solution elsewhere, but I've got everyone scratching their
heads...

What's happening is that any exe I build in BCB6 that opens another
file will run fine if I run it from the IDE, and will run if I
double-click the exe, but when I try to run it from the command line,
it doesn't find the file specified. That is, I'll get the error code
specified for a file not found.

I've written a simple 'readlines' script to eliminate the possibility
that there's some problem with the more complex scripts I'm trying to
run, and I still have the same problem. (script attached)
The file "haarcascade_frontalface_alt.xml" is in the project folder
with the exe, and I've added that folder to the include path and
library path (not that that's necessary for this script, I think).
Obviously, for this script, I get the "error opening output file" error
message.

Anyone have any ideas what I'm doing wrong? Any and all assistance
would be greatly appreciated.

Cheers, Al.

//Code Starts
//---------------------------------------------------------------------------

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
string str;
ifstream haarfile("haarcascade_frontalface_alt.xml");
if (!haarfile) //test file open
{
cout << "error opening output file" <<endl;
return -1;
}
while (! haarfile.eof()) //loop through lines
{
getline(haarfile, str);
cout << str << endl;
}
haarfile.close();
return 0;
}

//---------------------------------------------------------------------------
//Code ends
 
I

Ian Collins

Hi there,

I'm working with BCB6, and I'm having a strange error. I've tried
to find a solution elsewhere, but I've got everyone scratching their
heads...
BCB6?

I think you'd better find a group specific to your environment.
 
M

Marcus Kwok

Sorry, I do not have a solution to your problem, but I did see some
things that should be changed in your code (see below)

Hi there,

I'm working with BCB6, and I'm having a strange error. I've tried
to find a solution elsewhere, but I've got everyone scratching their
heads...

What's happening is that any exe I build in BCB6 that opens another
file will run fine if I run it from the IDE, and will run if I
double-click the exe, but when I try to run it from the command line,
it doesn't find the file specified. That is, I'll get the error code
specified for a file not found. [snip]

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
string str;
ifstream haarfile("haarcascade_frontalface_alt.xml");
if (!haarfile) //test file open
{
cout << "error opening output file" <<endl;
return -1;

IIRC the only portable values that main() can return are 0, EXIT_SUCCESS
(usually #defined to be 0), and EXIT_FAILURE. The latter two are
declared in said:
}
while (! haarfile.eof()) //loop through lines
{
getline(haarfile, str);
cout << str << endl;
}

This will loop one too many times. Better is:

while (getline(harrfile, str)) {
cout << str << endl;
}

See http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5 for
more info (and be sure to read the whole FAQ too, there is lots of good
stuff in there).
haarfile.close();

Not strictly necessary as the file will be closed by ifstream's
destructor when haarfile goes out of scope.
return 0;

Also not strictly necessary in C++, though some people like to have it
explicitly there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top