gpt4 book ai didi

C++ 数据文件处理

转载 作者:行者123 更新时间:2023-11-28 05:21:03 24 4
gpt4 key购买 nike

我在网上为我的学校项目获得了这段代码,它适用于 Turbo C++,但为什么我在 visual studio enterprise 2015 和 code::blocks 中运行它,它给了我一些未知的输出。我附上了输出,请帮帮我,我试过 _strcmp 但没有解决问题。这是我试图在代码块中运行的代码。

#include <iostream>
#include<string.h>
#include <fstream>
#include <stdio.h>

using namespace std;

class crim_rec
{
char name[20], sex[10], fathr_name[20], addrs[25], offense[20], blood[25], dob[9], reward[50];
int crim_code;
void disp();
public:
void get();
void wtf();
void rff();
void search();
void del();
void mod();
}c;
void crim_rec::get()
{
puts("\nEnter name of criminal:");
gets(name);
puts("\nsex (m/f):");
cin >> sex;
puts("\nEnter date of birth:");
gets(dob);
puts("Enter blood group (Ap/An/Bp/Bn/ABp/ABn/Op/On:");
gets(blood);
puts("\nenter father's name:");
gets(fathr_name);
puts("enter address:");
gets(addrs);
puts("\nEnter crime commited:");
gets(offense);
puts("\nEnter reward on criminal:");
gets(reward);
}
void crim_rec::disp()
{
cout << "The record of criminal:\n";
cout << "\nName of criminal: " << name;
cout << "\nsex: " << sex;
cout << "\nDOB: " << dob;
cout << "\nBlood Group: " << blood;
cout << "\nFather's name: " << fathr_name;
cout << "\nAddress: " << addrs;
cout << "\nCrime: " << offense;
cout << "\nReward: " << reward;

}
void crim_rec::wtf()
{
ofstream ofile;
ofile.open("CBI.txt", ios::app);

get();
ofile.write((char*)&c, sizeof(c));
ofile.close();
}
void crim_rec::rff()
{
ifstream ifile;
ifile.open("CBI.txt");
ifile.seekg(0, ios::beg);
ifile.read((char*)&c, sizeof(c));
while (ifile)
{
disp();
ifile.read((char*)&c, sizeof(c));
}
ifile.close();
}
void crim_rec::search()
{
char m[20];
ifstream ifile("CBI.txt");
puts("Enter name of criminal which has to be searched");
gets(m);
ifile.seekg(0, ios::beg);
ifile.read((char*)&c, sizeof(c));
while (ifile)
{
if (strcmp(m, name) == 0)
disp();
ifile.read((char*)&c, sizeof(c));
}
ifile.close();
}
void crim_rec::del()
{
char b[20];
ifstream ifile;
ifile.open("CBI.txt", ios::app);
ofstream ofile;
ofile.open("new.txt", ios::app);
puts("Enter the name of the criminal whose records you want to del");
gets(b);
ifile.seekg(0, ios::beg);
ifile.read((char*)&c, sizeof(c));
while (ifile)
{
if (strcmp(b, name))
ofile.write((char*)&c, sizeof(c));
ifile.read((char*)&c, sizeof(c));
}
ifile.close();
ofile.close();
remove("CBI.txt");
rename("new.txt", "CBI.txt");
}
void crim_rec::mod()
{
char d[20];
int p;
puts("\nEnter name of criminal whose record you want to modify\n");
gets(d);
fstream f;
f.open("CBI.txt", ios::in | ios::out);
f.seekg(0, ios::beg);
f.read((char*)&c, sizeof(c));
int a = f.tellg();
while (!f.eof())
{
if (!strcmp(d, name))
{
puts("\nPress 1 to change name\nPress 2 to change sex\nPress 3 to change date of birth\nPress 4 to change blood group\nPress 5 to change father's name\nPress 6 to change address\nPress 7 to change crime committed\nPress 8 to change reward on criminal\n");
cin >> p;
switch (p)
{
case 1:
gets(name);
break;
case 2:
cin >> sex;
break;
case 3:
gets(dob);
break;
case 4:
gets(blood);
break;
case 5:
gets(fathr_name);
break;
case 6:
gets(addrs);
break;
case 7:
gets(offense);
break;
case 8:
cin >> reward;
break;
}
f.seekg(a - sizeof(c), ios::beg);
f.write((char*)&c, sizeof(c));
}
f.read((char*)&c, sizeof(c));
a = f.tellg();
}
f.close();
}
int main()
{
int ch;
char choice;
do
{
cout << "\t Central Bureau of Investigation";
cout << "\n ********************************************";
cout << "\n\n * 1. View criminal details *";
cout << "\n\n * 2. Add new criminal details *";
cout << "\n\n * 3. Search a criminal record *";
cout << "\n\n * 4. Delete a criminal record *";
cout << "\n\n * 5. Modify a criminal record *";
cout << "\n ********************************************";
cout << "\n\n Enter your choice: ";
cin >> ch;


switch (ch)
{
case 1:

c.rff();
break;
case 2:

c.wtf();

break;
case 3:
c.search();
break;
case 4:
c.del();
break;
case 5:
c.mod();
break;
default:
{
cout << "\nerror!";
}
break;
}
cout << "\ncontinue? (y/n)\n";
cin >> choice;
} while (choice == 'y');
cout << "\nGood bye";
return 0;
}

image 1 image 2

最佳答案

问题可能是由于使用了 puts() 和 gets()。他们似乎在输入中留下了尾随的换行符。用 std::cout 和 std::cin 分别替换它们为我修复了奇怪的输出(如果我正确理解你的问题)

关于C++ 数据文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41494615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com