Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Bodim.lk out now !
Manoj Suranga Bandara
Updated:
Yesterday at 3:05 AM
Power Lifting Lever Belt
SkullVamp
Updated:
Jun 13, 2026
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Jun 13, 2026
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Jun 11, 2026
Ad icon
Wechat qr verification
Pawan2005
Updated:
Jun 11, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
C++ File Handling
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="Core" data-source="post: 10765340" data-attributes="member: 263471"><p><strong><span style="font-size: 15px">C++ File Handling</span></strong></p><p><strong></strong></p><p><strong></strong></p><p><strong>At first place I made this codes in this thread for one of pal who needs a help. then I thought this might be valuable for other people as well. if you need any help or want to know how to use file handling in C++ just go through the codes I made in here. I am sure then you can catch up the concept more easily. </strong></p><p><strong>remember this is included only small amount of streaming. still there are lot to learn. </strong></p><p></p><p><a href="http://www.elakiri.com/forum/showpost.php?p=10765261&postcount=11" target="_blank">http://www.elakiri.com/forum/showpost.php?p=10765261&postcount=11</a></p><p></p><p>[CODE]#include <iostream></p><p>#include <fstream></p><p>#include <conio.h></p><p></p><p>using namespace std;</p><p></p><p>struct items{</p><p> int itemNo;</p><p> char itemName[50];</p><p> double itemPrice;</p><p>};</p><p></p><p></p><p>void read();</p><p>void write();</p><p>const char FileName[50] = "FileName.txt";</p><p>char symbol=0;</p><p></p><p>int main()</p><p>{ </p><p> //write them into a file</p><p> int whatToDo;</p><p> cout << "if you want to write press 1 for reading 2" << endl;</p><p> cin >> whatToDo;</p><p></p><p> switch(whatToDo)</p><p> {</p><p> case 1: //WRITE</p><p>write();</p><p> break;</p><p> </p><p> case 2: //READ</p><p>read();</p><p> break;</p><p> default:</p><p> break; </p><p> }</p><p></p><p> </p><p> cin.get();</p><p> return 0; </p><p>}</p><p></p><p></p><p>void write()</p><p>{</p><p> struct items item;</p><p> </p><p> int itemNumber;</p><p> char itemName[50];</p><p> double itemPrice;</p><p> </p><p> </p><p> while(1)</p><p> { </p><p> cout << "What is the item no? " << endl;</p><p> cin >> itemNumber;</p><p> cin.ignore();</p><p> </p><p> cout << "What is the product name?" << endl;</p><p> cin.getline(itemName,50,'\n');</p><p></p><p> cout << "Whatis the price of the product" << endl;</p><p> cin >> itemPrice;</p><p> cin.ignore();</p><p> </p><p> item.itemNo=itemNumber;</p><p> strcpy(item.itemName,itemName);</p><p> item.itemPrice=itemPrice;</p><p> </p><p> //write to the file</p><p> </p><p> ofstream writeFile;</p><p> writeFile.open(FileName,ios::out|ios::app|ios::binary);</p><p> </p><p> if(writeFile.fail())</p><p> {</p><p> cerr << "An error occured..." << endl;</p><p> }else</p><p> {</p><p> </p><p> writeFile.write((char*)&item,sizeof item);</p><p> </p><p> writeFile.clear();</p><p> writeFile.close();</p><p> } </p><p>above:</p><p> cout << "Do you want to enter another record y/n" << endl;</p><p> symbol=0;</p><p> cin >> symbol;</p><p> </p><p> if(symbol == 'y')</p><p> {</p><p> //reloop </p><p> }else if(symbol== 'n')</p><p> {</p><p> break;</p><p> }</p><p> else</p><p> {</p><p> goto above;</p><p> }</p><p> }</p><p></p><p></p><p> return;</p><p>}</p><p> </p><p></p><p>void read()</p><p>{</p><p> struct items item;</p><p> </p><p> ifstream readFile;</p><p> readFile.open(FileName,ios::in|ios::binary);</p><p> </p><p> if(readFile.fail())</p><p> {</p><p> cerr << "An error occured.." << endl;</p><p> </p><p> }else</p><p> {</p><p> while(readFile.read((char*)&item,sizeof item))</p><p> {</p><p> cout << "******************************************************" << endl;</p><p> cout << "the item number is " << endl;</p><p> cout << item.itemNo << endl;</p><p> </p><p> cout << "the item name is " << endl;</p><p> cout << item.itemName << endl;</p><p> </p><p> cout << "the item price is " << endl;</p><p> cout << item.itemPrice << endl;</p><p> cout << "******************************************************" << endl;</p><p> }</p><p></p><p> }</p><p> getch();</p><p>return;</p><p></p><p>}[/CODE]</p><p><span style="font-size: 15px"><span style="color: Black"><strong>Written by Core.!!!</strong></span></span></p></blockquote><p></p>
[QUOTE="Core, post: 10765340, member: 263471"] [B][SIZE=4]C++ File Handling[/SIZE] [/B] [B] At first place I made this codes in this thread for one of pal who needs a help. then I thought this might be valuable for other people as well. if you need any help or want to know how to use file handling in C++ just go through the codes I made in here. I am sure then you can catch up the concept more easily. remember this is included only small amount of streaming. still there are lot to learn. [/B] [URL]http://www.elakiri.com/forum/showpost.php?p=10765261&postcount=11[/URL] [CODE]#include <iostream> #include <fstream> #include <conio.h> using namespace std; struct items{ int itemNo; char itemName[50]; double itemPrice; }; void read(); void write(); const char FileName[50] = "FileName.txt"; char symbol=0; int main() { //write them into a file int whatToDo; cout << "if you want to write press 1 for reading 2" << endl; cin >> whatToDo; switch(whatToDo) { case 1: //WRITE write(); break; case 2: //READ read(); break; default: break; } cin.get(); return 0; } void write() { struct items item; int itemNumber; char itemName[50]; double itemPrice; while(1) { cout << "What is the item no? " << endl; cin >> itemNumber; cin.ignore(); cout << "What is the product name?" << endl; cin.getline(itemName,50,'\n'); cout << "Whatis the price of the product" << endl; cin >> itemPrice; cin.ignore(); item.itemNo=itemNumber; strcpy(item.itemName,itemName); item.itemPrice=itemPrice; //write to the file ofstream writeFile; writeFile.open(FileName,ios::out|ios::app|ios::binary); if(writeFile.fail()) { cerr << "An error occured..." << endl; }else { writeFile.write((char*)&item,sizeof item); writeFile.clear(); writeFile.close(); } above: cout << "Do you want to enter another record y/n" << endl; symbol=0; cin >> symbol; if(symbol == 'y') { //reloop }else if(symbol== 'n') { break; } else { goto above; } } return; } void read() { struct items item; ifstream readFile; readFile.open(FileName,ios::in|ios::binary); if(readFile.fail()) { cerr << "An error occured.." << endl; }else { while(readFile.read((char*)&item,sizeof item)) { cout << "******************************************************" << endl; cout << "the item number is " << endl; cout << item.itemNo << endl; cout << "the item name is " << endl; cout << item.itemName << endl; cout << "the item price is " << endl; cout << item.itemPrice << endl; cout << "******************************************************" << endl; } } getch(); return; }[/CODE] [SIZE=4][COLOR=Black][B]Written by Core.!!![/B][/COLOR][/SIZE] [/QUOTE]
Insert quotes…
Verification
Hath warak paha keeyada? (hatha wadikireema paha)
Post reply
Top
Bottom