File Handling C++

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// .h file
#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;
};


// main.cpp
#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
If you don't want to display the fruits until all have been entered, you're going to have to store them in an array.

Can you use std::string ?
std::string would make this a lot cleaner. Fruits would become simply an array (or vector) of strings. Copy and clear functions are built in to std::string. You wouldn't have to code those yourself.

Please don't repost the same question.
http://www.cplusplus.com/forum/beginner/128141/
If you don't get a response after a reasonable time, simply bump the original thread.
closed account (oy721hU5)
Hello AbstractionAnon,

I am not to use std::string.

And thanks, I have marked the other post as solved.
I am not to use std::string

Then you will need to use a two dimensional character array.

1
2
3
static int max_fruits = 50;

  char Fruits[max_fruits][50];


Actually, I would suggest making Fruit its own class. The Fruit class would then be responsible for intiializing each instance of itself. A setFruitName function could copy the user input into the instance of Fruit. Line 67 becomes an array of Fruits instances. A getFruitName function could retrieve a pointer to the Fruit name.
Last edited on
closed account (oy721hU5)
I mean, the data will be written to a txt file. Once we write, then we could use a read method to display them.... I just do not know how. Please help.
Last edited on
Topic archived. No new replies allowed.