Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-28-2009, 01:59 PM   #1
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:

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.

struct Score {
string name;
int score;

vector<Score> best_scores;
Can you help me?

Btw, I did this:

#include <iostream>
#include <fstream>
#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; (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;
    	if (isalpha(c))
    	{ += c;
    stringstream s(b);
    s >> a.score;

  cout << a.score<< endl;
  cout << << endl;
  return 0;


Last edited by inpanzinator; 03-28-2009 at 02:05 PM.
Old 03-28-2009, 03:05 PM   #2
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, 04:21 PM   #3
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

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


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
List Hardware Configuration and save as txt file hmendoza Linux - Newbie 3 01-27-2009 04:12 PM
plz need help on C++ about read int from a TXT to array. cyborgt Programming 5 02-20-2008 03:11 PM
Read a txt file in C xeon123 Programming 3 10-21-2007 12:14 PM
why openoffice (spreadsheet) has no save as simple TXT ? Xeratul Linux - Software 5 05-29-2007 06: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 06:11 PM

All times are GMT -5. The time now is 09:29 PM.

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