Reading and Writing to a txt file.

I need help with this program I'm coding this program is supposed to write to a file and read from the file. Now it has some errors on the write function I can not write the first name. and on the read function I can not see the data that has to appear in the following format

firstname lastname
address
city state zipcode
phonenumber

Please can someone help me and tell me what it is wrong.I am new in this stuff.This is what I have so far.

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

using namespace std;

int write(void);
int write(void)
{
    ofstream outfile;
    char firstname[18],
         lastname[25],
         address[28],
         city[18],
         state[3],
         zipcode[6],
         phonenumber[10];

    outfile.open(("L:\\BU-521\\PersonalData.txt"));
    cout<<"Enter your First Name: ";
    cin.getline(firstname, 18);
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Address: ";
    cin.getline(address, sizeof address);
    cin.ignore();
    cout<<"Enter your City: ";
    cin.getline(city, 18);
    cout<<"Enter your State: ";
    cin.getline(state, sizeof state);
    cin.ignore();
    cout<<"Enter your ZipCode: ";
    cin.getline(zipcode, 6);
    cout<<"Enter your Phone Number: ";
    cin.getline(phonenumber, sizeof phonenumber);
    cin.ignore();

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<address<<setw(2)<<city<<setw(2)<<state<<setw(2)<<zipcode<<setw(2)<<phonenumber<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}

int read(void);
int read(void)
{
    ifstream input;
    char firstname[18],
         lastname[25],
         address[28],
         city[18],
         state[3],
         zipcode[6],
         phonenumber[10];

    int counter =1;
    while(!input.eof())
    {
        input>>firstname>>lastname>>address>>city>>state>>zipcode>>phonenumber;
        cout<<"Information Required. "<<endl;
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<address<<endl;
        cout<<city<<setw(2)<<state<<setw(2)<<zipcode<<endl;
        cout<<phonenumber<<endl;
        counter++;
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
}

int main()
{
      int choice;
      char ch;

      do{
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;

           int write(void);
           int read(void);

           switch(choice)
          {
             case 1:
                     read();
                   break;
             case 2:
                     write();
                   break;
             case 3:

                   break;
             default:
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}
Last edited on
write function
------------------
Change the statement to
outfile.open("L:\\BU-521\\PersonalData.txt",ios::out);

read function
-----------------
In the read funcn, which file are u trying to read?
Please return from the read function.

ifstream input;
input.open("filename",ios::in); //Then only u can read
Ok I did some of the changes sugested I noticed that the program is overwriting a record now I used the ios::app attribute to write more than one record and I also have to use it for the read function?. When I create the loop I want to know If I have to declare another variable different to the counter for example while(index= variable){index++}. This is the new Code I have so far.

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

using namespace std;

int write(void);
int write(void)
{
    ofstream outfile;
    char firstname[18],
         lastname[25],
         address[28],
         city[18],
         state[3],
         zipcode[6],
         phonenumber[11];
    int index;

    outfile.open("L:\\BU-521\\PersonalData.txt",ios::app);

    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Address: ";
    cin.getline(address, sizeof(address));
    cin.ignore();
    cout<<"Enter your City: ";
    cin.getline(city, 18);
    cout<<"Enter your State: ";
    cin.getline(state, sizeof(state));
    cin.ignore();
    cout<<"Enter your ZipCode: ";
    cin.getline(zipcode, 6);
    cout<<"Enter your Phone Number: ";
    cin.getline(phonenumber, sizeof(phonenumber));
    cin.ignore();

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<address<<setw(2)<<city<<setw(2)<<state<<setw(2)<<zipcode<<setw(2)<<phonenumber<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}

int read(void);
int read(void)
{
    ifstream input;
    input.open("L:\\BU-521\\PersonalData.txt",ios::in);

    char firstname[18],
         lastname[25],
         address[28],
         city[18],
         state[3],
         zipcode[6],
         phonenumber[11];

    int counter =1;
    while(!input.eof())
    {
        input>>firstname>>lastname>>address>>city>>state>>zipcode>>phonenumber;
        cout<<"Information Required. "<<endl;
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<address<<endl;
        cout<<city<<setw(2)<<state<<setw(2)<<zipcode<<endl;
        cout<<phonenumber<<endl;
        counter++;
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
    return 0;
}

int main()
{
      int choice;
      char ch;

      do{
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;


           switch(choice)
          {
             case 1:
                     read();
                   break;
             case 2:
                     write();
                   break;
             case 3:

                   break;
             default:
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}

ios::app means 'open the file in append mode'
Its used only while writing to a file not for reading.

If you want to write continuously to a file, u can use ios::app.

I didnt get the qn u asked about the while loop

Now is the program working as expected??
Topic archived. No new replies allowed.