Reading whats in a file

So my program currently will read the first number in the text file, but if I accept, or replace the number. It will start randomly putting 0's in and it will shift around my file ex.
If my first number is 3 and I want to replace it with 2 I hit 'r'.
It then will replace that value, and it is suppose to get the next number on the next line, and ask to change that, but it gets all jumbled. Sorry for the long explanation but I wanted to be thorough.

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

using namespace std;

class myClass{
    public:
    myClass (string fname)  //constructor
    {
        filename = fname;
        selection = 0;
        input = 0;
        number = 0.0;
        anothernumber = 0.0;
        replacement = 0.0;
        newfilename = "";
        //choice = '';
        answer = "";
    }

    string returnNewfile()
    {
        return newfilename;
    }

    string returnAnswer()
    {
        return answer;
    }

    int returnSelection()
    {
        return selection;
    }

    int returnInput()
    {
        return input;
    }

    double returnOtherNumber()
    {
        return anothernumber;
    }

    double returnNumber()
    {
        return number;
    }

    double returnReplace()
    {
        return replacement;
    }

    char returnChoice()         //choice
    {
        return choice;
    }

    void Select()
    {
    cout << "Please enter 0 for read or 1 for write mode" << endl;
    cin >> selection;

        if (selection == 0)
        {
            ifstream filename ("example.txt");
            if (filename.is_open())
            {
                while(getline(filename, line))                 //Goes through the file and gets each line individually
                {                                             //and prints out whats in it line by line.
                    cout << '\n' <<"This is what is in the file: " << line << endl;
                }
            filename.close();
            }
        }
        else if (selection == 1)
        {
            fstream filename("example.txt");
            if (filename.is_open())
                {
                    cout << "How many numbers do you want to enter? " << endl;
                    cin >> input;

                for (int i = 1; i <= input; i++)              //This will check how many numbers you want to enter
                {                                             //then will keep asking for the numbers until it reaches what you wanted.
                    getline(filename, line);
                    cout << '\n' << "This is what is in the file: " << line << endl;
                    cout << '\n' << "Hit a to accept value, r to replace, d to delete it, or type x to accept rest of the values. " << endl;
                    cin >> choice;

                    if (choice == 'a' || choice == 'A')
                    {
                        filename << line << endl;
                    }

                    if (choice == 'r' || choice == 'R')
                    {
                        cout << "What number do you want to replace it with? " << endl;
                        cin >> number;
                        filename << number << " " << line << endl;
                        cout << "The number has been replaced."  << endl;
                    }

                    if (choice == 'd' || choice == 'D')
                    {
                        number--;
                        cout << "Number deleted." << endl;
                    }

                    if (choice == 'x' || choice == 'X')
                    {
                        //exit();
                    }
                        cout <<'\n' << "Would you like to enter a new number?" << endl;
                        cin >> answer;

                    if (answer == "yes" || answer == "YES" || answer == "Yes")
                    {
                        cout << '\n' << "Please enter the number." << endl;
                        cin >> anothernumber;
                        filename << line << "";
                        filename << anothernumber << endl;
                    }
                }// end of for loop
            cout << "Would you like to save the changes of the file to a new one?" << endl;
            cin >> newfilename;

                if(newfilename == "yes" || newfilename == "Yes" || newfilename == "YES")
                {
                    string thisfilenow;
                    cout << "Please enter new file name." << endl;
                    cin >> thisfilenow;   //this will save the new data into another file leaving the old file intact.
                    filename << thisfilenow;
                }
            }// end of filename.open if statement
             filename.close();
        }// end of else if selectio == 1
    }//End of void select

    private:
    string filename;
    string line;
    string answer;
    string newfilename;
    int selection;
    double myarray[10][10];
    int input;
    double number, replacement,anothernumber;
    char choice;
};
int main()
{
    string fname = "example.txt";

    myClass co(fname);
    co.Select();
}
In your class you have a string member called filename which you have not used

Also it would help if we saw what your input file looked like
Sorry about that and thanks for the help.

This is what is in the text file before I run the code.
160
12


When I run it and try to replace a value with another like 25 it does this:



160
125 160


Last edited on
When you did getline(filename, line); on line 89, the internal file pointer of the file you are working with moves to the next line. So when you try to replace 160 with 125, you will actually be replacing the value on the next line. You need to make use of a peekline (if there is such a thing) kind of method so that you can instead "peek" at the contents of the next line and choose to replace it if need be

Yup, found one for you:

1
2
3
4
5
6
7
std::istream &peekline(std::istream &iss, std::string &s)
{
	std::streampos sp = iss.tellg();
	std::getline(iss, s);
	iss.seekg(sp);
	return iss;
}
Last edited on
Thank you Smac89, I will try that when I get home and post if I have further issues. Does anything else look funky? It's better when another set of eyes looks at your code.
Topic archived. No new replies allowed.