472,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

ofstream problem opening file


I have build a program that can do file transferring between a Windows
XP computer and a DOS computer via a serial port. The Windows program
I have build in C++ with Visual Studio 6.0. The DOS program I made in
Turbo C++ 3.0.

At this moment I am in a test phase of sending files and directories.
The code I am using in DOS to open a file for writing looks like this
(forgive me the typos, since I only have my source code at work and
don't have any compiler on this PC):

if (!IsDirectory()) // Check to see if the object is a directory

m_pOfstream = new ofstream;
m_pOfstream->open(l_Filename, ios::binary | ios::in);
if (m_pOfstream->good() == 0)
printf("State: %d\n", m_pOfstream->rdstate());
return FALSE;
return TRUE;

At the moment I am having a strange problem saving files to disk. If
no files exist yet, then everything works fine. But when the files do
exist, and the program actually has to remove them first, then I am
getting errors.

Let's say I send the following files and directories:

The first directory is skipped for removing. The opening of the
directory also returns FALSE, but this is no problem, since I don't
have to write any data to a directory name.
The first file (a.bat) gives an unexplainable error on opening the
file! rdstate() returns 4, which means something like an I/O failure I
The strange thing is, that the rest of the files don't give any
problem at all.

If I remove the file a.bat on the target side by hand, then everything
works fine.

The opening of the file and assigning a valid value to the m_pOfstream
pointer are done through the constructorof some CFile class I made,
while the closing and deleting of the pointer are done in the

Does anyone have any suggestions how to solve this problem?

Alfons van Zwol
Jul 23 '05 #1
1 4233
I see as obvious difficulty that you open an ofstream for input.
Also you do not check for errors, changing to code like below works for
me at least on windows, if you run this on a pure DOS box you will get
failures. BTW: this is a bit off topic as it is not strict C++ but OS
specific, but hey it is sunday night, so I hope the police is a bit
relaxed too....
-#include <cstdlib>
-#include <string>
-#include <windows.h>
-#include <fstream>
-#include <iostream>
-#include <stdio.h>-
-using namespace std;
-bool isDirectory(const string& path)
- DWORD dw = GetFileAttributes(path.c_str());
- return ( dw != 0xffffffff && dw & FILE_ATTRIBUTE_DIRECTORY);

-int main(int argc, char *argv[])
- string l_Filename = "C:\\TEMP\\test.txt";
- cout << isDirectory(l_Filename) << false << endl;
- cout << isDirectory("C:\\TEMP") << true <<endl;
- if (!isDirectory(l_Filename)) // Check to see if the object is a
- {
- if(remove(l_Filename.c_str()) == -1)
- {
- cerr << "Error deleteting file " << l_Filename << endl;
- }
- }
- ofstream m_pOfstream;
- m_pOfstream.open(l_Filename.c_str(), ios::out);
- if (m_pOfstream.good())
- {
- cout << "TRUE" << endl;
- }
- else
- {
- cout << "State: " << m_pOfstream.rdstate() << endl;
- cout << "FALSE" << endl;
- }
- m_pOfstream.close();
- system("PAUSE");
- return EXIT_SUCCESS;

Jul 23 '05 #2

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

Similar topics

by: steve | last post by:
Is there a way to catch file errors while writing to a file using ofstream? For ex., if the file is deleted or permissions changed by another process after it is opened, or when the disk is full....
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
by: Gurikar | last post by:
Hello, ofstream ofs; fun(char* str) { ofs<<str<<endl; } void main()
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++...
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
by: Paul LAURENT | last post by:
Hi everybody, I am using the STL "ofstream" class. I open a file using "ofstream" in update at the end mode ("ate"), I can read and write my file correctly. => What I would like to do is...
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
by: askalottaqs | last post by:
dont freak out by length of code! its commented out befiore each block so you dont have 2 read it all.. so anyway! i have a fairly simple question i wld say, all i need is a little guide, i...
by: ednaswap | last post by:
Hello, C++ experts, I am writing a Log class something like below. ================= c++ source code ======================== class Log { public: Log(); Log(string _filename);
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.