How to get entire contents of a text file

I forget how to get the entire contents of a text file. I want to be able to open the file and get every line in it and display it in the console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, char* argv[])
{
	ifstream infile("C:\\words.txt");
	string str;

	while ( !infile.eof() )
	{
		getline(infile, str);
		cout << str << endl;
	}

	infile.close();
}


EDIT: Apologies for the tabbed indentation - I'm on my work laptop and it's Visual Studio's fault. I'll remember to turn it off one day.
Last edited on
Ok thanks, now i have a problem and i cant seem to figure out why,

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long int money;
    int prisoners;
    string playerName;
    string prisonName;
    ofstream names; // Saves the prisoners list
    ofstream file; // Saves the player info
    string fileStr;
};

int main()
{
    int choice;
    srand(time(0x0));

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        v.file.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        v.file << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        v.file << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        v.money = 50000;
        v.prisoners = 0;

        v.file << v.money << endl;
        v.file << v.prisoners << endl;

        v.file.close();

        v.Game();
    }
    else if(choice == 2)
    {
        ifstream file;

        file.open("prison.txt");

        getline(file, v.playerName);
        getline(file, v.prisonName);
        file >> v.money;
        file >> v.prisoners;

        while(!v.names.eof)
        {
            getline(v.names, v.fileStr);
        }

        file.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    int random;
    int inMnum;
    bool endLoop = true;
    names.open("prisonersList.txt");

    cout << "Main Menu\n" << endl;

    cout << "What do you want to do?\n" << endl;

    cout << "1) Add prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;

    while(endLoop != false)
    {
        cin >> choice;

        switch(choice)
        {
            case 1:
                {
                    cout << "INMATE LIST" << endl;

                    vector<string> nameVect;
                    vector<string> crimeVect;

                    cout << "Enter how many inmates you would like to take in." << endl;
                    cout << "Your current prisoner count is: " << prisoners << endl;
                    cout << "You may not exceed 25 prisoners at this time" << endl;
                    cin >> inMnum;

                    if((inMnum > 0) && (inMnum < 26))
                    {
                        for(int i = 0; i < inMnum; i++)
                        {
                            nameVect.push_back(randNames());
                            crimeVect.push_back(randCrimes());
                        }

                        cout << "Name " << "Crime\n" << endl;

                        for(int i = 0; i < inMnum; i++)
                        {
                            cout << nameVect[i] << " ";
                            cout << crimeVect[i] << endl;
                            names << nameVect[i] << " ";
                            names << crimeVect[i] << endl;
                        }
                    }
                    else
                    {
                        cout << "Error" << endl;
                        endLoop = false;
                    }
                }
                break;
            case 2:
                cout << "Executed Prisoners\n" << endl;
                break;
            case 3:
                cout << "Prison Statistics\n" << endl;
                cout << "Warden Name: " << playerName << endl;
                cout << "Prison Name: " << prisonName << endl;
                cout << "Current Money: " << money << endl;
                cout << "Prisoners: " << prisoners << endl;
                break;
        }
    }

}

string Vars::randNames()
{
    string names[17] =
    {
        "Jerry",
        "Mike",
        "Phill",
        "Robert",
        "Allen",
        "Alex",
        "Tim",
        "Steven",
        "Ronald",
        "David",
        "Harold",
        "Thomas",
        "Andrew",
        "Carlito",
        "Frank",
        "Dale",
        "John"
    };

    return names[rand() % 17];
}

string Vars::randCrimes()
{
    string crimes[11] =
    {
        " - Rape",
        " - Child Abuse",
        " - Murder",
        " - Assault",
        " - Breaking and Entering",
        " - Domestic Violence",
        " - Unlawful Posession of a Firearm",
        " - Destruction of private property",
        " - Kidnapping",
        " - Armed Robbery",
        " - Petty Theft"
    };

    return crimes[rand() % 11];
}



In function 'int main()':|
|78|error: could not convert 'v.Vars::names.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>]' to 'bool'|
|78|error: in argument to unary !|
|80|error: no matching function for call to 'getline(std::ofstream&, std::string&)'|
||In member function 'void Vars::Game()':|
|92|warning: unused variable 'random'|
||=== Build finished: 3 errors, 1 warnings ===|
Last edited on
First error is because you've missed the parenthesis from the eof function.
1
2
3
4
5
// Change...
while(!v.names.eof)

// To...
while ( !v.names.eof() )


The second error should be resolved when you fix the first error.

The third error is because you're using getline wrong. It's parameters are an istream and a string and you're passing it an ofstream and a string. Furthermore, the stream your operating on hasn't been opened.

You need to ensure you're opening and closing the right streams for what you want here.
ok it all works now awesome, thanks!!
Topic archived. No new replies allowed.