C++ File Handling

Core

Member
Jan 23, 2010
3,120
195
0
Milky Way/Local Cluster/Local Sol/Earth
C++ File Handling


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.


http://www.elakiri.com/forum/showpost.php?p=10765261&postcount=11

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;

}
Written by Core.!!!