By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

fstream issues

P: 1
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::out? 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(fstream &testFile,char testTxt[]);
void fileWrite(fstream &testFile,char 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",fstream::in | fstream::out | fstream::binary);
fileRead(testFile,testTxt);
cout <<"\n\n1. Overwrite"<<endl;
cout <<"\n2. Re-Read"<<endl;
int i=0;
while (i!=27)
{
while (!kbhit())
;
i=getch();
switch(i)
{
case 49:
fileWrite(testFile,newTxt);
case 50:
fileRead(testFile,testTxt);
break;
case 27:
break;
default:
cout <<"Press one or two."<<endl;
}
}

}
void fileRead(fstream &testFile,char testTxt[])
{
testFile.seekg(0,ios::beg);
testFile.read(testTxt,10);
if (!testFile.bad())
cout <<"dataFile txt: "<<testTxt<<endl;
else
cout <<"Error!"<<endl;
testFile.sync();
return;
}
void fileWrite(fstream &testFile,char newTxt[])
{
testFile.seekp(0,ios::beg);
testFile.write(newTxt,5);
if (!testFile.bad())
cout <<"Text overwritten!"<<endl;
else
cout <<"Error!"<<endl;
testFile.sync();
return;
}

dataFile.txt contains the string
"asdfghjkl"
Jul 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: 4
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,AnsiString pErrorMsg)
{
aErrorCode = pErrorCode;
aErrorMsg = pErrorMsg;
};
int ErrorCode()
{
return aErrorCode;
};
AnsiString ErrorMsg()
{
return aErrorMsg;
};
};
class ENoClose:EFile
{
};
class ECantClose:EFile
{
};
class ENoRemove:EFile
{
};
class ENoRename:EFile
{
};
class ENoSeek:EFile
{
};
class ENoOpen:EFile
{
};

class TFile
{
private:
AnsiString aFileName;
bool aOpen;
FILE *fFile;
public:
TFile(AnsiString 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(AnsiString pName);
void SeekFromCur(long pPositions);
void SeekFromEnd(long 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_init)

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

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

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

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

void TFile::Open(const 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(aFileName.c_str()) != 0)
throw ENoRemove();
}
else
throw ENoClose();
}
//---------------------------------------------------------------------------

bool TFile::FileExists()
{
return ::FileExists(aFileName);
}
//---------------------------------------------------------------------------

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

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

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

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

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

void TFile::SeekFromEnd(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(AnsiString pName)
{
if (!aOpen)
{
if(rename(aFileName.c_str(),pName.c_str()) != 0)
throw ENoRename();
else
aFileName = pName;
}
else
throw ENoClose();
}
//---------------------------------------------------------------------------


good luck
Aug 7 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.