LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-28-2009, 12:59 PM   #1
inpanzinator
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
Read and save from a .txt - C++


Hey. I need to read from a file called ranking.txt the name of the players and the respective score. The file is something like this:

Code:
24145 John Burt
12442 Kate Blair
1244 Jacob
Then, I need to save the data into a vector, best_scores, defined by a data struct.

Code:
struct Score {
string name;
int score;
};

vector<Score> best_scores;
Can you help me?


Btw, I did this:

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

using namespace std;

struct Score{
	string name;
	int score;
}a, b, c, d, e, f, g, h, i, j;

vector<Score> best_scores;


int main () {
  char c, str[256] = "ranking.txt";
  ifstream is;

  is.open (str);        // open file
  string b;
  while (is.good())     // loop while extraction from file is possible
  {
    c = is.get(); // get character from file
    if (isdigit(c))
    {
    	b += c;
    }
    else
    	if (isalpha(c))
    	{
    		a.name += c;
    	}
    stringstream s(b);
    s >> a.score;

  }
  cout << a.score<< endl;
  cout << a.name << endl;
  is.close();
  return 0;


}

Last edited by inpanzinator; 03-28-2009 at 01:05 PM.
 
Old 03-28-2009, 02:05 PM   #2
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 57
Why are you using char arrays instead of std::string?
 
Old 03-28-2009, 03:21 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by inpanzinator View Post
Can you help me?
That looks suspiciously like homework.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List Hardware Configuration and save as txt file hmendoza Linux - Newbie 3 01-27-2009 03:12 PM
plz need help on C++ about read int from a TXT to array. cyborgt Programming 5 02-20-2008 02:11 PM
Read a txt file in C xeon123 Programming 3 10-21-2007 11:14 AM
why openoffice (spreadsheet) has no save as simple TXT ? Xeratul Linux - Software 5 05-29-2007 05:34 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM


All times are GMT -5. The time now is 10:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration