• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need C++ help reading input file...

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Blue Jester_2112

Member
Joined
May 11, 2001
Hey all, I'm working through my first C++ class and I'm a bit stuck on one of my assignemts. I'm absolutely not asking anyone to do my homework, but I am stuck and would appreciate any help or advice you can offer.
Basically I'm having trouble figuring out how to read a comma delimited text file consisting of text scores into and array. The textbook I have is really terse and not very helpful in this area and I've searched all over the web for information but haven't had any luck. Can anyone point me in the right direction or to some good sites that might be of use with this?

Thanks for any help you can offer.
 
Excellent, thanks for the tip, my textbook only glanced over this function about 300 pages back and I had forgotten all about it.
So would something like this work once I opened the source file? (I’m at work right now so I can’t actually test it out; I’m just trying to get a head start in my brain before I get home.)
I need it to read scores in the range of 0-200 into the array from the source file.
Am I headed in the right direction here?

Thanks again!

getline( 1, x, ‘,’ );

while (“infile” != ‘/n’)
{
for (index = 0, index < 10, index++);
{
array[index] = static_cast<int>(x);
getline( 3, x, ‘,’ );
}
}
 
Last edited:
You're using fstream and iostream libraries, right?

Here's a quick version which doesn't assume that the text file has any data (be sure to include the iostreeam.h and fstream.h headers). I'll spare you the drugery of using a dynamically created array and stick with a static one.

Snippet (for a maximum of 20 scores):

Code:
int myarray[20];
ifstream fin;
int i = 0;

fin.open("file", ios::in);
assert(!fin.fail());

//prime the input pump - make sure the file has data
fin >> myarray[i];

while(!fin.eof() && i <= 19) {
     fin >> myarray[i];
     i++;
}

fin.close();
assert(!fin.fail());
 
Back