如何在C ++中的指定位置添加新记录 [英] How can I add a new record on a specified position in C++

查看:92
本文介绍了如何在C ++中的指定位置添加新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我需要你的帮助!

这里是我写的代码。这是一个有点数据库,我们可以存储人的姓名和电话号码。我尝试进行4次操作(按名称搜索,按编号搜索,删除并将记录添加到特定位置)。前三个正在工作,但我正在努力修复案例4,目前,这是不正常工作。



我尝试过:



Hello folks! I need your help!
here the code I have written. This is kinda database, where we can store person's name and telephone number. I tried to do 4 operations(searching by name, searching by number, deleting and adding a record to a specific position). First three of them are working but I am struggling to fix case 4, which is, at the moment, not working properly.

What I have tried:

    #include<iostream>
    #include<fstream>
    #include<string> 
    #include<stdlib.h>
    using namespace std;
    class Person
    {
	long int tel_number;
	char name[20];
    public:
	void setdata()
	{
		cout << endl << "enter the name and the number\n";
		cin >> name >> tel_number;
	}
	void getdata()
	{
		cout << "Name: " << name << "\nNumber: " << tel_number << endl;
	}
	long int ret_num()
	{
		return tel_number;
	}
	char* ret_name()
	{
		return name;
	}
    };
    int main()

    {
	Person p;
	fstream file;
	int n = 1; char num_by_name[10]; bool isTrue = true; long int name_by_num;
	file.open("Person.dat", ios::trunc | ios::in | ios::out | ios::ate);
	while (n == 1)
	{
		p.setdata();
		file.write((char*)&p, sizeof(p));
		cout << "Wanna enter another person?\n1. Yes\n2. No\n";
		cin >> n;
	}
	file.seekg(0);
	cout << "\nAll entries:\n";
	while (file.read((char *)&p, sizeof(p)))
	{
		p.getdata();
	}

	while (isTrue)
	{

		cout << "\n\n\n1.Determine the telephone number of the specified person\n";
		cout << "2.Determine the name if telephone number is known\n";
		cout << "3.Delete a record\n";
		cout << "4.Add a record to a specific position\n";
		cout << "5.Exit\n";
		cin >> n;
		file.seekg(0);
		file.clear();
	start:
		switch (n)
		{
		case 1:
		{
				 //some code
		}
		case 2:
		{
				 //some code
		}
		case 3:
		{
				 //some code
		}
		case 4:
		{

				  cout << "enter the position to which u want to add a record\n";
				  fstream fin("temp.dat");
				  fin.seekg(0);
				  fin.clear();
				  while (fin.read((char*)&p, sizeof(p)))
				  {
					  fin.seekg(0, ios::end);
					  int endposition = fin.tellg();
					  int b = endposition / sizeof(Person);
					  cin >> b;
					  int position = (b - 1)*sizeof(Person);
					  fin.seekg(position);
					  while (fin.read((char*)&p, sizeof(p)))
					  {
						  p.setdata();
						  fin.write((char*)&p, sizeof(p));
					  }
					  cout << endl;
				  }
				  cout << "\nAll entries:\n";
				  while (fin.read((char*)&p, sizeof(p)))
				  {
					  p.getdata();
				  }
				  break;
			}
		case 5:
		{
				  exit(0);
				  break;
		}
		default:
		{
				   cout << "plz enter proper value\n";
				   goto start;
				   break;
		}
		}
	}

	system("pause");
	return 0;
}

推荐答案

如果你想这样做,请使用必须移动(复制)从插入索引开始的所有元素。最好是从最后或最后一个免费索引开始。



如果你被允许,你应该使用一些为这些任务提供的类,并使编码变得容易。这是向量类,其中包含 insert 函数,可以将对象插入所需的索引。请参阅提供的示例代码。
If you want to do it, use must move (copy) all elements beginning at the insertion index. Best is to start at the end or the last free index.

If you are allowed you should use some class which is provided for such tasks and makes coding easy. That is the class vector which has the insert function which can insert an object at an desired index. See the provided sample code.


这篇关于如何在C ++中的指定位置添加新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