?
Solved

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

Posted on 2003-03-28
3
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

770 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