while loop won't print file content in the console

Hello, as I'm sure you'll be able to tell by how strangely organized my program looks I'm a new C++ learner.
Recently I've been messing around with fstream, and after experimenting a bit with it to see what does what I tried to write a program that lets the user input something, adds it as a line in a file, and then prints out all the content in the file:

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
  #include <iostream>
  #include <fstream>
  #include <string>
  using namespace std;

int main()
{
    string const filePath("fstream landfill/Project8 (test fstream)/test.txt");
    ofstream testOutFlux(filePath.c_str()), testOutFluxMod(filePath.c_str(), 
    ios::app); 
    ifstream testInFlux(filePath.c_str()); 

    if(testOutFlux && testInFlux) //check that both streams have been 
    //successfully set up
    {
        string testLine, testInput;
        cout << "Succesfully opened " << filePath << " , what to add?" << 
        endl;

        getline(cin, testInput); 
        testOutFlux << testInput << endl; //deletes and replaces the file's data 
        //with testInput's content

        getline(testInFlux, testLine); //modifies testLine so as to be a copy of 
        //the file's data

        cout << endl << "successfully modified " << filePath << endl;
        cout << "it contains: " << endl << testLine << endl << endl;

        char Continue;
        do
        {
            cout << "add something?" << endl;
            cin >> Continue;

            if(Continue == 'y')
            {
                cout << endl << "what to add?" << endl;

                cin.ignore(); //ignores preceding uses of cin
                getline(cin, testInput); //modifies testInput to contain a line
                testOutFluxMod << testInput; //adds testInput at the end of  
                //the file rather than replacing it

                testInFlux.ignore(); //ignores preceesing uses of testInFlux
                getline(testInFlux, testLine); //modifies testLine to contain 
                //1 line of testInFlux's file

                cout << endl << "successfully modified " << filePath << endl;
                cout << "it now contains: ";

                testInFlux.ignore(); //ignores preceding uses of testInFlux
                
                string line; 
                while(getline(testInFlux, line)) 
                {
                    testOutFlux << line << endl;
                }
                cout << endl;
            }

            else
            {
                cout << endl << "closing program" << endl;
            }

        }while(Continue == 'y');


    }

    else
    {
        cout << "ERROR: could not open " << filePath << endl;
    }

    return 0;
}


By all accounts, according to my apparently non-sufficient research and very limited understanding of C++, the while loop at line 55 should get me all of the file's lines, but it won't even print out the first one.
Does anyone who's more experienced with fstream and C++ than me know where the issue might lie? Thanks in advance.
Last edited on
1
2
3
    ofstream testOutFlux(filePath.c_str()), testOutFluxMod(filePath.c_str(), 
    ios::app); 
    ifstream testInFlux(filePath.c_str()); 

You can't have two independent streams (one for writing and another for reading) on the same file.

But you can have a single file in update mode, like this.
 
fstream test(filePath.c_str(), ios::in | ios::out);

But you need to remember to flush the stream between writing and reading.
Get it wrong, and it still won't work.
https://stackoverflow.com/questions/17536570/reading-and-writing-to-the-same-file-using-the-same-fstream


TBH, as a newbie, you need to get comfortable with plain read from one file write to another.
So, I've been testing out what you suggested and tried converting both my streams into a single stream.

So far so good, I'm getting the same issue as before but that's because I'm not flushing the stream, and now at least I know how to use fstreams and that using an ofstream and an ifstream on the same file isn't a great idea.
I'm using seekp() temporarily at L22 to serve as a replacement for fflush (so as to at least get the first cout to print out testLine correctly), which I need as I don't seem to understand how to use it correctly.
Here's what I tried to write at L29, 43, 47, 53, and 57:

 
fflush(filePath);


And for each of these I get the error:

|error: cannot convert 'const string {aka const std::__cxx11::basic_string<char>}' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fflush(FILE*)'|


Needless to say, with how inexperienced I am with C++ and programming as a whole, I have no idea what any of this means.
The only clue I have is that I know arrays can't be copied and strings are arrays, so I'm guessing the function converts by copying and that's what's causing the issue? Other than that I have no idea what's wrong with this, and even if that's the issue I don't know how to fix it.
Last edited on
Because you should be using the stream member function of the same name.
https://www.cplusplus.com/reference/ostream/basic_ostream/flush/

Riiight if I'm not using the right function of course it'll get weird. Re-wrote my program with what you told me so far and some other stuff I learned on the side with the documentation you provided. It looks like this:

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
  #include <iostream>
  #include <fstream>
  #include <string>
  using namespace std;

int main()
{
    string filePathCin;
    cout << "what file to open?\n";
    getline(cin, filePathCin);
    string const filePath(filePathCin);
    fstream testStream(filePath.c_str(), ios::in | ios::out | ios::trunc);

    if(testStream)
    {
        string testLine, testInput;
        cout << "Succesfully opened " << filePath << " , what to add?" <<
        endl;

        getline(cin, testInput);
        testStream << testInput << endl;
	testStream.seekp(0);
         
        //input and add testInput's content to file
        //then flush for next use and set cursor at 0
        

        getline(testStream, testLine); 
        //modifies testLine so as to be a copy of the file's data

        cout << endl << "successfully modified " << filePath << endl;
        cout << "it contains: " << endl << testLine << endl << endl;
	//print it out

	testStream.flush();
	//and flush for next use

        char Continue;
        do
        {
            cout << "add something?" << endl;
            cin >> Continue;

            if(Continue == 'y')
            {
                cout << endl << "what to add?" << endl;

                cin.ignore();
                getline(cin, testInput); 
                testStream << testInput << endl;
		testStream.flush();
		
		//input and add testInput's content to file
        	//then flush for next use
                
                getline(testStream, testLine); 
		//modifies testLine so as to be a copy of the file's data
		

                cout << endl << "successfully modified " << filePath << endl;
                cout << "it now contains:\n";

                testStream.ignore();
		testStream.flush();

                while(getline(testStream, testLine))
                {
                    testStream << testLine << endl;
                }
                cout << endl;
		//repeatedly change testLine's content as the file's lines
		//one after another
		//and print it out, well it's supposed to?
            }

            else
            {
                cout << endl << "closing program" << endl;
            }

        }while(Continue == 'y');


    }

    else
    {
        cout << "ERROR: could not open " << filePath << endl;
    }

    return 0;
}


No more debugger nagging me with errors but I'm back to square one, the first cout works alright but the while loop still won't print anything out; I don't think I'm forgetting any line in which I should put .flush() so I'm guessing the issue comes from elsewhere? Or maybe am I flushing where I shouldn't? Either way, I'm yet again fresh out of ideas.
Last edited on
https://www.cplusplus.com/reference/ostream/basic_ostream/tellp/
Use tellp and seekp to find out where your next file output (p=put) will go to in the file.

There's also the companion tellg and seekg to find out where your next file input (g=get) will come from.
Topic archived. No new replies allowed.