fstream problem

B

beepa

As you will be able to see I am fairly new at this. Here is the part I'm
having problems figuring out:
The file I'm inputing from is formated like this:

firstName lastName
postion name (one or two words)
firstName lastName
first base (for example)
firstName lastName
position name (one or two words)

I'm trying to seperate the first name from the last name putting the last
name in parallel arrays with the position in the
other array. The garbage array I set up just to see what I was getting rid
of. When the position has two words it always gets messed up as far as what
it puts in the arrays the code is runable. Any suggestions would be most
welcome. TIA (This is just a C++ win32console program created in an empty
solution using MS Visual Studio 2005)

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
 char sentry;
 string garbage[9];
 string player[9];
 string position[9];
 fstream inFile;
 inFile.open("team1.txt");
 for(int test = 0; test < 9; test++)
 {
  inFile >> garbage[test];
  inFile >> player[test];
  getline(inFile,position[test]);
 }
 for(int test = 0; test < 9; test++)
 {
  cout << "this is garbage " << test << " - " << garbage[test] << endl;
  cout << "this is player " << test << " - " << player[test] << endl;
  cout << "this is position " << test << " - " << position[test] << endl;
 }
 cin.get(sentry);

 return 0;
}
 
Joined
Sep 26, 2007
Messages
6
Reaction score
0
beepa said:
The file I'm inputing from is formated like this:

firstName lastName
postion name (one or two words)
firstName lastName
first base (for example)
firstName lastName
position name (one or two words)

I'm trying to seperate the first name from the last name putting the last
name in parallel arrays with the position in the
other array. The garbage array I set up just to see what I was getting rid
of. When the position has two words it always gets messed up as far as what
it puts in the arrays the code is runable. Any suggestions would be most
welcome. TIA (This is just a C++ win32console program created in an empty
solution using MS Visual Studio 2005)

I have just tried the code. Can you show how u write the text file and what the result? The good one and the bad result one.
 
J

James Kanze

As you will be able to see I am fairly new at this. Here is the part I'm
having problems figuring out:
The file I'm inputing from is formated like this:
firstName lastName
postion name (one or two words)
firstName lastName
first base (for example)
firstName lastName
position name (one or two words)
I'm trying to seperate the first name from the last name
putting the last name in parallel arrays with the position in
the other array.

I'm not too sure what you mean by "parallel arrays", but if I
understand you correctly, each record in the file consists of
two lines. With the first name and the last name on the first
line, and the position on the second.
The garbage array I set up just to see what I was getting rid
of. When the position has two words it always gets messed up
as far as what it puts in the arrays the code is runable. Any
suggestions would be most welcome. TIA (This is just a C++
win32console program created in an empty solution using MS
Visual Studio 2005)
Code:
#include <iostream>
#include <fstream>
#include <string>[/QUOTE]

If you're going to have arrays, you'll need
[QUOTE]
using namespace std;[/QUOTE]
[QUOTE]
int main()
{
char sentry;
string garbage[9];
string player[9];
string position[9];
fstream inFile;
inFile.open("team1.txt");[/QUOTE]

You should check that the open succeeded.
[QUOTE]
for(int test = 0; test < 9; test++)
{
inFile >> garbage[test];
inFile >> player[test];
getline(inFile,position[test]);[/QUOTE]

OK.  First, >> to a string doesn't extract trailing "white
space", so the '\n' character which terminates the first line is
left in the stream.  The getline then reads up to that
character.  Which isn't what you want.

Personally, I'd do this a bit different:

    struct Entry
    {
        std::string         garbage ;
        std::string         player ;
        std::string         position ;
    } ;
    std::vector< Entry >array ;

    std::string         line1 ;
    std::string         line2 ;
    while ( std::getline( inFile, line1 )
            && std::getline( inFile, line2 ) ) {
        Entry               e ;
        std::istringstream  s( line1 ) ;
        s >> e.garbage >> e.player ;
        e.position = line2 ;
        array.push_back( e ) ;
    }

But with a lot more error handling, in case the file was
corrupt.  (I'd also probably allow spaces in the last name.  In
anything less trivial, of course, Entry would be a class with a
constructor and a >> operator.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top