Trouble reading and displaying from txt file.

Hello,
I'm having difficulties with reading from a text file and displaying it in the console. I've looked at multiple examples but just do not understand what I'm supposed to do/ what I'm doing wrong. All help will be greatly appreciated. Also if anyone can offer advice on how to only take in specific data from a txt file that has a large amount of data I would be very grateful. The following is my main code if you need any other data, or parameters for what I'm doing please let me know.

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
#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include "prototype.h"
#include "yearlyInformationFunctions.c"
using namespace std;

//------MAIN PROGRAM-------
int main()
{
    // Variable Declaration
    int menuoption = 0;
    int yrMenuOption = 0;
    int hisMenuOption = 0;
    int validnumber = 0;
    int yearinput = 0;



    // declaring variable to read data
   ifstream in_stream;
   ofstream out_stream;
    // opening file
   in_stream.open ("RocTempDataV1.txt");
    //error checking if file exists
    if(  in_stream.fail( ) )
    {   //file opened
        cout << "Unable to open file - does not exit in current directory" << endl;
        exit(0);
    }

    out_stream.open("RocTempDataV1.txt");


    cout << in_stream ;
   // Function call for main menu
   mainMenu(menuoption);


    // switchstatement for main menu
    switch (menuoption)
    {


    case 1 :
    cout << "Enter valid year between 1940 and 2011 : " ;
    cin >> yearinput;
    // goes to yearly information menu switch selection
    validYear(yearinput);
    yearlyInformationMenu(yrMenuOption);

    break;

    case 2 :
   // goes to histroical menu switch selection
    historicalInformationMenu(hisMenuOption);
    break;

    case 3 :
    // exits the program.
    void exit(int exitcode);

    }



// switch statement for yearly information menu
    switch (yrMenuOption)
    {

    case 1:

    break;

    case 2:

    break;


    case 3:

    break;

    case 4:

    break;

    case 5:
    //  return to main menu switch statement.
    mainMenu(menuoption);
    break;

    default:
    cout << "Invalid integer - Please try again"<<endl;
    cout << "Please re-enter integer : ";
    cin >> yrMenuOption;

    }


    // Switch for historical information menu

    switch (hisMenuOption)

    {

        case 1 :

        break;


        case 2 :

        break;


        case 3 :

        break;

        case 4 :
        mainMenu(menuoption);

        break;


        default:
        cout << "Invalid integer - Please try again"<<endl;
        cout << "Please re-enter integer : ";
        cin >> hisMenuOption;


    }


}
Last edited on
Mmm, I'm not sure how exactly I can help you since your code is incomplete. But I think I will try to explain you how to work with files. I encourage you to check this tutorial:

http://www.cplusplus.com/doc/tutorial/files/

OK, so at the beginning you were doing fine. You declared your file streams correctly:
1
2
3
// declaring variable to read data
   ifstream in_stream;
   ofstream out_stream;


and you also openned the files correctly:
1
2
3
4
5
6
7
8
 // opening file
   in_stream.open ("RocTempDataV1.txt");
    //error checking if file exists
    if(  in_stream.fail( ) )
    {   //file opened
        cout << "Unable to open file - does not exit in current directory" << endl;
        exit(0);
    }


So what you did here is that you declared in_stream to be an input stream (a stream can be seen as a set of characters that travels to OR from a file), which means that in_stream should ONLY be used for input operations. The same rule applies for out_stream, which you declared to be an output stream, so you can ONLY use it for output operations. And thus, following statement is wrong:

cout << in_stream ;

cout is an output stream and in_stream is an input stream, so they are essentially of incompatible types.

Once, you declare your streams, you need to "open" them. You can somewhat say that this will "link" your stream to a file. Every stream has a source and a destiny. So openning an input stream will link it to its source and openning an output stream will link it to its destiny. I need to stress out that you opened both streams to the same file:
1
2
3
4
 // opening file
   in_stream.open ("RocTempDataV1.txt");
...
out_stream.open("RocTempDataV1.txt");

It is more common to have an input file from which you are going to read data and an output file, where you are going to store data after processing it.

Once everything is set up, you can use out_stream just as you use cout for output. It works exactly the same way, the only difference is that instead of putting the data on the screen, it will put it in your file. And of course, using in_stream is exactly the same as using cin, except that you would read the data from the file instead of the keyboard (and thus, there will be no 'pause' when inputing data).

So for example let's say you wish to read the first line of your file:
1
2
3
4
5
6
7
// declaring variables
string text;
istream in_file;
// opening the file
in_file.open("file.txt");
// reading the file
getline(in_file, text);


or if you wish to read numbers
1
2
3
4
5
// declaring variables
int number;
istream in_file;
in_file.open("file.txt");
in_file >> number;


And you can store data this way:
1
2
3
4
ostream out_file;
out_file.open("output.txt");
out_file << text << endl;
out_file << number;


You can basically store anything in an output file:
1
2
out_file << "Hello world!" << endl;
out_file << "I'm " << number << " years-old.";


I hope, this was helpful. This was just a very brief explanation, so I recommend you still check the tutorial I passed you.

EDIT: Oh yes, one more thing, remember to ALWAYS close the files once you finished using them (e. g. at the end of the program):
1
2
in_file.close()
out_file.close()
Last edited on
Mostly used your code and added a few lines to make it work.

Reading oldcrows post will help understand everything.

My advise, start with a basic program to do one thing, such as open and read a file, once that is complete and you know it's working, move on to the next step.
Your next step might be to add your user input statements.
or, open your output file and make sure you can write to it.

Small steps will get you there safely with less chance to fall. If you want to leap and bound, expect to tumble.

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
#include <iostream>
#include <fstream>

using namespace std;

//------MAIN PROGRAM-------
int main()
{
    // declaring variable to read data
   ifstream in_stream;
   ofstream out_stream;
    // opening file
   in_stream.open ("out.txt");
    //error checking if file exists
string line;


    if(  in_stream.fail( ) )
    {   //file opened
       cout << "Unable to open file - does not exit in current directory" << endl;
       exit(0);
    }

    while(!in_stream.eof())
    {
       getline(in_stream,line);
       cout << line << endl;
    }
}
You guys are the best! If we're ever to meet I'm buying both of you a round (your choice of beverage ) on me =)
Topic archived. No new replies allowed.