ofstream problem opening file

Discussion in 'C++' started by Alfons, Feb 6, 2005.

  1. Alfons

    Alfons Guest

    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_Filename);
    }

    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;
    }
    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
     
    Alfons, Feb 6, 2005
    #1
    1. Advertising

  2. Alfons

    Guest

    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
    -directory
    - {
    - 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::eek:ut);
    - 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;
    -}
     
    , Feb 6, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tornado
    Replies:
    1
    Views:
    811
    Mike Wahler
    Jul 29, 2003
  2. steve
    Replies:
    2
    Views:
    747
    Raoul Gough
    Aug 20, 2003
  3. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    633
    Ivan Johansen
    Jul 14, 2005
  4. Replies:
    6
    Views:
    1,592
    James Kanze
    Feb 12, 2008
  5. fniles
    Replies:
    0
    Views:
    301
    fniles
    Apr 26, 2009
Loading...

Share This Page