10 Replies - 26432 Views - Last Post: 27 February 2010 - 07:28 AM Rate Topic: -----

#1 khan290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-February 10

input data,delete and update a file

Posted 27 February 2010 - 02:01 AM

Develop a program that can store students’ data (ID, name, GPA) about students in a file. The program user can add, update, delete and search a student. Use a function to implement each such operation. The user can also ask the program to import data from some other file (deleting the existing data if any). Following is a sample execution of this program:

1. Add a record

2. Update a record



Select an option: 1

ID: 1234

Name: Ahsan Jameel

GPA: 3.25

Record added

i have done but its not working :(

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{

	int k=0;
	string line;
	string find;
	char name[25];
	int id=0;
	float gpa=0;

	ofstream myfile;
    myfile.open("data.txt");
	
	while(k!=3){

	cout<<"press 1 for adding data"<<endl;
	cout<<"press 2 for update "<<endl;


	cin>>k;

	if(k==1)
	{

	

		cout<<"enter ID "<<endl;
		cin>>id;

		cout<<"enter Name"<<endl;
		cin>>name;

		cout<<"enter GPA "<<endl;
		cin>>gpa;

		myfile<<name<<endl;
		myfile<<id<<endl;
		myfile<<gpa<<endl<<endl<<endl;


	}


	

	if(k==2)
	{



		cout<<"name u want to update "<<endl;
		cin>>find;

		ifstream file;

		file.open("data.txt");

		
		while (!file.eof() && line!=find)
		{
		
            getline(file,line);
		}


     	cout<<"enter ID "<<endl;
		cin>>id;

		cout<<"enter Name"<<endl;
		cin>>name;

		cout<<"enter GPA "<<endl;
		cin>>gpa;

		myfile<<name<<endl;
		myfile<<id<<endl;
		myfile<<gpa<<endl<<endl<<endl;

      

	}
	

	if(k==3){

	myfile.close();
	}
	}

	return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: input data,delete and update a file

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: input data,delete and update a file

Posted 27 February 2010 - 02:10 AM

What is the problem? what did you expect and is not working? Specify the details to get good help :)
Edit:* And also use proper indenting so that it will be much easier to follow the code.

This post has been edited by Anarion: 27 February 2010 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 khan290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-February 10

Re: input data,delete and update a file

Posted 27 February 2010 - 02:25 AM

View PostAnarion, on 27 February 2010 - 01:10 AM, said:

What is the problem? what did you expect and is not working? Specify the details to get good help :)
Edit:* And also use proper indenting so that it will be much easier to follow the code.


i am new here.. i want to make code which can take input in a file and can update too. first i enter data in a file and then select 2 to update . but it does not overwrite the specific data .
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: input data,delete and update a file

Posted 27 February 2010 - 02:43 AM

View Postkhan290, on 27 February 2010 - 11:55 AM, said:

i am new here.. i want to make code which can take input in a file and can update too. first i enter data in a file and then select 2 to update . but it does not overwrite the specific data .

OK, here's an example of updating a file:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    fstream file("test", ios::out); //open for output
    file<<"dummy text output"; //write this to file stream
    file.seekp(6, ios::beg); //move the put cursor to start of "text"
    file<<"program for files"<<endl;
    file.close();
    return 0;
}

seekp is used to change the position of the write cursor. So it is useful for updating files. Updating a file, like in database for example, depends on the file structure you have. But remember that any extra characters not updated (and are after the part you updated), remain unchanged.

This post has been edited by Anarion: 27 February 2010 - 02:45 AM

Was This Post Helpful? 1
  • +
  • -

#5 khan290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-February 10

Re: input data,delete and update a file

Posted 27 February 2010 - 02:56 AM

View PostAnarion, on 27 February 2010 - 01:43 AM, said:

View Postkhan290, on 27 February 2010 - 11:55 AM, said:

i am new here.. i want to make code which can take input in a file and can update too. first i enter data in a file and then select 2 to update . but it does not overwrite the specific data .

OK, here's an example of updating a file:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    fstream file("test", ios::out); //open for output
    file<<"dummy text output"; //write this to file stream
    file.seekp(6, ios::beg); //move the put cursor to start of "text"
    file<<"program for files"<<endl;
    file.close();
    return 0;
}

seekp is used to change the position of the write cursor. So it is useful for updating files. Updating a file, like in database for example, depends on the file structure you have. But remember that any extra characters not updated (and are after the part you updated), remain unchanged.


thanx sir
but i am at beginner level so cant understand these terms. i am in second semester. can u tell in more simplified form , if u can . it would help me more.
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: input data,delete and update a file

Posted 27 February 2010 - 03:02 AM

Everything you need to know about seekp() is here: http://www.cplusplus.../ostream/seekp/
Have fun!
Was This Post Helpful? 1
  • +
  • -

#7 khan290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-February 10

Re: input data,delete and update a file

Posted 27 February 2010 - 03:23 AM

View Postsarmanu, on 27 February 2010 - 02:02 AM, said:

Everything you need to know about seekp() is here: http://www.cplusplus.../ostream/seekp/
Have fun!

