How to read into file and display file

Im trying to write a function for another program that reads the name, score and date to a txt file, then display it as a sort of "scoreboard". How would I do this in a function to be called to the main?
Are you saying you need to create a function that reads from a file, and displays that information on the console? Have you already created an input file stream? Are you having trouble getting the data? I don't know which part you are stuck on, or if you possibly have no idea where to begin.
Very confused with syntax and how sequential files work, but basically I need to create a function for another program that reads the name, score, and date into a file, then displays those 3 things line by line
So the name, score, and date will be read in from the console, stored in a file (line by line) and then display the results of that file?
Yes but I cannot figure out how to code this
closed account (SECMoG1T)
I guess you mean reading from a txt file not to. But what does your "phrase" scoreboard mean? If you mean just a display pattern then you task is easy, if your data is arranged inform of the name score and date per line then you can use the getline function to read each line into a string a display the string., loop unitil the whole file is read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void display ()
{
   ifstream in ("scoreboard.txt");
   string current="";//always a good Idea to intialize your variables
   
   if (!in){cout <<" text file not found"; exit (1);}
   while (in)
    {
       getline (in, current, '\n');
       cout <<current <<endl;
     }
   
    in.close ();
 }


That was just to give you an idea, you can start there and feel free to modify that to suite your need;
Some good reference too will be handy to get you started
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Here is something quick I came up with. Feel free to use the link Andy gave you as well. I also found this:
http://www.cplusplus.com/forum/articles/6046/
It looks like you have just broke the tip of the iceberg. Have fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void collectData(ofstream& fout)
{
	string name = "", date = "", score = "";
	
	do
	{
		cout << "Enter your name" << endl;
		cin >> name;
		cout << "Enter the score" << endl;
		cin >> score;
		cout << "Enter the date" << endl;
		cin >> date;
		fout << name << " " << score << " " << date << endl;
	} while (name != "quit" || score != "quit" || date != "quit");
	  // type "quit" for name, score, or date to stop
}

void displayData(ifstream& fin)
{
	string line = "";
	while (!fin.eof())
	{
		getline(fin, line, '\n');
		cout << line << endl;
	}

}
void main()
{
	{
		ofstream fout("output.txt");
		collectData(fout);
	}
	{
		ifstream fin("output.txt");
		displayData(fin);
	}
	system("pause");
}
Last edited on
Topic archived. No new replies allowed.