Editing an initialized struct member using fstream

Hello guys. I am stuck in the code where I have initialized the struct member but I don't know how can I edit or delete it in a txt file. (Is this possible?) Since I am making a "pc part shop about processors and motherboards. Any tips are appreciated. Also sorry if my code is not clean, I am a beginner.

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

const int size = 5;

struct Computer
{
	string processor[size];
	string motherboard[size];
};

struct Prices
{
	double proc_prices[size];
	double mboard_prices[size];
};

void display_proc(struct Computer comp, struct Prices price);
void display_moth(struct Computer comp, struct Prices price);

int main(){
	int chosen_proc, chosen_moth;
	Computer computer = {
	    {"1. Asus Prime A320M-K AMD AM4 uATX MotherBoard with LED lighting, DDR4 3200MHz, 32Gb/s M.2, HDMI, SATA 6Gb/s, USB 3.0","2. MSI H410M-A PRO LGA 1200 Supports 10th Gen Intel Core and Pentium Gold / Celeron processors"}, // processsor array init
	    {"1. Intel Pentium Dual Core G2030 3.0Ghz 3MB Cache LGA1155 22nm Processor", "2. Intel Core i3-7100 Processor (3M Cache, 3.90 GHz)"} // motherboard array init 
	};
	Prices all_prices = {
		{2499, 5999},
		{2999, 3999}
	};
	system("Color 0A"); 
	
	ofstream proc1("processor.txt", ios::app);
	ofstream mother1("motherboard.txt", ios::app);
	
	cout <<"Choose your Processor!" << endl;
	if(proc1.is_open()){  
   		display_proc(computer, all_prices);
    	proc1.close();//file close  
  	}  
  	else{  
   		cout<<"Error in file opening"<<endl;  
  	} 
	cout <<"Enter a number: ";
	cin >> chosen_proc;
	
	cout <<"Choose your Motherboard!" << endl;
	if(mother1.is_open()){
		display_moth(computer, all_prices);
   		mother1.close();//file close  
	}
	else{
		cout <<"Error in file opening" << endl;
	}		
	cout <<"Enter a number: ";
	cin >> chosen_moth;
	
}
void display_proc(struct Computer comp, struct Prices price)
{
	for(int i = 0; i < size; i++)
	{
		cout << comp.processor[i] << "  "  << endl
		 << "Price: Php"<< price.proc_prices[i] << endl;
		 
	}
}
void display_moth(struct Computer comp, struct Prices price)
{
	for(int i = 0; i < size; i++)
	{
		cout << comp.motherboard[i] << "  "  << endl
		 << "Price: Php"<< price.mboard_prices[i] << endl;
		 
	}
}
Last edited on
So what do you want to edit/delete? Currently you open the file and close it, otherwise you're doing nothing.

if you want do 'display' the data to a file:
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
void display_proc(ostream& os, struct Computer comp, struct Prices price);

...

	cout <<"Choose your Processor!" << endl;
	if(proc1.is_open()){  
   		display_proc(proc1, computer, all_prices);
   		display_proc(cout, computer, all_prices);
    	proc1.close();//file close  
  	}  
  	else{  
   		cout<<"Error in file opening"<<endl;  
  	} 


...

void display_proc(ostream& os, struct Computer comp, struct Prices price)
{
	for(int i = 0; i < size; i++)
	{
		os<< comp.processor[i] << "  "  << endl
		 << "Price: Php"<< price.proc_prices[i] << endl;
		 
	}
}

When opening the file you shouldn't use ios::app if you don't want to append but modifing.
Not sure I really understand what you are trying to achieve. For processing 'sequential' text files, it is very difficult to change data in the text file. This type of processing is often done by reading the whole of a file into a container (often a vector of struct), adding/removing/amending data as required and then writing all the data from the container back to the file, overwriting what was their before.

If you just want to add data to the end of the file then this can be done simply by appending the data.

What are the assignment requirements?
Topic archived. No new replies allowed.