thnx
can we take seekp() with our own wish to any line. or it just move in the beginning of file ???
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: input data,delete and update a file

Posted 27 February 2010 - 03:29 AM

View Postkhan290, on 27 February 2010 - 12:23 PM, said:

View Postsarmanu, on 27 February 2010 - 02:02 AM, said:

Everything you need to know about seekp() is here: http://www.cplusplus.../ostream/seekp/
Have fun!

thnx
can we take seekp() with our own wish to any line. or it just move in the beginning of file ???

Of course. You just have to specify the position ...
Was This Post Helpful? 0
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: input data,delete and update a file

Posted 27 February 2010 - 03:36 AM

There are two "predefined" methods that you may want to know:
myfile.seekg(0, ios::end); // advances the cursor at the end of the file


and
myfile.seekg(0, ios::beg); // put the cursor at the beginning of the file


The first one is generally used when you want to know the size of the file.
Was This Post Helpful? 1
  • +
  • -

#10 khan290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-February 10

Re: input data,delete and update a file

Posted 27 February 2010 - 03:37 AM

View Postsarmanu, on 27 February 2010 - 02:29 AM, said:

View Postkhan290, on 27 February 2010 - 12:23 PM, said:

View Postsarmanu, on 27 February 2010 - 02:02 AM, said:

Everything you need to know about seekp() is here: http://www.cplusplus.../ostream/seekp/
Have fun!

thnx
can we take seekp() with our own wish to any line. or it just move in the beginning of file ???

Of course. You just have to specify the position ...

look at my code after loop of string matching i want to switch to that position of matching and overwrite thae data .
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{

	int k=0;
	string line;
	string find;
	char name[25];
	int id=0;
	float gpa=0;

	ofstream myfile;
	ofstream mfile;
    myfile.open("data.txt");
	mfile.open("output.txt");
	
	
	while(k!=3){

	cout<<"press 1 for adding data"<<endl;
	cout<<"press 2 for update "<<endl;

    int count=0;

	cin>>k;

	if(k==1)
	{

	

		cout<<"enter ID "<<endl;
		cin>>id;

		cout<<"enter Name"<<endl;
		cin>>name;

		cout<<"enter GPA "<<endl;
		cin>>gpa;

		myfile<<name<<endl;
		myfile<<id<<endl;
		myfile<<gpa<<endl<<endl<<endl;

	


	}


	

	if(k==2)
	{

        long pos;
        
		cout<<"name u want to update "<<endl;
		cin>>find;

		ifstream file;

		file.open("data.txt");

		
		while (!file.eof() && line!=find)
		{
		
            getline(file,line);
			count++;
		}

   pos=count;
		myfile.seekp(pos);


     	cout<<"enter ID "<<endl;
		cin>>id;

		cout<<"enter Name"<<endl;
		cin>>name;

		cout<<"enter GPA "<<endl;
		cin>>gpa;

		myfile<<name<<endl;
		myfile<<id<<endl;
		myfile<<gpa<<endl<<endl<<endl;

      

	}
	

	if(k==3){

	myfile.close();
	}
	}

	return 0;

}

Was This Post Helpful? 0
  • +
  • -

#11 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: input data,delete and update a file

Posted 27 February 2010 - 07:28 AM

First of all, let me correct you about the proper file input. Use getline itself to check if there are more lines or not. The explanation is [ Here ] (thanks to Bench :) )
OK, you can use tellg to find out where the get cursor is located. So, you start reading through the file, when you reach the desired part, stop the reading loop and use file.tellg() to get the position. Then, start overwriting data to update that part.

And here's a dummy program I wrote to test:
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
using namespace std;

int main() {
    fstream file("test", ios::in); //input file
    if(!file.is_open()) //test to see if it has been opened properly
        return 0;
    string answer, name, line;
    cout<<"What name do you want to update? ";
    getline(cin, answer);
    cout<<"What's the new name? ";
    getline(cin, name);
    /* --- */
    deque<string> deq; //double ended queue to store strings
    cout<<"Iterating through the file..."<<endl;
    while( getline(file, line) ) { //while there is data to read
        if(line != answer)
            deq.push_back(line); //add this line to the queue
        else
            deq.push_back(name); //the current line was the update-needing line, so add the new name instead
    }
    file.close(); //close the previously opened file
    file.open("test-update", ios::out); //open output file
    if(!file.is_open()) return 0; //test for open file
    while(!deq.empty()) { //while the queue is not empty...
        file<<deq.front()<<endl; //output the first element of queue
        deq.pop_front(); //delete the first element of queue
    }
    /* --- */
    file.close();
    return 0;
}


Probably not the best method, but it is easy to follow. :)
*Edit* Just note that, if you update the contents using seekp and stuff (overwriting), you have to correct the remaining contents of the file, because there may still be some characters of the previous name not updated with the new characters. To eliminate this, I chose to rewrite the whole file with new contents. But if you want to have a serious program and you need the best performance with heavy files, consider a better way.

Code Updated: changed those 2 cin>> to getline

This post has been edited by Anarion: 27 February 2010 - 07:36 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1