File I/O textfiles

How to read and write from a text file

Page 1 of 1

12 Replies - 1547 Views - Last Post: 05 January 2010 - 05:48 PM Rate Topic: -----

#1 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

File I/O textfiles

Posted 05 January 2010 - 01:55 PM

Hi guys,

My question isnt simply how to read and write from a text file, I need to write scores to a text file and then read them in at the users request in some sort of order, I know how to do basic read and write to text files and I know how to use different sorting algorithms, my question is I want to output a users name to a file with their score, if I put them on the same line how would I then read the line in and sort them by score would I need to split the line into string and numeric? or if I put the user's name on a line and the score on another how do I then read in every second line?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O textfiles

#2 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: File I/O textfiles

Posted 05 January 2010 - 02:45 PM

Try these:
http://www.dreaminco...h...930&hl=file
http://www.dreaminco...h...699&hl=file
Was This Post Helpful? 0
  • +
  • -

#3 Eventide  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 20-June 09

Re: File I/O textfiles

Posted 05 January 2010 - 03:08 PM

Create a database to store it, i advice you to use SQLite, because it's file based and recommended to small apps.
but you can also make a different file to each user, or maybe do a XML file, then you can organize it best

This post has been edited by Eventide: 05 January 2010 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: File I/O textfiles

Posted 05 January 2010 - 03:42 PM

Thanks Eventide that would have been my take on it, but my assignment requires me to use a text file, I can think of ways to do it just nothing clean and simple.
Was This Post Helpful? 0
  • +
  • -

#5 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: File I/O textfiles

Posted 05 January 2010 - 03:48 PM

A database is a good idea. If you have to use a text file to store the list, you can read it in as follows. Suppose you have stored the data in a file called "test.txt", which has the following contents:

4D1	   43
anonymouscoder 56
Eventide   11



You can read it in with the following:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
	ifstream infile;
	infile.open("test.txt", ifstream::in);

	string user;
	int score;
	while (infile >> user >> score) {
		cout << user << " " << score << endl;
		// do something with the user name and score
	}

	infile.close();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: File I/O textfiles

Posted 05 January 2010 - 03:53 PM

read in in your text file line by line,
parse the line into tokens at every delimeter,
ignore the first word in each line (because thats the name),
sum up all numbers left in the line
Was This Post Helpful? 0
  • +
  • -

#7 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: File I/O textfiles

Posted 05 January 2010 - 03:56 PM

Thanks Paul, thats more like the kind of thing, will give it a shot, one thing though any ideas how I would go about keeping the arrays in order, what I mean is the scores would prefferably be outputted to the screen highest to lowest so how would I make sure the names are ordered the same.

Ok ImaSexy, I will look into what a token is.

This post has been edited by 4D1: 05 January 2010 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: File I/O textfiles

Posted 05 January 2010 - 04:19 PM

#include <algorithm> has a really cool sort function

create a stucture to hold your user name and score so you can group those together. then create an array of that structure according to how many students there are. You can then use the vector sort function to sort the structure array by there grade.

example

struct studentInfo
{
	string name;
	int grade;
};



then create your vector

vector<studentInfo>students(/*number of students*/);



create your comparison function and call the sort function

sort(students.begin(),students.end()); //you can make a comparison functions to control your sorting --3rd parameter



this is just the easiest way I though of when reading your assignment, there are other ways of course

This post has been edited by ImaSexy: 05 January 2010 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#9 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: File I/O textfiles

Posted 05 January 2010 - 04:31 PM

View PostImaSexy, on 5 Jan, 2010 - 03:19 PM, said:

this is just the easiest way I though of when reading your assignment, there are other ways of course


Yes, I think a vector, or other STL container, with the provided sort function would be best. Does the assignment allow you to do that? Or do you have to implement sorting yourself?
Was This Post Helpful? 0
  • +
  • -

#10 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: File I/O textfiles

Posted 05 January 2010 - 05:14 PM

ImaSexy, how do I get data into the struct and into the vector? I dont know how to do it using a struct, I have never used linked lists etc before
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: File I/O textfiles

Posted 05 January 2010 - 05:21 PM

