Solved

problem with fstream

Posted on 1998-09-25
6
207 Views
Last Modified: 2012-05-04
Hi all,

I have had an on going problem with this program I am working on and at this point Iam not sure whether the problem is in my file save function or my file retrieval function.
The problem is that when using getline() (which I need) I can not retrieve the data.

However using the same code except for removing the getline() and using the bit wise operators it works fine until the StationName  has a white space in it. If I remove the stationName all together the rest of the code works perfectly, or if I remove everything except the stationName with getline() it also works perfectly. It almost seems that the combination of the getline() and bitwise operators are the problem.

If the code provided is not adequate let me know and I will be glad to e_mail the code in its entirety.

This is still the same problem from Q.10078727. So the points for both Q's are available.  
I would appreciate any suggestions that might help resolve this problem!

Thanks,
bbarnette
_________________________________________________________________________
void MtrFr::SaveMtrFr(ptrType Data)
// SaveMtrFr() writes all data to HD. Data is a pointer to Head
{

ofstream Fin("mtrconv.txt",ios::binary);

for(ptrType CurrSav = Data; CurrSav != NULL; CurrSav =   CurrSav->Next)
{
Fin<<CurrSav->StationName<<endl;
Fin<<CurrSav->Mon<<" "
<<CurrSav->Day<<" "
<<CurrSav->Yr<<" "
<<CurrSav->Meter_Size<<" "
<<CurrSav->Large_Gear<<" "
<<CurrSav->Small_Gear<<" "
<<CurrSav->Dial_Size<<" "
<<CurrSav->Pulse_Ft<<" "
<<CurrSav->Roc_Conv<<endl;

}
}// end SaveMtrFr()

void MtrFr::Retrieve_Historical_Data()
{
ptrType Tail = NULL;
ptrType CurPtr;
char H_Station_Name[25];
char M_S[5];
unsigned int L_G, S_G ,D_S, Mn, Dy, Yr;
float P_F ,R_C;

Delete_List();

ifstream Fout("mtrconv.txt",ios::binary);

// retrieve data from file
while((Fout.getline(H_Station_Name,25))&& (Fout >> Mn >> Dy >> Yr >> M_S >>  L_G >> S_G >> D_S >> P_F >> R_C))
{
 CurPtr = new TurbMtr;// allocate memory for new node

if(Head != NULL)
{// list already exit, traverse to the next node
Tail->Next = CurPtr;
}
else if(Head == NULL)
{// list does not exit
Head = CurPtr;
Tail = Head;
}

Tail = CurPtr;

strcpy(Tail->StationName, H_Station_Name);
Tail->Mon = Mn;
Tail->Day = Dy;
Tail->Yr = Yr;
strcpy(Tail->Meter_Size, M_S);
Tail->Large_Gear = L_G;
Tail->Small_Gear = S_G;
Tail->Dial_Size = D_S;
Tail->Pulse_Ft = P_F;
Tail->Roc_Conv = R_C;

Tail->Next = NULL;
}  // end while

Fout.close();

}// end Retrieve_Historical_Data()
0
Comment
Question by:bbarnette
  • 3
  • 2
6 Comments
 

Author Comment

by:bbarnette
Comment Utility
Edited text of question
0
 

Author Comment

by:bbarnette
Comment Utility
Edited text of question
0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
Does it fail on the 1st record or 2nd record?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
If you e-mail me the source code and any other files I might need (data files) and/or tell me what data to enter into the program, I will try it.  My address is
nietod@theshop.net
0
 

Author Comment

by:bbarnette
Comment Utility
It does fail on the second record.
I was also told that I could not use getline() with a binary file. So I changed it to a text file(:: in, ::out) and that did not resolve the problem either.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
Problem solved via e-mail.  The problem was that the input function was not reading the end of the last line of data so subsequent attempts to input were reading from the wrong place in the file.

Look for an e-mail on some suggestions later today.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now