C++ 数据文件处理
C++ Data file handling
我在网上为我的学校项目获得了这段代码,它在 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 替换它们为我修复了奇怪的输出(如果我正确理解你的问题)
我在网上为我的学校项目获得了这段代码,它在 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 替换它们为我修复了奇怪的输出(如果我正确理解你的问题)