Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fstream problem

Reply
Thread Tools

fstream problem

 
 
beepa
Guest
Posts: n/a
 
      11-12-2007
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;
}
 
Reply With Quote
 
 
 
 
Philip
Guest
Posts: n/a
 
      11-12-2007
I think inFile.getline() will be advisable to get position names.

On Nov 12, 4:04 pm, "beepa" <sil...@shhh.com> 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;}
>
>



 
Reply With Quote
 
 
 
 
wira1guys wira1guys is offline
Junior Member
Join Date: Sep 2007
Posts: 6
 
      11-12-2007
Quote:
Originally Posted by beepa
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-13-2007
On Nov 12, 9:04 am, "beepa" <sil...@shhh.com> 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>


If you're going to have arrays, you'll need
#include <vector>
as well.

> using namespace std;


> int main()
> {
> char sentry;
> string garbage[9];
> string player[9];
> string position[9];
> fstream inFile;
> inFile.open("team1.txt");


You should check that the open succeeded.

> for(int test = 0; test < 9; test++)
> {
> inFile >> garbage[test];
> inFile >> player[test];
> getline(inFile,position[test]);


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

 
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
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM
problem with fstream Frédéric Manzanares C++ 3 11-29-2003 10:13 PM
file creation problem in Windows using fstream Brandon McCombs C++ 8 11-24-2003 08:46 AM
Fstream problem jbruno4000 C++ 5 11-03-2003 04:30 AM
fstream problem las C++ 1 07-16-2003 12:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57