File Handling

closed account (oy721hU5)
Hello all,

I am writing a string of characters using character arrays only. For instance, it asks the user for how many records you want. Say 3, it can vary. Then you type three records. These three records will be written to a text file... Please look at the end of this post for ref.

Please and thank you!
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>

#include <fstream>

using namespace std;

class CFile
{
	public:
		CFile(){}


		void ClearTheScreen()
		{
			system("cls");
		}
		void NormalTermination()
		{
			cout << "\n\n\n\t\t\t";
		}
		void OpenTheFile()
		{
			WRITE.open("File02.txt",ios::app);
		}
		void CloseTheFile()
		{
			WRITE.close();
		}
		void WriteToTheFile()
		{
			int k,RecordsToWrite,m, n;
			char Buffer[50];

			cout << "\t\t\tHow many records you want : ";
			cin >> RecordsToWrite;
			for ( k = 0; k < RecordsToWrite; k++ )
			{
				PrepareTheFruits();
				cout << "Give me a fruit : ";
				cin >> Buffer;

				m = 0;
				while ( Buffer[m] != '\0' )
				{
					Fruits[m] = Buffer[m];
					m++;
				}
				WRITE << "\n"  << Fruits;
            }
            cout << "\n\t\t\t" << Fruits;

        }
		void PrepareTheFruits()
		{
			int k;
			for ( k = 0; k < 50; k++)
			{
				Fruits[k] = 32; // 32 is the ascii code for SPACE
			}
			Fruits[49] = '\0';
		}
        private:
            char Fruits[50];
            ofstream WRITE;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "file.h"

int main()
{
	CFile MyFile;

	MyFile.OpenTheFile();     // opens the file
	MyFile.WriteToTheFile();  // stores user's input on the .txt file
        MyFile.CloseTheFile();
        MyFile.NormalTermination();

	return 0;
}


Like this:
Please enter a fruit: Orange // this is the programs prompting
Please enter a fruit: Pear // and user input
Please enter a fruit: Lemom

// Here is where I have problems, I must display them like this
Orange
Pear
Lemon


Last edited on
Topic archived. No new replies allowed.