?
Solved

Weird problem working with text files,can some one help?

Posted on 2003-03-28
3
Medium Priority
?
203 Views
Last Modified: 2010-04-01
ok, 1st thnx for read me.

the following is how the proggy read the file
fstream database;
database.open(filename,ios::in | ios::out);
  while (!database.eof())
  {
     database.getline(persona[cont].nombre,60,'\n');
     if(strlen(persona[cont].nombre)==0) {cont=0;break;}
     database.getline(persona[cont].telefono,20,'\n');
     database.getline(persona[cont].ciudad,30,'\n');
     database.getline(persona[cont].edad,10,'\n');
     cont++;
  }
database.close();

then this is how i save the file....

cont=0;
database.open(filename,ios::out);
database.seekg (0, ios::beg);
for(;cont<max;cont++)
   {
   database << persona[cont].nombre <<endl;
   database << persona[cont].telefono <<endl;
   database << persona[cont].ciudad << endl;
   database << persona[cont].edad <<endl;
   }
database.close();

well all that works fine when the proggy runs the 1st and 2nd time... but at the 3er time the file just get deleted well not deleted but all the data inside is missing :(...
i really need help.

Thnx again u guys.
0
Comment
Question by:aagc
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 340 total points
ID: 8229343
I think the reason the data file is empty is because you're not going through the main loop to write anything out:
for(;cont<max;cont++)
  {
  database << persona[cont].nombre <<endl;
  database << persona[cont].telefono <<endl;
  database << persona[cont].ciudad << endl;
  database << persona[cont].edad <<endl;
  }

That in turn must be because the condition is failing: (cont < max).

So I'm guessing that "max" is being set to 0 somehow.
Your code doesn't show how max is computed, so I can't
be more specific.

Most likely you set max from "cont" at the end of the read phase.  If that guess is correct, then your problem is
that this line:
 if(strlen(persona[cont].nombre)==0) {cont=0;break;}
is firing, which basically says "if there's any entry with a zero length name, discard the entire file".

Doug
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9566434
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: dpearson

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question