fstream problem

Discussion in 'C++' started by beepa, Nov 12, 2007.

  1. beepa

    beepa Guest

    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;
    }
    
    beepa, Nov 12, 2007
    #1
    1. Advertising

  2. beepa

    Philip Guest

    I think inFile.getline() will be advisable to get position names.

    On Nov 12, 4:04 pm, "beepa" <> wrote:
    > 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;}
    >
    > 
    Philip, Nov 12, 2007
    #2
    1. Advertising

  3. beepa

    wira1guys

    Joined:
    Sep 26, 2007
    Messages:
    6
    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.
    wira1guys, Nov 12, 2007
    #3
  4. beepa

    James Kanze Guest

    On Nov 12, 9:04 am, "beepa" <> wrote:
    > 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>[/color]
    
    If you're going to have arrays, you'll need
        #include <vector>
    as well.
    [color=blue]
    > using namespace std;[/color]
    [color=blue]
    > int main()
    > {
    >  char sentry;
    >  string garbage[9];
    >  string player[9];
    >  string position[9];
    >  fstream inFile;
    >  inFile.open("team1.txt");[/color]
    
    You should check that the open succeeded.
    [color=blue]
    >  for(int test = 0; test < 9; test++)
    >  {
    >   inFile >> garbage[test];
    >   inFile >> player[test];
    >   getline(inFile,position[test]);[/color]
    
    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.)
    
    --
    James Kanze (GABI Software)             email:
    Conseils en informatique orientée objet/
                       Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. las

    fstream problem

    las, Jul 16, 2003, in forum: C++
    Replies:
    1
    Views:
    433
    Simon Saunders
    Jul 16, 2003
  2. jbruno4000

    Fstream problem

    jbruno4000, Nov 2, 2003, in forum: C++
    Replies:
    5
    Views:
    459
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Nov 3, 2003
  3. Brandon McCombs
    Replies:
    8
    Views:
    482
    Dave O'Hearn
    Nov 24, 2003
  4. Frédéric Manzanares

    problem with fstream

    Frédéric Manzanares, Nov 28, 2003, in forum: C++
    Replies:
    3
    Views:
    512
  5. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
Loading...

Share This Page