Creating and writing to file

closed account (9hUqko23)
I need to output all of the data I get to a text file. How would I go about doing this? I've read and re-read the chapter in my old C++ book but I can't figure it out. Could some give me an example using parts of my code?
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
#include "stdafx.h" //trying to output data somewhere
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

int main(void)
{
	ofstream outputFile; //trying to output this data somewhere.
	outputFile.open("program3data.txt")

	if a, b, bb, c, d, e;
	char A, C;
	cout << "Input an alarm priority" << endl;
	cin >> a;
	if (a == 1)
	{
		cout << "Input a number to determine which equipment type you move to. " << endl;
		cout << "1 = PCB's 2 = Reg's 3 = xfmr's 4 = everything else" << endl;
		cin >> e;
		if (e == 1)
		{
			//code to output all of the analog telmetries assigned priority of one
			cout << "Input PCB prefix" << endl;
			cin >> A;
			cout << "Input MEAG Equipment number" << endl;
			cin >> bb;
			//since all of the alarm telemetries are the same -or should be- I can output the telemetries as the same for each
			//equipment number.
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "A(3)" << " " << "VOLTS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "B(2)" << " " << "VOLTS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "C(1)" << " " << "VOLTS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "NEUTRAL AMPS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "A(3)" << " " << "AMPS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "B(2)" << " " << "AMPS" << endl;
			cout << "PCB" << " " << "#" << A << "-" << bb << " " << "PHASE" << " " << "C(1)" << " " << "AMPS" << endl;
		}
	else if (e == 2)
		{
			cout << "Enter a Regulator equiment prefix" << endl;
			cin >> C;
			cout << "Enter a Regulator equipment number" << endl;
			cin >> d;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "A(3)" << " " << "VOLTS" << endl;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "B(2)" << " " << "VOLTS" << endl;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "C(1)" << " " << "VOLTS" << endl;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "A(3)" << " " << "AMPS" << endl;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "B(2)" << " " << "AMPS" << endl;
			cout << "REGULATOR" << " " << "#" << C << "-" << d << " " << "PHASE" << " " << "C(1)" << " " << "AMPS" << endl;
		}
		//use a for loop to output all of the equipment numbers for the tranformers since the nunmber will either be A, B,
		//C, D, E, F, G, H, or I.
		else if (e == 3)
		{
			for (int t = 0; t <= 20; t++)
			{
				cout << "XFMR" << " " << "A" << " " << "INST AMPS" << endl;
			}
			for (int s = 0; s <= 20; s++)
			{
				cout << "XFMR" << " " << "B" << " " << "INST VOLTS" << endl;
			}
			for (int r = 0; r <= 20; r++)
			{
				cout << "XFMR" << " " << "C" << " " << "UNDERFREQUENCY" << endl;
			}
		}
		else if (e == 4)
		{
			//All other priority one nemenclatures
		}
		else
		{
			cout << "there are no other alarm priority nomencaltures of one." << endl;
		}
		//code for the rest fo the alarm nomenclatures.
	}
	else if (a == 2)
	{
		cout << "code to out put all of the analog telemetries assigned priority of two." << endl;
	}
	else if (a == 3)
	{
		cout << "code to output all of the analog telemetries assigned priority of three." << endl;
	}
	else if (a == 4)
	{
		cout << "code to output all of the analog telemetries assigned priority of four." << endl;
	}
	else if (a == 0)
	{
		cout << "code to output all of the analog telemetries assigned priority of five." << endl;
	}
	else;
	{
		cout << "Not a recognized alarm priority" << endl;
	}
	outputFile.close(); //trying to out put this data somwhere
	cout << "Done!" << endl;

	return 0;
}
  
Last edited on
Well, all your program does is checking with some IFs, and some for loops. If you want to save to outputFile, you should use it first :) Right now you only open it and close. Idk what data you want to store, but here's an example of my code for saving some data to file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::ofstream file;

file.open("path/filename");
if( file.is_open() )
{
     std::vector<int> vec(4, 15);//Vector with 4 ints of value 15.
     for(auto& it : vec)
     {
          file << it << std::endl; // Put every item from vec in separate line, in file.
     }
     file.flush();
     file.close();
}
else
{
//show error
}


Remember, that opening a file in this mode will clear(delete) its previous content.
Cheers!
Topic archived. No new replies allowed.