bbarnette
asked on
problem with fstream
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::bin ary);
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::bi nary);
// retrieve data from file
while((Fout.getline(H_Stat ion_Name,2 5))&& (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()
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::bin
for(ptrType CurrSav = Data; CurrSav != NULL; CurrSav = CurrSav->Next)
{
Fin<<CurrSav->StationName<
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
{
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::bi
// retrieve data from file
while((Fout.getline(H_Stat
{
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()
ASKER
Edited text of question
Does it fail on the 1st record or 2nd record?
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
nietod@theshop.net
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER