我正在寻找一些帮助我的程序随机访问我的文件。我希望用户在我的文件中输入他们想要访问的记录并将其显示给他们。我在一些网站上发现了一些代码,它们的运行方式与我想要的方式非常接近,但我并不理解。这是我项目的所有代码。 RandomReadProcess()
是我的导师通过电子邮件发送给我的一个例子,RandomReadGiveMeFileLocation()
就是我现在正在做的工作。 `
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class CFile
{
public:
CFile(){}
///////////////////////////////////////////////////////////////////////////////Opening and Closing Files
void OpenTheFileWrite()
{
WRITE.open("File20.txt",ios::app);
}
void CloseTheFileWrite()
{
WRITE.close();
}
void OpenTheFileRead()
{
READ.open("File20.txt");
}
void CloseTheFileRead()
{
READ.close();
}`enter code here`
void OpenTheFileWriteANewOne()
{
WRITE.open("File21.txt");
}
void CloseTheFileWriteANewOne()
{
WRITE.close();
}
void OpenForRandomAccess()
{
ReadWrite.open("File21.txt",ios::in | ios::out );
}
void CloseForRandomAccess()
{
ReadWrite.close();
}
/////////////////////////////////////////////////////////////////////////////// Spacing for the data in file20.txt
void FormatDataForWriting()
{
int k;
// Clean the Buffer
for ( k = 0; k < 47; k++ )
{
Data[k] = 32;// ascii code for space
}
Data[46] = '\0';
for ( k = 0; k < strlen(FirstName); k++ )
{
Data[k] = FirstName[k];
}
for ( k = 0; k < strlen(LastName); k++ )
{
Data[k+15] = LastName[k];
}
for ( k = 0; k < strlen(SSN); k++ )
{
Data[k+35] = SSN[k];
}
}
////////////////////////////////////////////////////////////////////////////// Writes the data to File20.txt
void WriteRecords()
{
int k,HowMany;
cout << "\n\t\tInput how many records : ";
cin >> HowMany;
for ( k = 0; k < HowMany; k++ )
{
cout << "\n\t\tFirstName : ";
cin >> FirstName;
cout << "\t\tLastName : ";
cin >> LastName;
cout << "\t\tSSN : ";
cin >> SSN;
FormatDataForWriting();
WRITE << Data;
}
}
/////////////////////////////////////////////////////////////////////////////
void ReadRecords()
{
string MeUp,HolderOne;
int k,m;
OpenTheFileRead(); /////////opens File20.txt
getline(READ,MeUp); /////////Reads all of File20.txt
CloseTheFileRead(); /////////closes File20.txt
OpenTheFileWriteANewOne(); //////Opens File21.txt
for ( k = 0; k < MeUp.length(); k++ ) /////
{
if ( MeUp.length() % 46 == 0 ) //// Oragnizes the input of the file.
{
HolderOne = MeUp.substr(k,46); //// Everything goes down.
HolderOne += 13; ////
HolderOne += 10; ////
WRITE << HolderOne; /////
}
k = k + 45;
}
CloseTheFileWriteANewOne(); //////Closes File21.txt
}
////////////////////////////////////////////////////////////////////////////////
void RandomReadProcess()
{
char Buffer[255];
int k,HowManyRecords;
OpenForRandomAccess();
ReadWrite.seekg(0,ios::end);
k = ReadWrite.tellg();
HowManyRecords = k / 47;
for (k = 0; k < HowManyRecords; k++)
{
ReadWrite.seekg(k*48,ios::beg);
ReadWrite.read(Buffer,47);
Buffer[46] = '\0';
// cout << "\n\t\tThe record read = " << Buffer;
}
CloseForRandomAccess();
}
//////////////////////////////////////////////////////////////////////////////////
void RandomReadGiveMeFileLocation()
{
int k,m,RecordNumber;
OpenForRandomAccess();
RandomReadProcess();
cout << "\n\t\t\tGive me Record # : ";
cin >> RecordNumber;
ReadWrite.seekg(RecordNumber*sizeof(ReadWrite),ios::beg);
ReadWrite.read(reinterpret_cast<char *>(&ReadWrite),sizeof(ReadWrite));
cout << "\n\t\t\tFirstName: ";
cout << ReadWrite.Data << endl;
cout << "\n\t\t\tLastName: ";
// cout << ReadWrite. << endl;
cout << "\n\t\t\tSSN: ";
// cout << ReadWrite.SSN << endl;
CloseForRandomAccess();
}
//////////////////////////////////////////////////////////////////////////////////
private:
char FirstName[16];
char LastName[21];
char SSN[10];
char Data[48];
ofstream WRITE;
ifstream READ;
fstream ReadWrite;
};