Click to See Complete Forum and Search --> : Read file and write to another


April 7th, 1999, 10:17 AM
Hello, I hope someone can halp me. What I'm trying to do is to read a file Lecons.lst and remove the records that I want to delete. The way to do it is to read the file and create temporary file where the records I want to keep will reside. I will than have to delete the lecons.lst and rename the temporary file to lecons.lst. It's easier said than done.

As you can see from the code I have no idea how to create a temporary file. Also the dirsToDelete would have for example (C:\lavac\lecon\l000003) which is also a path we find in the lecons.lst at the PathLecon[64]. I want to compare and is they don't exist I want to write the record into the temporary file.

Can someone help me please..Thank you in advance

void CVRUtilitiesApp::DeleteLessons( const vector<Directory> &dirsToDelete)
{
File f(ToString(iniInfo.GetLessonBasePath()));
f.AppendToPath("Lecons.lst");
f.Open(O_RDONLY);
File tf(ToString(iniInfo.GetLessonBasePath()));
tf.AppendToPath("temp.lst");
tf.Open( O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
VRLessonRecord lr;
while(lr.Read(f)){
for( int i= 0; i < dirsToDelete.size(); i++){
Directory dir = dirsToDelete[i];

if(lr.GetPath() != directory){
char buf[ 1024];
long alire= sizeof buf;
lr.Write(f);
dir.Remove();
dir= dir.GetParent();
vector<Path> childs;
dir.EnumChilds("*.*", childs);
}
}

}
RecalcLessonRawList();
}

int File::Read( void *buf, int lg)
{
SetLastOp();
return read( fh, buf, lg);
}

//==========================================================================
int File::Write( long pos, const void *buf, int lg)
{
Seek( pos, SEEK_SET);
int ret= Write( buf, lg);
lastError= errno;
SetLastOp();
return ret;
}
bool File::Open( int access, int mode)
{
if( (access & O_TEXT) == 0) access|= O_BINARY;
char buf[ LgMaxPath + 1];
strncpy( buf, GetPath(), sizeof buf);
fh= open( buf, access, mode);
lastError= errno;
SetLastOp();
return fh >= 0;
}
Path &Path::AppendToPath( const string &base)
{
if( name.length() == 0)
SetPath( base);
else{
AddPathSeparator();
if( base[ 0] == PathSep)
name.append( base, 1, base.length() - 1);
else
name.append( base, 0, base.length());

name= FullPath( name);
}
return *this;
}

bool VRLessonRecord::Read( File &f)
{
return f.Read( this, sizeof *this) == sizeof *this;
}

bool VRLessonRecord::Write( File &f)
{
return f.Write( this, sizeof *this) == sizeof *this;
}

class VRLessonRecord
{
private:
char Nom[ 64];
char Createur[ 64];
char PathLecon[ 64];
short Langue;
short Syquest;
short Serveur;
char AVideo;
DWORD dwTag;
unsigned short wVer;
unsigned char bAssist;
char Reserved[ 248];

public:
CString GetPath();
CString GetName();
bool Read( File &f);
bool Write( File &f);
};