473,699 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extracting records from a file

Hi!
I have to write the records of a vector in a file, e and then open this file
to extract the record to refill the vector.

My program has two class: Visita(Appointm ent) and Data(date). The second one
is used in the first one such as a field. I wrote the scrivi() function to
write the record in a file called "archivio.t xt" and it works fine.
The I wrote the carica() function to load the records that I have placed on
the file, but I don't know what I have mistaken.
In debugging I saw that the while loop "while( ifs.get() != EOF )", that I
used to read the content of the file untill the end, stops after only one
step. In fact my after calling the function carica() I have only a record in
my vector "diario".
Can someone help me?
I have copy pasted a part of the code, if you need more, tell me.

tnx a loooooooot...

ps. Sorry for my bad english.

--
Aleander

<code>
/* CLASS Visita */
class Visita {
friend std::istream& operator>>( std::istream& in, Visita& v);
friend std::ostream& operator<<( std::ostream& out, const Visita& v);

public:
Visita(string n = "", string c= "", string t= "", Data r =
Data(01,01,1900 ) )
: nome(n), cognome(c), telefono(t), dataApp(r) {};

Visita( const Visita &v );

char* str(); // converte un oggetto visita in uno char*

bool operator<(Visit a &v);

string getNome() const { return nome; }
string getCognome() const { return cognome; }
string getTelefono() const { return telefono; }
Data getData() const { return dataApp; }

Visita& setNome(string n){ nome=n; return *this; }
Visita& setCognome(stri ng c){ cognome=c; return *this; }
Visita& setTelefono(str ing t){ telefono=t; return *this; }
Visita& setData(int g,int m,int a){ dataApp=Data(g, m,a); return
*this; }

// private:
string nome;
string cognome;
string telefono;
Data dataApp;
};

// CLASS Data
class Data{

friend ostream& operator<<( ostream& output, const Data &d);
friend istream& operator>>( istream& input, Data &d);
friend class Visita;
public:
Data(int g=3, int m=3, int a=1903); // costruttore
Data( const Data &d);

Data& operator=( const Data &r );

int getGiorno() const { return giorno; } //funzioni di utilità get
data
int getMese() const { return mese; }
int getAnno() const { return anno; }

private:
int giorno;
int mese;
int anno;
};

// FUNCTION THAT SAVES ALL ON A FILE
template<class T>
void salva(){

ofstream ofs("archivio.t xt", ios::out);
if (!ofs)
cerr << "Impossibil e aprire file!";

ofs.seekp(0); // si posiziona all'inizio del file

for (int i=0; i< diario.size(); i++){
ofs.seekp(i*91) ;
ofs.setf( ios::left );
ofs << setw(30) << diario[i].getNome()
<< setw(30) << diario[i].getCognome()
<< setw(20) << diario[i].getTelefono()
<< diario[i].getData()
<< '\n';
}
ofs.close();
}

