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
Ad icon
ZTE MF283U 4G Unlocked Router (Used)
ayanthamaxi
Updated:
Sunday at 8:26 PM
ලංකාවේ හොඳම උපකාරක පන්ති සහ ගුරුවරුන් එකම තැනකින් - TopTuition.lk
dulithapathum
Updated:
Saturday at 8:07 AM
Colombo
RidhMathraa ’26 🎶✨
Tmadhusanka
Updated:
Wednesday at 11:58 PM
Ad icon
Colombo
PXN V10 Pro Direct Drive Racing Wheel (Under Warranty)
Abdur Rahman
Updated:
Wednesday at 10:23 PM
Ad icon
USDT ණය සේවාව - USDT Loan Service
පුරවැසියා
Updated:
Wednesday at 4:54 PM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
Computers & Internet
Problems and Troubleshooting
C++ Help
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: 10765261" data-attributes="member: 263471"><p><strong>Here we go buddy.!</strong></p><p></p><p><strong></strong></p><p><strong>This doesn't work with Old compilers such as Borland but definitely works with newer compilers such as Microsoft Visual C++, MinGW</strong></p><p></p><p>Write</p><p>Read</p><p></p><p><img src="http://www.elakiri.com/forum/images/icons/sq/11.gif" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p>[CODE]</p><p>#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>}</p><p>[/CODE]</p><p></p><p><strong>By Core</strong></p></blockquote><p></p>
[QUOTE="Core, post: 10765261, member: 263471"] [b]Here we go buddy.![/b] [B] This doesn't work with Old compilers such as Borland but definitely works with newer compilers such as Microsoft Visual C++, MinGW[/B] Write Read [IMG]http://www.elakiri.com/forum/images/icons/sq/11.gif[/IMG] [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] [B]By Core[/B] [/QUOTE]
Insert quotes…
Verification
Dahaya deken beduwama keeyada?
Post reply
Top
Bottom