well if your just sorting by the grade i would use the comparison funciton along with the sort funciton like so

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct studentInfo
{
	string name;
	int grade;
};

bool compareGrades(studentInfo const &arg1, studentInfo const  &arg2)
{
	if(arg1.grade!=arg2.grade)
		return arg1.grade<arg2.grade;
}

int main()
{
	vector<studentInfo>students(0); //lets just say its already populated;
	sort(students.begin(),students.end(),compareGrades);
	
	
	return 0;
}




something like that


@4D1

you can popultate the same way paul said. The syntax is a littly funky for a vector of a struct
int getNumberOfStudents(ifstream &file)
{
	string line = "";
	int size = 0;
	while(getline(file,line))
		size++;
	//reset file pointer
	file.clear();
	file.seekg(0,ios::beg);
	return size;
}

int main()
{
	ifstream file("SOMEFILE.txt");
				//get the number of lines -- also number of students
	int numStudents = getNumberOfStudents(file);
	
	vector<studentInfo>students(numStudents);
	
	//what paul said
	string name;
	int grade;
	while(file>>name>>grade)
	{
		students[i].name = name;
		students[i].grade = grade;
		i++;
	}
	//then sort here

	
	
	return 0;
}



This post has been edited by ImaSexy: 05 January 2010 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: File I/O textfiles

Posted 05 January 2010 - 05:35 PM

	ifstream input;

	input.open("scores.txt");

	if(input.is_open())
	{
		int i=0;

		while(!input.eof())
		{
			getline(input, tempuser);

			name.push_back(tempuser);
		   // score.push_back(tempscore);

			cout << name[i] << " ";
		   // cout << score[i];
			i++;
		}
	}
	else
	{
		cout << "\n\n\t\t\tSorry there was an error getting scores";
	}

	input.close();


This is what I have so far, it will take in the line and assign it to a vector, it would be great to turn this into a vector of a struct to make sorting easier, however I dont know how to access a struct when it is in a class and I dont know how to populate the vector struct with name and score???

I dont understand your function

bool compareGrades(studentInfo const &arg1, studentInfo const  &arg2)//Why memory addresses as arguments and why constants?
{
	if(arg1.grade!=arg2.grade)
		return arg1.grade<arg2.grade; //dont know what the < is doing?
}

int main()
{
	vector<studentInfo>students(0); //lets just say its already populated;
	sort(students.begin(),students.end(),compareGrades);
	
	
	return 0;
}


Ahh ok you are passing the memory address of the struct as argument and accessing the members via a pointer

This post has been edited by 4D1: 05 January 2010 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: File I/O textfiles

Posted 05 January 2010 - 05:48 PM

i didnt compile this, but im pretty sure it will populate your vector.

Are you even allowed to use vectors for this assignment though?

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

//hold student info
struct studentInfo
{
	string name;
	int grade;
};

int getNumberOfStudents(ifstream &file)
{
	string line = "";
	int size = 0;
	//read the file line by line
	while(getline(file,line))
		size++; // get number of lines/students
	//reset file pointer
	// if you dont then your pointer will stay at the end of the file
	// and you wond be able to read the file any further
	file.clear();
	file.seekg(0,ios::beg);
	
	return size;
}

int main()
{   
	ifstream input;

	input.open("scores.txt");
	//get the number of lines in your file
	int size = getNumberOfStudents(input);
	//create your vector of stucts
	vector<studentInfo>students(size);

	if(input.is_open())
	{
		int i=0;
		string user;
		int score;

		//read in your user and then his/her score
		//populate your vector (funky syntax -- i know)
		// works for a text format like:
		//Name	Grade
		while(input>>user>>score)
		{
			students[i].name = user;
			students[i].grade = score;
			i++;
		}
	}
	else
	{
		cout << "\n\n\t\t\tSorry there was an error getting scores";
	}
				// take a look inside your vector so you can see whats going on

	for(int i=0; i<size; i++)
	{
		cout<<students[i].name<<endl
			<<students[i].grade<<endl;
	}
			  

	//then call your sort function
	
	
	input.close();
	
	return 0;
}

	
	
	


This post has been edited by ImaSexy: 05 January 2010 - 05:50 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1