Solved

problem with fstream

Posted on 1998-09-25
6
212 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

10 Experts available now in Live!

Get 1:1 Help Now