Ifstreams, Strings, and Arrays

Bringing info in from files using strings and separating into arrays.

Page 1 of 1

1 Replies - 1040 Views - Last Post: 05 October 2009 - 02:24 AM Rate Topic: -----

#1 newatthis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 09

Ifstreams, Strings, and Arrays

Posted 04 October 2009 - 11:45 PM

For an assignment, we are supposed to read a line of data from a file (txt) -

Ex. Badges 0 3 5 (for the team name, wins, losses, and ties, respectively)

- into a string line, and from that string line, convert the string into four different arrays (using structures).

For example, my beginning is

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;

const int MAX_TEAMS = 12;
const int SIZE = 81;

struct team
	   char name[SIZE];
	   int wins[SIZE];
	   int losses[SIZE];
	   int ties[SIZE];

int main()

cout << "******************************************" << endl;
cout << "Hello! This program will allow you to put \nin a file name, open said";
cout << " file, calculate\nthe teams' statistics, and then read the\nteams' data"; 
cout << " from that file onto the screen. \nThe data will include a team's name,";
cout << " wins, \nlosses, and ties." <<endl;
cout << "******************************************" << endl;

I know how to read in things from files, generally, but I don't know how I would read in an entire string of data versus just one piece at a time. The code I'm familiar with stops at every null space instead of every newline. For example, I've written

ifstream inFile;

char name[SIZE];
char fileName[SIZE];
cout << "Enter the name of the file: ";
cin >> fileName;

inFile >> name;
cout << name << endl;
inFile >> name;
cout << "\t" << name << endl;


while using the practice txt document with it saying

hello user

Could someone help push me in the right direction for using strings with an explanation or pseudocode? I've yet to find anything in my textbook that describes the situation I'm currently describing.

Is This A Good Question/Topic? 0
  • +

Replies To: Ifstreams, Strings, and Arrays

#2 olibenu   User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: Ifstreams, Strings, and Arrays

Posted 05 October 2009 - 02:24 AM

this will read a line
getline(infile, somestring);

this will read in word by word (seperated by spaces)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1