473,508 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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;
}
Jul 23 '05 #1
6 2347
On Thu, 17 Mar 2005, Sea Squid wrote:
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
Jul 23 '05 #2
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;


"Reidar Øksnevad" <re******@ifi.uio.no> wrote in message
news:Pi******************************@kaksi.ifi.ui o.no...
On Thu, 17 Mar 2005, Sea Squid wrote:
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

Jul 23 '05 #3
"Sea Squid" <Se*******@hotmail.com> schrieb im Newsbeitrag news:42******@news.starhub.net.sg...
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
Jul 23 '05 #4
No. I didn't connect anything to the parallel port.

"Heinz Ozwirk" <ho**********@arcor.de> wrote in message
news:42***********************@newsread4.arcor-online.net...
"Sea Squid" <Se*******@hotmail.com> schrieb im Newsbeitrag
news:42******@news.starhub.net.sg...
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
Jul 23 '05 #5

"Sea Squid" <Se*******@hotmail.com> wrote in message
news:42********@news.starhub.net.sg...
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.)
Jul 23 '05 #6

"Sea Squid" <Se*******@hotmail.com> schrieb im Newsbeitrag news:42********@news.starhub.net.sg...
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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1946
by: Stephen S M WONG | last post by:
As C++ program supports objects with constructors/destructors/methods and data items, I'll assume C++ implementation on segment model computer, like a 80286 with different access restrictions on...
1
1960
by: LenS | last post by:
If this is the wrong place to post this, please advise better place. Otherwise, I have created the following python program and it works. Running on XP. I think I am now at that stage of learning...
2
3934
by: MLH | last post by:
I would like to modify the following Access Basic code in Access 2.0 app so the field would an automatic counter-type field. Dim DefaultWorkspace As WorkSpace, CurrentDatabase As Database Dim...
19
1459
by: madhur | last post by:
Hello char *a="str"; strcpy(a,"abc"); I have learnt that since "a" is a string literal, it might be allocated on read only memory on some implementations. Is it correct to modify a string...
65
12510
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
62
3962
by: Snis Pilbor | last post by:
Hi, It seems pretty common to return pointers to a static array, for example: char *capitalize( char *name ) { static char buf; sprintf( buf, "%s", name );
5
1537
by: Eric_Dexter | last post by:
I was trying to add this to my project but I must be missing some includes or there is a serius error somewhere Anthra Norell wrote:
8
3457
by: raghu | last post by:
Can anyone please tell me what are the different segments in a C program? Where can I find more details regarding this? Please help. Merry Christmas and Happy New Year Regards, Raghu
83
3852
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
4
6965
by: jesjak | last post by:
hi all, can any one give me some explanation of "wat is .bss segment in object file" and what it will contain and difference between data segment and .bss segment.... help me.... thanks, jes
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.