473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

fstream issues

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
1 5325
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

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

Similar topics

5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
7
by: Computer Whizz | last post by:
Hi, I was just wondering if someone would like to comment on these two issues. I had a 15 minute wander around some sites and was curious about loading files (plain ASCII I think will do for a...
4
by: Pablo | last post by:
At my current job I've been tasked with maintaining a very old C++ application written largely in ANSI c++ in what I believe to be with an old DOS based Borland compiler. I'm trying to add a...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.