I/O, fstream error.

Hello,

Could you help me fix it?
I use Xcode on Mac.
When I ender input, name and age, it saves in the .txt file only the first latter. Not all information.

I need to save all information.

Thank you in advance.


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

using namespace std;

int number;
string text;
string line;
int main() {

    
fstream myfile;
myfile.open ("/users/alexey/documents/cpp2/sample.txt");
    cout << "please type you name" << endl;
    cin >> text;
    myfile << text;
    cout << "how old are you?" << endl;
    cin >> number;
    myfile << number;
    
    if(myfile.is_open())
    {
     while ( getline (myfile,line) )
        {
            cout << line << endl;
        }
    }
    else {
        cout << "file can't be open" << endl;
    }
    
    myfile.close();
    }
Last edited on
A few things:
first you need to create your file properly, then you need to check if it is ok. You also need to separate the different parts by a whitespace character. After you have written your data you need to go back to the beginning to read the data.
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
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib> // perror

using namespace std;

int main()
{
  int number;
  string text;

  fstream myfile("sample.txt", ios::in | ios::out | ios::app);
  if (!myfile)
  {
    perror("Error creating file ");
    return -1;
  }
  cout << "please type you name" << endl;
  cin >> text;
  myfile << text << "\t";
  cout << "how old are you?" << endl;
  cin >> number;
  myfile << number << "\n";

  myfile.seekg(0, ios::beg); // go back to beginning to read
  string name;
  int age;

  while (myfile >> name >> age)
  {
    cout << name << "\t" << age << "\n";
  }
}
I'm running this on windows, I changed the file open mode to get it to work.

After writing the data to the file there are two ways to proceed:
a) close the file and open it again
or
b) reset the position to the start.

I used the latter here.

(Sorry, I moved some of the variable declarations to closer to where they were first used, it made it easier for me to see whether the variable was a string or an integer and so on.)

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

using namespace std;


int main()
{
    const char * fname = "/users/alexey/documents/cpp2/sample.txt";
//    const char * fname = "sample.txt";
    
    fstream myfile(fname, ios::in | ios::out | ios::trunc);
    
    if (!myfile)
    {
        cout << "Unable to open file: " << fname << endl;
        return 1;
    }
        
    cout << "please type you name" << endl;
    string text;
    cin >> text;
    
    myfile << text << ' ';
    
    cout << "how old are you?" << endl;
    int number;
    cin >> number;
    
    myfile << number;

    myfile.seekg(0); // reset position to start

    string line;
    
    while ( getline (myfile,line) )
    {
        cout << line << endl;
    }
    
    myfile.close();
}


please type you name
asdfg
how old are you?
123
asdfg 123

If you want the items on separate lines, output a newline '\n' to the file after each one.
Thank you very much Mr. Thomas and Mr, Chervil!
Thomas and Chervil, thank you very much again, a very good code, I am happy to read it! :)
You're welcome, @mynameisalexey.
Topic archived. No new replies allowed.