Solved

problem with fstream

Posted on 1998-09-25
6
215 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
[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
  • 2
6 Comments
 

Author Comment

by:bbarnette
ID: 1173773
Edited text of question
0
 

Author Comment

by:bbarnette
ID: 1173774
Edited text of question
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1173775
Does it fail on the 1st record or 2nd record?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1173776
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
ID: 1173777
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
ID: 1173778
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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