0
  hie guys could anyone help me on how i can read a delimited file as i view all customer details 








                #include <fstream
                #include <iostream>
                #include <string>

                using namespace std;
                int main ()

                {

                int choice [1];
                string name;
                string idnumber;
                string address;
                string surname;
                string FileName;
                fstream myfile;
                string line;







            cout<<"================================================================================"<<endl;
            cout<<" CUSTOMER DETAILS FILE "<<endl;
            cout<<"================================================================================"<<endl;
            cout<<" "<<endl;
            cout<<" "<<endl;
            cout<<" "<<endl;
            cout<<"******************************************************************************** "<<endl;
            cout<<" 1) ADD NEW CUSTOMER "<<endl;
            cout <<" "<<endl;
            cout<<" 2) EDIT CUSTOMER'S DETAILS "<<endl;
            cout<<" "<<endl;
            cout<<" 3) DELETE CUSTOMER'S DETAIL "<<endl;
            cout <<" "<<endl;
            cout <<" 4) VIEW CUSTOMER DETAILS "<<endl;
            cout <<" "<<endl;
            cout <<" 5) BACK TO MAIN PAGE "<<endl;
            cout<<"******************************************************************************** "<<endl;
            cout<<" "<<endl;
            cout<<" please enter your choice :";
            cin>> choice[1];
            system("cls");
            if (choice [1]==1)



            //asking the user to input required data
            {
            cout <<" "<<endl;
            cout <<"================================================================================"<<endl;
            cout <<" NEW CUSTOMER "<<endl;
            cout <<"================================================================================"<<endl;
            cout <<" "<<endl;
            cout <<" "<<endl;

            cout <<" "<<endl;
            cout <<"enter customer name :" ;
            cin >> name;


            cout <<" "<<endl;
            cout <<"enter customer surname :";
            cin>>surname;


            cout <<" "<<endl;
            cout <<"enter customer I.D :";
            cin>>idnumber;



            cout <<" "<<endl;
            cout <<"enter customer aaddress :";
            cin>>address;

            // saving inputted text

            ofstream myfile;
            myfile.open ("TBS.txt",ios::out|ios::app);
            myfile << name<<'\t'<<surname<<'\t'<<idnumber<<'\t'<<address<<endl;
            myfile.close();
            system("cls");


            cout <<""<<endl;
            cout <<"================================================================================ "<<endl;
            cout <<""<<endl;
            cout <<"*************NEW CUSTOMER DETAILS HAS BEEN ADDED SUCCESFULLY***** "<<endl;
            cout <<""<<endl;
            cout <<"================================================================================ "<<endl;
            }
            else if (choice [1]==2)
            {

            cout <<" "<<endl;
            cout <<"================================================================================"<<endl;
            cout <<" EDIT CUSTOMER DETAILS"<<endl;
            cout <<"================================================================================"<<endl;
            cout <<" "<<endl;
            cout <<" "<<endl;
            }

            \\ function to view all details
            else if (choice[1]== 4)
            {
            string getcontent;
            ifstream myfile ("TBS.txt",ios::in);
            if(myfile.is_open())
            {
            while( myfile.eof())
            {
            myfile >> getcontent;
            cout << getcontent <<endl;
            }
            }
            }

            system("PAUSE>null");
            return 0;
            }

Edited by tyggagucci

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by deceptikon
0

Reading a delimited file can be super easy or somewhat tricky depending on the format. If you're doing something super basic where the delimiter isn't allowed to be embedded in field data, it's almost trivial:

#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

namespace file_management
{
    using namespace std;

    vector<string> read_fields(istream& in, char delimiter)
    {
        vector<string> fields;
        string line;

        if (getline(in, line))
        {
            istringstream iss(line);
            string field;

            while (getline(iss, field, delimiter))
            {
                fields.push_back(field);
            }
        }

        return fields;
    }
}

int main()
{
    using namespace file_management;
    using namespace std;

    ifstream in("test.txt");

    if (in)
    {
        vector<string> fields;

        while (fields = read_fields(in, '\t'), !fields.empty())
        {
            for (int i = 0; i < fields.size(); ++i)
            {
                cout << '"' << fields[i] << '"' << (i < fields.size() - 1 ? ", " : "\n");
            }
        }
    }
}

If embedded delimiters are allowed, such as with the full CSV format, then you need to do a bit more parsing to handle the escape sequences.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.