/* FUNCTION THAT LOAD THE FILE
template<class T>
void carica(){
ifstream ifs("archivio.t xt", ios::in );

if ( !ifs ){
cerr << "File could not be opened." << endl;
system("PAUSE") ;
mostraMenu();
}else{

diario.clear();
char nom[30];
char cog[30];
char tel[20];
Data data;

int i=0;
while( ifs.get() != EOF ){
ifs.seekg(i*91) ;
ifs >> setw(30) >> nom
setw(30) >> cog
setw(20) >> tel
data;


Visita buffer;
buffer.setNome( nom);
buffer.setCogno me(cog);
buffer.setTelef ono(tel);
buffer.setData( data.getGiorno( ), data.getMese(), data.getAnno() );
cout << "Caricato:\ n" << buffer << endl;
diario.push_bac k(buffer);
i++;
}
}
system("PAUSE") ;

}

</code>
Jul 23 '05 #1
2 1871
On 2005-05-29, Aleander <> wrote:
Hi!
I have to write the records of a vector in a file, e and then open this file
to extract the record to refill the vector.

My program has two class: Visita(Appointm ent) and Data(date). The second one
is used in the first one such as a field. I wrote the scrivi() function to
write the record in a file called "archivio.t xt" and it works fine.
The I wrote the carica() function to load the records that I have placed on
the file, but I don't know what I have mistaken.
In debugging I saw that the while loop "while( ifs.get() != EOF )", that I
used to read the content of the file untill the end, stops after only one
step. In fact my after calling the function carica() I have only a record in
my vector "diario".
Can someone help me?
I have copy pasted a part of the code, if you need more, tell me.


For a start, I don't understand your format. Is it a fixed byte offset
random access file or a text file ? If you're writing and reading a random
access file, then don't use setw (I don't understand what that's for), use
read() to read a fixed number of bytes, and use write() to write a fixed
number of bytes. If you want a text file, use the std::getline function
which writes and reads a line of the file, and write one field to each line.
Alternatively, you could write one record per line by using a delimiter
character, like a comma ',' but you have to be sure that this character
doesn't appear in any of the names if you do that.

Do not use the stream extraction operator for char* pointers (how do you know
that you allocated enough space ?).

A number of additional points:
(1) does it make sense to change the details of an "appointmen t" once it's
been created ? Why all the setX methods ?
(2) don't return char* (think for a moment about the memory ownership issues
/problems here). Return a std::string instead.
(3) don't just whine on stderr and keep going if a function fails. Return
an "unsuccesfu l" error code, or throw an exception.
(4) what are the template params for ? They dont seem to do anything.
(5) don't hard code the filenames

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
Donovan Rebbechi wrote:

A number of additional points:
(1) does it make sense to change the details of an "appointmen t" once it's
been created ? Why all the setX methods ?
(2) don't return char* (think for a moment about the memory ownership issues
/problems here). Return a std::string instead.
(3) don't just whine on stderr and keep going if a function fails. Return
an "unsuccesfu l" error code, or throw an exception.
(4) what are the template params for ? They dont seem to do anything.
(5) don't hard code the filenames


(6) EOF or its usage (despite the fact that the OP used it in a wrong way) has
no use in writing/reading of fixed size record oriented files.
Create an entry, usually at the beginning of the file, and store the number
of records there.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2944
by: Gnaneshwar Babu | last post by:
Hi I am facing a problem with extracting event logs of win32 to a file. Am using the following code to extract eventlogs to file use Win32::EventLog; $handle=Win32::EventLog->new("System", $ENV{ComputerName}) or die "Can't open Application EventLog\n"; $handle->GetNumber($recs) or die "Can't get number of EventLog records\n";
2
2532
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine accessing the abobe mentioned asp page. It WORKS for every type of file when I change the content type according to the file type, but it won't work with self extracting files. When an end user downloads a self extracting file by accessing the...
4
15845
by: Otis Hunter | last post by:
I have been given an Access Database which contains a table that has an OLE object field that contains a Word document. That table contains hundreds of records. I would like to find out how I can write a VB script, to be executed either within Access or executed at the CMD prompt, which will loop through all the records and open the document object and save it to a Word document that I can access from Windows Explorer. An additional info...
3
1926
by: Alfred | last post by:
Hi I would like to extract only 15 records at a time from the backend in alfabetic order. Click on a button and then the next 15. Reason data must come over a 56k modem. The data is not alphabetticaly in database. Any ideas how to right such a function thanks alfred
0
3722
by: Nadav | last post by:
Hi, Introduction: *************************** I am using the MSI API to extract MSI embedded files, I do this by iterating through all of the records in the ‘_Streams’ table and dumping each to a local directory on the disk, the following illustrate this: 01)MsiDatabaseOpenView(m_hMSI, L"SELECT `Name`,`Data` FROM `_Streams`", &hStreamsView)
0
1565
by: Sunil Basu | last post by:
Hi, I have a interesting thing to know and discuss with you. I am extracting data from an Excel file in a Delphi DbGrid through SQL. I want to create a criteria on a specific cell value of the excel file. I want to retrieve a record if the value of a particular column in excel say D4 is < 0 where D is the column and D4 the cell value. If this condition is met then we display that record in the Delphi DBGrid. Is this possible through a...
6
4447
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has some one experience with other libaries to
4
3013
by: apatel85 | last post by:
Hey Guys, Total Number of Records (Based on 5 fields): 1000 Total Unique Records (Based on 5 Fields): 990 Total number of fields: 5 I have question regarding extracting duplicates from the dataset. I have 2 fields that makes a record unique. I have used group by function to find duplicates and got 10 records that are duplicating. Each records duplicating 1 times, thus, 10 unique records and 10
2
2001
by: zenosan | last post by:
SunOS bsm1b 5.9 Generic_118558-09 sun4u sparc SUNW,Netra-440 This is perl, v5.6.1 built for sun4-solaris I have listed the platform/OS information for your reference. I need to extract records from a log file with the following format: |O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183 line 1
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.