Writing and reading from file?

I am using my last week's assignment to read and write my reversed string to a file but I have no idea how to go about it, any ideas? I tried looking into fstream but it is a bit confusing. Here is what I got so far, a non-working code of course.

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


using namespace std;

string reverse(string); 
string file(string);
//char* strrev(char* text);

int main()
{
	ifstream file;
	ofstream file;
	string text;
	char word[20];
	
	// Prompt
	cout << "Enter a string  : ";
	getline(cin, text);

	// Reverse
	cout << "Reversed        : ";
	string text_temp = reverse(text);

	file.open("file.txt");
	file >> text_temp;
	cout << output;
	file.close();

	cout << endl << "Inversed        : ";
	for ( int i = text_temp.length()-1; i >= 0; i-- ) 
	cout << text_temp[i]; 
	cout << endl;

	return 0;
}



string reverse(string text)
{
    string reverse;
    char msg;

    // for int i, i = length of string, 
    for (int i = text.length() - 1; i >= 0; i--)
    {
      // msg = each letter subtracting down length
      msg = text[i];
	  // reverse is now each letter in msg adding up
      reverse += msg;
    }
    
	return reverse;

}
Use << operator to write string to a file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	fstream file; // use fstream (for reading and writing)
	string text;

	// Prompt
	cout << "Enter a string  : ";
	getline(cin, text);

	// Reverse
	cout << "Reversed        : ";
	string text_temp = reverse(text);

       // Write the reversed string to file
	file.open("file.txt");
	file << text_temp;
	file.close();

	return 0;
}

If you want to read the string from file, use getline(file,text); and don't forget to open the file before reading from it
Line 30: What is 'output'?

One mistake here is that you named your ifstream and ofstream the same variable name. This is just like if you had:
1
2
int a;
char a;

Not going to work so great.

For a beginner use of ifstream/ofstream, they can be viewed exactly as cout/cin. Expect that you have to declare, open, and close them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream inFile("input.txt");  // opens input.txt for reading
ofstream outFile("output.txt");  // opens output.txt for writing
int i;

cin >> i; // Reads 'i' from your keyboard
inFile >> i; // Reads 'i' from input.txt

cout << i;  // Write 'i' to the console
ouFile << i; // Write 'i' to output.txt



inFile.close();
outFile.close()


That's just to show the similarities between keyboard/files. Not really copy/paste into code material.

I don't want to get too complicated but the relationship is even closer. cin/isfstream are istreams, and cout/ofstream are ostreams. You can have something 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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void printData(string data, ostream& out);

int main(void)
{
  ofstream outFile("output.txt");
  string welcome = "Hello ";
  string yourName;

  cout << "Please enter your name: ";
  getline(cin, yourName);

  welcome += yourName;

  printData(welcome, cout);  // Print to console
  printData(welcome, outFile); // print to file

  outFile.close():
  return 0;
}

void prinData(string data, ofstream& out)
{
  out << data;
}
Topic archived. No new replies allowed.