Solved

problem with fstream

Posted on 1998-09-25
6
210 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

912 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

20 Experts available now in Live!

Get 1:1 Help Now