How come this C++ segment doesn't work?

S

Sea Squid

I am using Dev-C++ 4.9.8. The console stop after executing "cout << "Written
one iteration" << endl;".
Instructions in "if (! myFile){}" test is not run.

Thanks.





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

int main()
{
int i;
char c;
ofstream myFile("LPT1:");
// ofstream myFile("c:/out.txt");
// Creates an ofstream object named myFile

if (! myFile) // Always test file open
{
cout << "Error opening output file" << endl;
return -1;
}

for(i = 0; i < 5; i++) {
// c = getc();
c = 1;
cout << "Written one iteration" << endl;
myFile << c << endl;
}

myFile.close();

return 0;
}
 
?

=?ISO-8859-1?Q?Reidar_=D8ksnevad?=

I am using Dev-C++ 4.9.8. The console stop after executing "cout << "Written
one iteration" << endl;".
Instructions in "if (! myFile){}" test is not run.

Thanks.





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

int main()
{
int i;
char c;
ofstream myFile("LPT1:");
// ofstream myFile("c:/out.txt");
// Creates an ofstream object named myFile

if (! myFile) // Always test file open
{
cout << "Error opening output file" << endl;
return -1;
}

for(i = 0; i < 5; i++) {
// c = getc();
c = 1;
cout << "Written one iteration" << endl;
myFile << c << endl;
}

myFile.close();

return 0;
}

I wonder if your problem is that you are trying to write the
character 1 to the file. This is translanslated in ASCII to SOH (Start of
heading), and I think it might be illegal to write it directly to a file.

I haven't actually tested it, but i remember having a similar problem a
few years ago.

Regards,
Reidar
 
S

Sea Squid

Thank you for your reply.
Changing that line to the following doesnt help either.
My parallel port is empty, but I can see it in the hardware profile.

myFile << "test" << endl;
 
H

Heinz Ozwirk

Sea Squid said:
I am using Dev-C++ 4.9.8. The console stop after executing "cout << "Written
one iteration" << endl;".
Instructions in "if (! myFile){}" test is not run.

Thanks.





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

int main()
{
int i;
char c;
ofstream myFile("LPT1:");
[...]

Did you swith on your printer (or whatever is connected to LPT1)?

Heinz
 
S

Sea Squid

No. I didn't connect anything to the parallel port.



Sea Squid said:
I am using Dev-C++ 4.9.8. The console stop after executing "cout << "Written
one iteration" << endl;".
Instructions in "if (! myFile){}" test is not run.

Thanks.





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

int main()
{
int i;
char c;
ofstream myFile("LPT1:");
[...]

Did you swith on your printer (or whatever is connected to LPT1)?

Heinz
 
H

Howard

Sea Squid said:
No. I didn't connect anything to the parallel port.

Put code in the loop to check the state of the stream. It may go into an
error state if you write to it when nothing is hooked up there. (Just
guessing.)

-Howard

(BTW, please put your responses after the text you are responding to, or
interspersed with it if appropriate. Newsgroup messages are read
top-to-bottom, so having your answer at the top is confusing to many of us.)
 
H

Heinz Ozwirk

Sea Squid said:
No. I didn't connect anything to the parallel port.

Some systems are really slow to detect a missing device. Give it some time - or map LPT1 to some file or print-queue. But that's not a C++ problem.

Heinz
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top