Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem reading from a file

Reply
Thread Tools

problem reading from a file

 
 
SB
Guest
Posts: n/a
 
      04-12-2004
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
am time 1....

This is repeated for three people, for five days. So for each person (three
names), there is five days worth of the am/pm time 1/2, appointment data.
The problem I'm having is reading each line and storing it's value in the
corresponding person's class member. I have a Doctor class (which represents
a person in the file) and that class has a five element array of a Day class
(each day's data gets its own Day object). I also have a Schedule class
which contains an array of three Doctor classes. Here is the code for
reading the input file and populating the objects data members...

void Schedule::loadSchedule(ifstream& inputFile)
{
string temp; // temp variable for storing each line read from file
int i; // for doc index
int j; // for day index

// read from input file and populate each doctors data
for (i = 0; i < NUMBER_OF_DOCTORS; i++)
{
getline(inputFile, temp);
doc[i].setName(temp);
cout<<endl<<"name is <"<<doc[i].getName()<<">";
j = 0; // reset day counter for each doctor
for (j; j < NUMBER_OF_DAYS; j++)
{
// make sure we haven't accidentally read beyond the end of the file
if (inputFile.eof())
{
cout<<"End of File has been reached while reading the input
file."<<endl;
exit(1);
}
getline(inputFile, temp);
doc[i].setDay(i, temp);
cout<<endl<<"day is <"<<doc[i].getDay(j)<<">";
getline(inputFile, temp);
doc[i].setAmStart(i, temp);
cout<<endl<<"am start for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmStart(j)<<">";
getline(inputFile, temp);
doc[i].setAmEnd(i, temp);
cout<<endl<<"am end for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmEnd(j)<<">";
getline(inputFile, temp);
doc[i].setAmAppt(i, temp);
cout<<endl<<"am appointment for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmAppt(j)<<">";
getline(inputFile, temp);
doc[i].setPmStart(i, temp);
cout<<endl<<"pm start for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getPmStart(j)<<">";
getline(inputFile, temp);
doc[i].setPmEnd(i, temp);
cout<<endl<<"pm end for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getPmEnd(j)<<">";
getline(inputFile, temp);
doc[i].setPmAppt(i, temp);
cout<<endl<<"pm appointment for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getPmAppt(j)<<">";
}
}

// close the file
inputFile.close();
}

Here is the output from the cout statement in the above code...

name is <Andrew Jones>
day is <Monday>
am start for Monday is <9>
am end for Monday is <12>
am appointment for Monday is <Lecture>
pm start for Monday is <13>
pm end for Monday is <16>
pm appointment for Monday is <Free>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
name is <Eric Roberts>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <Tuesday>
am start for Tuesday is <9>
am end for Tuesday is <12>
am appointment for Tuesday is <Free>
pm start for Tuesday is <13>
pm end for Tuesday is <16>
pm appointment for Tuesday is <Surgery>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
name is <Mathew Hayden>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <Wednesday>
am start for Wednesday is <9>
am end for Wednesday is <12>
am appointment for Wednesday is <Free>
pm start for Wednesday is <13>
pm end for Wednesday is <16>
pm appointment for Wednesday is <Lecture>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>
day is <>
am start for is <>
am end for is <>
am appointment for is <>
pm start for is <>
pm end for is <>
pm appointment for is <>

The problem is the empty less than/greater than signs. I don't know why data
is not being populated for those values. Is it the way I'm using getline or
something? Interestingly, the less than/greater than signs that do contain
data are all correct. I matched them up with the data in the input file and
it matches. So the values that are getting populated are being done in the
correct order. Can anyone see what I'm doing wrong here? Any help is
appreciated!

Thank you in advance!


 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-12-2004
On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <(E-Mail Removed)> wrote:

Without declarations for the data you're working with, folks aren't too
likely to waste their time guessing as to what your problem might be.

I'd suggest re-posting with a /small/ version of your program that exhibits
the erroneous behavior, driven by a /small/ input file that you can show
the exact contents of (I couldn't tell if your input file details were a
description or data or what...but I guessed they were just a description),
and then just show enough of the output to indicate one or two examples of
the failure.

Thanks,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-12-2004
I just noticed you also have all these calls to setThisAndThat(). Include
them too. IOW, show a complete program if you want prompt, useful feedback.
Thanks,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
SB
Guest
Posts: n/a
 
      04-12-2004
Thanks, but I actually figured it out myself. I mistakenly used variable i
instead of j for the setXXX function calls. Although, the output that I
posted was the entire output.

Thanks anyways!

"Leor Zolman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <(E-Mail Removed)> wrote:
>
> Without declarations for the data you're working with, folks aren't too
> likely to waste their time guessing as to what your problem might be.
>
> I'd suggest re-posting with a /small/ version of your program that

exhibits
> the erroneous behavior, driven by a /small/ input file that you can show
> the exact contents of (I couldn't tell if your input file details were a
> description or data or what...but I guessed they were just a description),
> and then just show enough of the output to indicate one or two examples of
> the failure.
>
> Thanks,
> -leor
>
> --
> Leor Zolman --- BD Software --- www.bdsoft.com
> On-Site Training in C/C++, Java, Perl and Unix
> C++ users: Download BD Software's free STL Error Message Decryptor at:
> www.bdsoft.com/tools/stlfilt.html



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments