473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use a text files as a sql fields to edit,delete and modify records using c++

i want to search and display the field stored in text file and also i want to delete the field that is entered. help me with this code. case 3 and 4 is incomplete.


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     int a;
  8.     int st_id;
  9.     string st_name,st_place,ch;
  10.     ofstream ofile;
  11.     ifstream nfile;
  12.     do
  13.     {
  14.         cout<<"\n1.Insertion";
  15.         cout<<"\n2.Display";
  16.         cout<<"\n3.Delete";
  17.         cout<<"\n4.Search";
  18.         cout<<"\n Press 0 to exit";
  19.         cout<<"\nEnter your choice\t";
  20.         cin>>a;
  21.         switch(a)
  22.         {
  23.         case 1:
  24.             ofile.open("student.txt",ios::out | ios::app | ios::binary);
  25.  
  26.             if(ofile.is_open())
  27.             {
  28.                 cout<<"\nEnter the Student Id: ";
  29.                 cin>>st_id;
  30.                 cout<<"\nEnter the name of the student: ";
  31.                 cin>>st_name;
  32.                 cout<<"\nEnter the location of student: ";
  33.                 cin>>st_place;
  34.                 long begin,end;
  35.                 ifstream myfile ("student.txt");
  36.                 begin = myfile.tellg();
  37.                 myfile.seekg (0, ios::end);
  38.                 end = myfile.tellg();
  39.                 myfile.close();
  40.                 if((end-begin)==0)
  41.                 {
  42.                     ofile<<"\nstudent id\t"<<"student name\t"<<"location\n";
  43.                 }
  44.                 ofile<<"\n"<<st_id<<"\t\t"<<st_name<<"\t\t"<<st_place<<"\n";
  45.                 ofile.close();
  46.             }
  47.             else
  48.                 cout<<"failed to Insert values into file";
  49.             break;
  50.         case 2:
  51.             nfile.open("student.txt",ios::in);
  52.             if(nfile.is_open())
  53.             {
  54.  
  55.                 getline(nfile,ch);
  56.                 cout<<ch<<" inside print   "<<endl;
  57.                 nfile.close();
  58.             }
  59.             else
  60.                 cout<<"failed to get values";
  61.             break;
  62.         case 3:
  63.             ofile.open("student.txt",ios::binary|ios::in);
  64.             if(ofile.is_open())
  65.             {
  66.                 int num,choice;
  67.                 string plo;
  68.                 cout<<"\nEnter your choice to remove";
  69.                 cout<<"\nTo delete elements by ID press 1";
  70.                 cout<<"\nTo delete elements by location or name press 2\n";
  71.                 cin>>choice;
  72.                 switch(choice)
  73.                 {
  74.                 case 1:
  75.                     cout<<"\nEnter the id remove:";
  76.                     cin>>num;
  77.                     if(num==st_id)
  78.                     {
  79.  
  80.                         cout<<"removed";
  81.                     }
  82.                     else
  83.                         cout<<"\nId not found";
  84.                     break;
  85.  
  86.                 case 2:
  87.                     cout<<"\nEnter the name or location of the student:";
  88.                     cin>>plo;
  89.                     if(plo==st_name)
  90.                     {
  91.                         cout<<"removed";
  92.                     }
  93.                     else if(plo==st_place)
  94.                     {
  95.                         cout<<"removed";
  96.                     }
  97.                     else
  98.                         cout<<"\n content is not found";
  99.                     break;
  100.                 }
  101.  
  102.  
  103.             }
  104.             break;
  105.         case 4:
  106.             ofile.open("student.txt",ios::binary|ios::in);
  107.             if(ofile.is_open())
  108.             {
  109.                 int sid,option;
  110.                 string spn;
  111.                 cout<<"\nchoose your option for search";
  112.                 cout<<"\nTo search elements by ID press 1";
  113.                 cout<<"\nTo search elements by location or name press 2\n";
  114.                 cin>>option;
  115.                 switch(option)
  116.                 {
  117.                 case 1:
  118.                     cout<<"\nEnter the id for search:";
  119.                     cin>>sid;
  120.                     if(sid==st_id)
  121.                     {
  122.  
  123.                         cout<<st_id<<"\t"<<st_name<<"\t"<<st_place;
  124.                         cout<<"content";
  125.                     }
  126.                     else
  127.                         cout<<"\nId not found";
  128.                     break;
  129.  
  130.                 case 2:
  131.                     cout<<"\nEnter the name or location of the student:";
  132.                     cin>>spn;
  133.                     if(spn==st_name)
  134.                     {
  135.                         cout<<st_id<<"\t"<<st_name<<"\t"<<st_place;
  136.                         cout<<"content";
  137.                     }
  138.                     else if(spn==st_place)
  139.                     {
  140.                         cout<<st_id<<"\t"<<st_name<<"\t"<<st_place;
  141.                         cout<<"content";
  142.                     }
  143.                     else
  144.                         cout<<"\n content is not found";
  145.                     break;
  146.                 }
  147.  
  148.  
  149.             }
  150.             break;
  151.         }
  152.     }while(a!=0);
  153.     return 0;
  154. }


thanks for the lookup and suggestion
Aug 19 '13 #1
6 1624
weaknessforcats
9,208 Expert Mod 8TB
I believe you are writing the program too soon. Put this aside and start a new project. In this project you will have functions to manage a database. In this case a text file. The function should return a FILE* to the file it created.

Write a function to create the database by creating the file.

Now write the main(), call this function and verify the database is created. I recommend you not use binary as a beginner.

Now write a function to add a student. I recommend a struct here with a member for student ID, a string for student name and a string for student location.

Expand|Select|Wrap|Line Numbers
  1. bool write(FILE* pf, string id, string name, string loc);
  2.  
The function to add the student must be sure the record layout is exactly correct. Let's assume your record layout is:

1 - 8 student id
9-38 student name
39-50 student location.

The add function UNPACKS the struct puts the student id in an 8 byte array, outs the student name in a 30 byte array and puts the student location in an 11 byte array. It then writes the three arrays in the correct order terminated by a \n.

You will need to use string::c_str() to convert the string object into a C style string.

Now test this function in your main().


You should be able to see the contents if your file using notepad or for Linux maybe vi.

Next write a read function that reads a record from the file and returns a struct with the members filled with data from the file.

Next write search functions. One to search for student name, one for location and one for student id. The functions return a FILE* which is positioned to the record searched for. If the data is not found the FILE* returned is zero.

After the search you can call your read to get the data which will be returned in a struct for you. From here you can just display the struct members.

Try to write a function with a FILE* argument rather than doing seek() and tell all over the place. Manage your data using only your database management functions. If there is a problem you can quickly isolate to the file management functions as opposed to starting to debug from main() each time.

Post again and let me know what happened.
Aug 19 '13 #2
I tried that too. but while getting input it leaves the field id.and when trying to search and display it prints both the if and else loops
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class file
  5. {
  6. public:
  7.     char st_id[256],st_name[256],st_place[256];
  8.     string ch;
  9.     void getdata();
  10.     void putdata();
  11.     void display();
  12.     void search();
  13. };
  14. void file::getdata()
  15. {
  16.     ofstream ofile("student.txt",ios::out|ios::app|ios::binary);
  17.     if(ofile.is_open())
  18.     {
  19.         cout<<endl;
  20.         cout<<"\nEnter the Student Id:"<<endl;
  21.         cin.getline(st_id,256);
  22.         cout<<"\nEnter the name of the student:";
  23.         cin.getline(st_name,256);
  24.         cout<<"\nEnter the location of student:";
  25.         cin.getline(st_place,256);
  26.     }
  27.     else
  28.         cout<<"\nFailed to create and Insert";
  29. }
  30. void file::putdata()
  31. {
  32.     ofstream ofile1("student.txt",ios::out|ios::binary|ios::app);
  33.     ifstream nfile("student.text",ios::in);
  34.     long begin,end;
  35.     nfile.open("student.txt");
  36.     begin = nfile.tellg();
  37.     nfile.seekg (0, ios::end);
  38.     end = nfile.tellg();
  39.     nfile.close();
  40.     if((end-begin)==0)
  41.     {
  42.         ofile1<<"student id\t\t"<<"student name\t\t"<<"location";
  43.     }
  44.     else
  45.         ofile1<<st_id<<"\t\t"<<st_name<<"\t\t"<<st_place<<"\n";
  46.     ofile1.close();
  47.  
  48. }
  49. void file::display()
  50. {
  51.     ifstream nfile1;
  52.     nfile1.open("student.txt",ios::in|ios::out);
  53.     if(nfile1.is_open())
  54.     {
  55.         while(!nfile1.eof())
  56.         {
  57.             getline(nfile1,ch);
  58.             cout<<ch;
  59.         }
  60.         nfile1.close();
  61.     }
  62.     else
  63.         cout<<"failed to get values";
  64. }
  65. void file::search()
  66. {
  67.     ifstream infile;
  68.     infile.open("student.txt",ios::in|ios::out);
  69.     if(infile.is_open())
  70.     {
  71.         string sword;
  72.         cout<<"\nEnter the search word :";
  73.         cin>>sword;
  74.  
  75.         while(!infile.eof())
  76.         {
  77.             getline(infile,ch);
  78.             int i,pos;
  79.             for(i=0;i<=ch.length();i++)
  80.                 ch[i]=ch[i];
  81.             for(i=0;i<=sword.length();i++)
  82.                 sword[i]=sword[i];
  83.             pos=ch.find(sword);
  84.             if(pos!=string::npos)
  85.             {
  86.                 cout<<ch<<endl;
  87.  
  88.             }
  89.             else
  90.             {
  91.                 cout<<"word not found"<<endl;
  92.             }
  93.         }
  94.         infile.close();
  95.     }
  96.  
  97. }
  98.  
  99. int main()
  100. {
  101.     file obj;
  102.     int a;
  103.     cout<<"\n1.Insertion";
  104.     cout<<"\n2.Display";
  105.     cout<<"\n3.Search";
  106.     cout<<"\nEnter your choice\t";
  107.     cin>>a;
  108.     switch(a)
  109.     {
  110.     case 1:
  111.         obj.getdata();
  112.         obj.putdata();
  113.         break;
  114.     case 2:
  115.         obj.display();
  116.         break;
  117.     case 3:
  118.         obj.search();
  119.     }
  120.     return 0;
  121. }
  122.  
Aug 20 '13 #3
i don't no how to use that bool write. i am not fresher. i have just completed my college.i don't have any work experience to know fully
Aug 20 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
Let's start with this function:
Expand|Select|Wrap|Line Numbers
  1. void file::putdata()
  2. {
  3.     ofstream ofile1("student.txt",ios::out|ios::binary|ios::app);
  4.     ifstream nfile("student.text",ios::in);
  5.     long begin,end;
  6.     nfile.open("student.txt");
  7.     begin = nfile.tellg();
  8.     nfile.seekg (0, ios::end);
  9.     end = nfile.tellg();
  10.     nfile.close();
  11.     if((end-begin)==0)
  12.     {
  13.         ofile1<<"student id\t\t"<<"student name\t\t"<<"location";
  14.     }
  15.     else
  16.         ofile1<<st_id<<"\t\t"<<st_name<<"\t\t"<<st_place<<"\n";
  17.     ofile1.close();
  18.  
  19. }
  20.  
First, the function should return at least a bool. Maybe true if the function worked and false if it didn't.

Second. I would not open the file in each function. Instead I would have a data member for the ofstream and I would write an open(string) function which can be called by the other member functions. There is duplicated code in every function to open the ofstream.

Third, I would not use ios::binary. You ware writing characters and you need to be sure each record is the same size.

Fourth, after you open the file, tellg() does not position to the beginning of the file. Use seekg to ios::beg.

Fifth, all that should be in the ofstream is the data. It looks like there are column names being written, You supply the column names only when you display.

putdata should look like:

Expand|Select|Wrap|Line Numbers
  1. void file::putdata()
  2. {
  3.  ofile1<<st_id<< st_name << st_place <<\n;
  4. }
and that's it. putdata just puts the data. It does not open the file, does not add any data of it's own, does not display data because it's not a display function.

BTW if you feel you need to display in putdata, then use an int return and return a number. Then have an error function take the int as an argument and display the error message.

Maybe you have a seekg to ios::end if this function appends to the end of the file.

Lastly, change you array size form 256 to something small, like 10. That way you can see the file on one line using notepad or vi. (I don't know what OS you are using). When everything works you can change back to 256.

Post again and I'll parse another of the functions.
Aug 20 '13 #5
sir,there is no problem in "putdata" function.

I used tellg function to use the titles only once during the compilation. To avoid the content title to print and save each and every time it is executed.

I have problem with search and getdata function.

In getdata function it does't get the data for ID(first charater) from the user Instead of that it skip to next name and place to get the data from the user.That is the problem over there.

And in search function it retrieve the data from the file that is searched but it also prints the else loop content "word not found". It checks each and every word used in the file.

To avoid overwriting and save the file i use ios::binary and ios::app(append).The task is to get id,name and place as much as user entered.

If i use string to get that when the user type the first name and space to write the second name it take the second name as a input for 3rd string and exit the loop.That is why i used char and provided it with 256 length.


May be my English for explanation of program is poor.so for understanding what i am trying to say please compile the program i also made some modification over here. say it and say.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class file
  5. {
  6. public:
  7.     char st_id[256],st_name[256],st_place[256];
  8.     string ch,sword;
  9.     void getdata();
  10.     void putdata();
  11.     void display();
  12.     void search();
  13. };
  14. void file::getdata()
  15. {
  16.     ofstream ofile("student.txt",ios::out|ios::app|ios::binary);
  17.     if(ofile.is_open())
  18.     {
  19.         cout<<endl;
  20.         cout<<"\nEnter the Student Id:"<<endl;
  21.         cin.getline(st_id,256);
  22.         cout<<"\nEnter the name of the student:";
  23.         cin.getline(st_name,256);
  24.         cout<<"\nEnter the location of student:";
  25.         cin.getline(st_place,256);
  26.     }
  27.     else
  28.         cout<<"\nFailed to create and Insert";
  29. }
  30. void file::putdata()
  31. {
  32.     ofstream ofile1("student.txt",ios::out|ios::binary|ios::app);
  33.     ifstream nfile("student.text",ios::in);
  34.     long begin,end;
  35.     nfile.open("student.txt");
  36.     begin = nfile.tellg();
  37.     nfile.seekg (0, ios::end);
  38.     end = nfile.tellg();
  39.     nfile.close();
  40.     if((end-begin)==0)
  41.     {
  42.         ofile1<<"student id\t\t"<<"student name\t\t"<<"location";
  43.     }
  44.     else
  45.         ofile1<<st_id<<"\t\t"<<st_name<<"\t\t"<<st_place<<"\n";
  46.     ofile1.close();
  47.  
  48. }
  49. void file::display()
  50. {
  51.     ifstream nfile1;
  52.     nfile1.open("student.txt",ios::in|ios::out);
  53.     if(nfile1.is_open())
  54.     {
  55.             while(getline(nfile1,ch))
  56.             {
  57.  
  58.                 int i,f=ch.find(sword);
  59.                 if(f!=string::npos)
  60.                 {
  61.                     cout<<"line"<<"\t"<<ch<<endl;
  62.  
  63.                 }
  64.                 else
  65.                 {
  66.                     for(i=0;i<=sword.length();i++)
  67.                         sword[i]=sword[i];
  68.                     for(i=0;i<=ch.length();i++)
  69.                         ch[i]=ch[i];
  70.                     if(sword[i]==ch[i])
  71.                     {
  72.                     cout<<"word not found"<<endl;
  73.                     }
  74.                 }
  75.             }
  76.             nfile1.close();
  77.     }
  78.     else
  79.         cout<<"failed to get values";
  80. }
  81. void file::search()
  82. {
  83.     ifstream infile;
  84.     infile.open("student.txt",ios::in|ios::out);
  85.     if(infile.is_open())
  86.     {
  87.         string sword;
  88.         cout<<"\nEnter the search word :";
  89.         cin>>sword;
  90.  
  91.         while(!infile.eof())
  92.         {
  93.             getline(infile,ch);
  94.             int i,pos;
  95.             for(i=0;i<=ch.length();i++)
  96.                 ch[i]=ch[i];
  97.             for(i=0;i<=sword.length();i++)
  98.                 sword[i]=sword[i];
  99.             pos=ch.find(sword);
  100.             if(pos!=string::npos)
  101.             {
  102.                 cout<<ch<<endl;
  103.  
  104.             }
  105.             else
  106.             {
  107.                 cout<<"word not found"<<endl;
  108.             }
  109.         }
  110.         infile.close();
  111.     }
  112.  
  113. }
  114.  
  115. int main()
  116. {
  117.     file obj;
  118.     int a;
  119.     cout<<"\n1.Insertion";
  120.     cout<<"\n2.Display";
  121.     cout<<"\n3.Search";
  122.     cout<<"\nEnter your choice\t";
  123.     cin>>a;
  124.     switch(a)
  125.     {
  126.     case 1:
  127.         obj.getdata();
  128.         obj.putdata();
  129.         break;
  130.     case 2:
  131.         obj.display();
  132.         break;
  133.     case 3:
  134.         obj.search();
  135.     }
  136.     return 0;
  137. }
  138.  
thank you
Aug 21 '13 #6
sorry my mistake(say it and say) try it and say
Aug 21 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Steve | last post by:
My application uses transfertext to create text files that are FTPed to a website and are processed by a webstore. The text files can be for one of three purposes: Add one or more items to the...
5
by: Buddhist[CHinA] | last post by:
The text files are not only the .txt files, but also all ascii files. Thx.
3
by: sena0112 | last post by:
Hi, I'm a beginner in visual basic and programming. My programme needs to let the user browse for a text file and when the user press a command button, the programme will list out the text file into...
2
by: =?Utf-8?B?SWJyYWhpbS4=?= | last post by:
Hello, I have a client c# application from which I want to Add/Edit/Delete & list records by connecting to Web Service. HOw can I write classes & methods in Web service projet that will : ...
3
madhoriya22
by: madhoriya22 | last post by:
I have solved the problem
13
by: ramprakashjava | last post by:
hi, i hav "java.lang.NullPointerException" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz.. ...
1
by: puneetmca | last post by:
hi...this code of deleting a record from database using checkbox is not working...echo "<td>$row</td>; echo "<td>$row</td>"; echo "<td>$row....these lines print as it is...rather then...
0
by: salhana | last post by:
I have problem to solve this. I want to delete all the records in one table. but it always get error says, "run time error '91', object variable or with block variable not set". Please some body...
1
by: Lilly Torres | last post by:
I have a simple form to add, edit, delete & view records, what I want is that when I am viewing the records i can have the oportunity to press the Add Record Button and that the form automatically...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.