473,748 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream problem opening file

Hello,

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
{
remove(l_Filena me);
}

m_pOfstream = new ofstream;
m_pOfstream->open(l_Filenam e, ios::binary | ios::in);
if (m_pOfstream->good() == 0)
{
printf("State: %d\n", m_pOfstream->rdstate());
return FALSE;
}
else
{
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:
C:\temp\test\
C:\temp\test\a. bat
C:\temp\test\b. com
C:\temp\test\c. bat

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
believe.
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
destructor.

Does anyone have any suggestions how to solve this problem?

Alfons van Zwol
Jul 23 '05 #1
1 4398
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(con st string& path)
-{
- DWORD dw = GetFileAttribut es(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_F ilename) << false << endl;
- cout << isDirectory("C: \\TEMP") << true <<endl;
-
- if (!isDirectory(l _Filename)) // Check to see if the object is a
-directory
- {
- if(remove(l_Fil ename.c_str()) == -1)
- {
- cerr << "Error deleteting file " << l_Filename << endl;
- }
- }
-
- ofstream m_pOfstream;
- m_pOfstream.ope n(l_Filename.c_ str(), ios::out);
- if (m_pOfstream.go od())
- {
- cout << "TRUE" << endl;
- }
-
- else
- {
- cout << "State: " << m_pOfstream.rds tate() << endl;
- cout << "FALSE" << endl;
- }
- m_pOfstream.clo se();
-
- 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

2
10623
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. I couldn't figure out which members could be used to check for these types of errors. I tried the good(), bad(), fail() etc., after writing to a full disk, deleted file etc.
2
448
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 did below: char filename = {"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};
11
3826
by: Gurikar | last post by:
Hello, ofstream ofs; fun(char* str) { ofs<<str<<endl; } void main()
3
9186
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++ functions that do this or is there any way I can check manually? Thanks, Prashant
15
6956
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 list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
2
8972
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 deleting some lines from the file which decreases the size of the file.
5
5157
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); ofile.write(buffer,sizeof(buffer)); ofile.close(); }
1
4292
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 am trying to write an input stream to write to files, but the file being opened needs to change with each for loop, so i did this ofstream out; for (int j=0;j<24;j++){
5
2969
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);
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.