Trouble reading from text file

Hello everyone, hope your day is going well.

mines not so well, I can't figure out why this isn't working. I'm learning how to write and read from a file,

This program take 3 words (I know I wrote string's name as Sentence, but its just supposed to work for words) and stores them* into text file. It does that part correctly, but then it can't read it out correctly! What am I doing wrong?

thanks guys/gals


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

int main()
{
	std::string sSentence = " ";
	std::fstream SentenceFile;
	SentenceFile.open("C:\\Users\\[MYNAME]\\Desktop\\Sentences.txt");

	std::cout << "enter sent";
	std::cin >> sSentence;
	SentenceFile << sSentence << std::endl;
	std::cin >> sSentence;
	SentenceFile << sSentence<< std::endl;
	std::cin >> sSentence;
	SentenceFile << sSentence << std::endl;


	SentenceFile >> sSentence;
	std::cout << sSentence <<std::endl;
	SentenceFile >> sSentence;
	std::cout << sSentence << std::endl;
	SentenceFile >> sSentence;
	std::cout << sSentence << std::endl;
	
	SentenceFile.close();
	std::getchar();
	std::getchar();
	return 0;
}


*typo
Last edited on
working for me with stringstream, which is almost exactly like fstream, but easier to demo online. Did you try to enter more than three space-separated words?

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

int main()
{
    std::string word;
    std::stringstream ss;

    std::cout << "enter 3 space-separated words:" << std::endl;
    std::cin >> word;
    ss << word << std::endl;
    std::cin >> word;
    ss << word<< std::endl;
    std::cin >> word;
    ss << word << std::endl;
    
    std::cout << "Your words are:" << std::endl;
    ss >> word;
    std::cout << word <<std::endl;
    ss >> word;
    std::cout << word << std::endl;
    ss >> word;
    std::cout << word << std::endl;

    return 0;
}
You need to close and reopen the file or rewind it to the beginning to read from it.

1
2
3
4
5
6
7
8
9
10
ofstream fout("words.txt");
string word;
for (int i = 0; i < 3; i++) {
    cin >> word;
    fout << w << '\n';
}
f.close();
ifstream fin("words.txt");
while (fin >> word)
    cout << word << '\n';

Uh it inserts the words completely correctly,

it's the cout part it fails to do.
I don't know the sstream library, I want to learn fstream.
Oh okay I didnt see @tpb, but I fixed it.

is there any way to set it back to the start without closing/opening it?

heres my new code:
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::string sSentence = " ";
	std::ofstream SentenceFile;
	SentenceFile.open("C:\\Users\\rehanyh3724\\Desktop\\Sentences.txt");

	std::cout << "enter sent";
	std::cin >> sSentence;
	SentenceFile << sSentence << std::endl;
	std::cin >> sSentence;
	SentenceFile << sSentence<< std::endl;
	std::cin >> sSentence;
	SentenceFile << sSentence << std::endl;

	std::ifstream SentenceFileo;
	SentenceFileo.open("C:\\Users\\rehanyh3724\\Desktop\\Sentences.txt");
	SentenceFileo >> sSentence;
	std::cout << sSentence <<std::endl;
	SentenceFileo >> sSentence;
	std::cout << sSentence << std::endl;
	SentenceFileo >> sSentence;
	std::cout << sSentence << std::endl;
	
	SentenceFile.close();
	SentenceFileo.close();
	std::getchar();
	std::getchar();
	return 0;
}

You can use seekg. I opened the file with truncate below so it starts empty even if it already has something in it. Alternatively you could open it with ios_base::app instead of trunc to append lines to the existing contents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    fstream f("words.txt", ios_base::in|ios_base::out|ios_base::trunc);
    string word;
    for (int i = 0; i < 3; i++) {
        cin >> word;
        f << word << '\n';
    }
    f.seekg(0);
    while (f >> word)
        cout << word << '\n';
    return 0;
}

Topic archived. No new replies allowed.