File I/O textfilesHow to read and write from a text file
Page 1 of 1
12 Replies - 1222 Views - Last Post: 05 January 2010 - 05:48 PM
#1
File I/O textfiles
Posted 05 January 2010 - 01:55 PM
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
Replies To: File I/O textfiles
#2
Re: File I/O textfiles
Posted 05 January 2010 - 02:45 PM
#3
Re: File I/O textfiles
Posted 05 January 2010 - 03:08 PM
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
#4
Re: File I/O textfiles
Posted 05 January 2010 - 03:42 PM
#5
Re: File I/O textfiles
Posted 05 January 2010 - 03:48 PM
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;
}
#6
Re: File I/O textfiles
Posted 05 January 2010 - 03:53 PM
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
#7
Re: File I/O textfiles
Posted 05 January 2010 - 03:56 PM
Ok ImaSexy, I will look into what a token is.
This post has been edited by 4D1: 05 January 2010 - 04:01 PM
#8
Re: File I/O textfiles
Posted 05 January 2010 - 04:19 PM
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
#9
Re: File I/O textfiles
Posted 05 January 2010 - 04:31 PM
ImaSexy, on 5 Jan, 2010 - 03:19 PM, said:
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?
#10
Re: File I/O textfiles
Posted 05 January 2010 - 05:14 PM
#11
Re: File I/O textfiles
Posted 05 January 2010 - 05:21 PM
#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
#12
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
#13
Re: File I/O textfiles
Posted 05 January 2010 - 05:48 PM
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
|
|

New Topic/Question
Reply



MultiQuote






|