fstream issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MForey
    New Member
    • Jul 2006
    • 1

    #1

    fstream issues

    I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data doesn't show in the next read, or when I open the file itself. Additionally, I get a insufficient parameters error from if I remove the ios flags from the open--from what I have read, isn't fstream supposed to default the flags to ios::in|ios::ou t? I am using BC5 to compile, if that makes a difference.

    My code is currently as follows:


    #include <fstream.h>
    #include <conio.h>

    void fileRead(fstrea m &testFile,ch ar testTxt[]);
    void fileWrite(fstre am &testFile,ch ar newTxt[]);

    void main()
    {
    char testTxt[11]="\0\0\0\0\0\0\ 0\0\0\0\0";
    char newTxt[6]="zxcvb\0";
    fstream testFile;
    testFile.open(" dataFile.txt",f stream::in | fstream::out | fstream::binary );
    fileRead(testFi le,testTxt);
    cout <<"\n\n1. Overwrite"<<end l;
    cout <<"\n2. Re-Read"<<endl;
    int i=0;
    while (i!=27)
    {
    while (!kbhit())
    ;
    i=getch();
    switch(i)
    {
    case 49:
    fileWrite(testF ile,newTxt);
    case 50:
    fileRead(testFi le,testTxt);
    break;
    case 27:
    break;
    default:
    cout <<"Press one or two."<<endl;
    }
    }

    }
    void fileRead(fstrea m &testFile,ch ar testTxt[])
    {
    testFile.seekg( 0,ios::beg);
    testFile.read(t estTxt,10);
    if (!testFile.bad( ))
    cout <<"dataFile txt: "<<testTxt<<end l;
    else
    cout <<"Error!"<<end l;
    testFile.sync() ;
    return;
    }
    void fileWrite(fstre am &testFile,ch ar newTxt[])
    {
    testFile.seekp( 0,ios::beg);
    testFile.write( newTxt,5);
    if (!testFile.bad( ))
    cout <<"Text overwritten!"<< endl;
    else
    cout <<"Error!"<<end l;
    testFile.sync() ;
    return;
    }

    dataFile.txt contains the string
    "asdfghjkl"
  • lalchandra
    New Member
    • Aug 2006
    • 4

    #2
    here is the header file data:

    #ifndef UFileH
    #define UFileH

    #include <stdio.h>
    #include <time.h>
    #include <system.hpp>
    //---------------------------------------------------------------------------
    /* aMode type

    r Open for reading only.
    w Create for writing. .
    a Append; open for writing at end-of-file or create for writing if the file does not exist.
    r+ Open an existing file for update (reading and writing).
    w+ Create a new file for update (reading and writing).
    a+ Open for append; open (or create if the file does not exist) for update at the end of the file.
    b Binary
    t Text

    */


    // File Exceptions
    class EFile
    {
    private:
    int aErrorCode;
    AnsiString aErrorMsg;
    public:
    EFile()
    {
    aErrorCode = 0;
    aErrorMsg = "";
    };
    EFile(int pErrorCode)
    {
    aErrorCode = pErrorCode;
    aErrorMsg = "";
    };
    EFile(int pErrorCode,Ansi String pErrorMsg)
    {
    aErrorCode = pErrorCode;
    aErrorMsg = pErrorMsg;
    };
    int ErrorCode()
    {
    return aErrorCode;
    };
    AnsiString ErrorMsg()
    {
    return aErrorMsg;
    };
    };
    class ENoClose:EFile
    {
    };
    class ECantClose:EFil e
    {
    };
    class ENoRemove:EFile
    {
    };
    class ENoRename:EFile
    {
    };
    class ENoSeek:EFile
    {
    };
    class ENoOpen:EFile
    {
    };

    class TFile
    {
    private:
    AnsiString aFileName;
    bool aOpen;
    FILE *fFile;
    public:
    TFile(AnsiStrin g pFileName);
    ~TFile();
    void Close();
    bool Eof();
    bool FileExists();
    long FilePos();
    long FileSize();
    bool IsOpen()
    {
    return aOpen;
    }
    void Open(const char *pMode);
    int Read(void *pInfo, size_t pSize, size_t pCount = 1);
    void Remove();
    void Rename(AnsiStri ng pName);
    void SeekFromCur(lon g pPositions);
    void SeekFromEnd(lon g pPosition);
    void SeekTo(long pPosition);
    int Write(void *pInfo, size_t pSize, size_t pCount = 1);

    };
    //---------------------------------------------------------------------------

    #endif







    here is the implementation (.cpp)

    #include <vcl.h>

    #pragma hdrstop

    #include "UFile.h"


    //---------------------------------------------------------------------------
    #pragma package(smart_i nit)

    //---------------------------------------------------------------------------

    TFile::TFile(An siString pFileName)
    {
    aFileName = pFileName;
    aOpen = false;
    }
    //---------------------------------------------------------------------------

    TFile::~TFile()
    {
    try
    {
    Close();
    }
    catch(...)
    {
    };

    }
    //---------------------------------------------------------------------------

    void TFile::Open(con st char *pMode)
    {
    fFile = fopen( aFileName.c_str (), pMode);
    if (fFile == NULL)
    throw ENoOpen();
    aOpen = True;
    }
    //---------------------------------------------------------------------------

    bool TFile::Eof()
    {
    return feof(fFile);
    }
    //---------------------------------------------------------------------------

    void TFile::Remove()
    {
    if (!aOpen)
    {
    if(remove(aFile Name.c_str()) != 0)
    throw ENoRemove();
    }
    else
    throw ENoClose();
    }
    //---------------------------------------------------------------------------

    bool TFile::FileExis ts()
    {
    return ::FileExists(aF ileName);
    }
    //---------------------------------------------------------------------------

    void TFile::Close()
    {
    if (aOpen)
    {
    if(fclose(fFile ) == 0)
    aOpen = false;
    else
    throw ECantClose();
    }
    }
    //---------------------------------------------------------------------------

    int TFile::Write(vo id *pInfo, size_t pSize, size_t pCount)
    {
    return fwrite(pInfo, pSize, pCount, fFile);
    }
    //---------------------------------------------------------------------------

    int TFile::Read(voi d* pInfo, size_t pSize, size_t pCount)
    {
    return fread(pInfo, pSize, pCount, fFile);
    }
    //---------------------------------------------------------------------------

    void TFile::SeekTo(l ong pPosition)
    {
    if (fseek(fFile, pPosition, SEEK_SET) != 0)
    throw ENoSeek();
    }
    //---------------------------------------------------------------------------

    void TFile::SeekFrom Cur(long pPositions)
    {
    if(fseek(fFile, pPositions, SEEK_CUR) != 0)
    throw ENoSeek();
    }
    //---------------------------------------------------------------------------

    void TFile::SeekFrom End(long pPosition)
    {
    if(fseek(fFile, pPosition, SEEK_END) != 0)
    throw ENoSeek();
    }
    //---------------------------------------------------------------------------

    long TFile::FilePos( )
    {
    return ftell(fFile);
    }
    //---------------------------------------------------------------------------

    long TFile::FileSize ()
    {
    long position = ftell(fFile);

    fseek(fFile, 0, SEEK_END);
    long result = ftell(fFile);

    fseek(fFile, position, SEEK_SET);

    return result;
    }
    //---------------------------------------------------------------------------

    void TFile::Rename(A nsiString pName)
    {
    if (!aOpen)
    {
    if(rename(aFile Name.c_str(),pN ame.c_str()) != 0)
    throw ENoRename();
    else
    aFileName = pName;
    }
    else
    throw ENoClose();
    }
    //---------------------------------------------------------------------------


    good luck

    Comment

    Working...