lab 合集 five

File processing

Ex 1:

Suppose we want to simulate accessing a website from a variety of clients.
This problem consists of two parts.

1)Part I:
Randomly generate an IPv4 address (0.0.0.0 ~ 255.255.255.255) and a time-stamp (YYYY-MM-DD HH:MM:SS). Print all records in a file with each individual record occupying a single line. For example:
Contents of record.txt

192.168.2.1 2020-02-02 19:11:20
202.182.22.23 2020-02-12 20:22:13
  …

Note that your time-stamp should be in ascending order. Generate at least 100 records.

2)Part II: Read the record.txt file you created in part I, take the statistics and print on the screen how many addresses each class has:

Number of class A addresses: 12
Number of class B addresses: 15
Number of class C addresses: 23
Number of class D addresses: 28
Number of class E addresses: 22

Roughly, you can classify each address according to the following rules:

Class A:    0.0.0.0- 127.255.255.255
Class B:   128.0.0.0-191.255.255.255
Class C:   192.0.0.0-223.255.255.255
Class D:   224.0.0.0-239.255.255.255
Class E:   240.0.0.0– 247.255.255.255

【我的代码】

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include <windows.h>
using namespace std;
class Client
{
public:
 Client(int a=0,int b=0,int c=0,int d=0,int y=0,int m=0,int dy=0, int h=0,int mi=0,int s=0)
 {
  address[0]=a;
  address[1]=b;
  address[2]=c;
  address[3]=d;
  setYear(y);
  setMonth(m);
  setDay(dy);
  setHour(h);
  setMinute(mi);
  setSecond(s);
 }
 void setYear(int y)
 {
  year=y;
 }
 void setMonth(int m)
 {
  month=m;
 }
 void setDay(int d)
 {
  day=d;
 }
 void setHour(int h)
 {
  hour=h;
 }
 void setMinute(int m)
 {
  minute=m;
 }
 void setSecond(int s)
 {
  second=s;
 }
 int address[4];
 int year,month,day,hour,minute,second;
};
int main()
{
 ofstream outfile("record.txt",ios::out|ios::binary);
 if(!outfile)
 {
  cerr<<"File could not be opened."<<endl;
  exit(1);
 }
 for(int i=99;i>=0;i--)
 { 
  SYSTEMTIME systm;      
  GetLocalTime(&systm);  
  srand(i);
  Client clientData(rand()%255,rand()%255,rand()%255,rand()%255,systm.wYear,systm.wMonth,systm.wDay,systm.wHour,systm.wMinute,systm.wSecond-i+39);
  if(clientData.second<0)
  {
  clientData.setMinute(systm.wMinute-1);
  clientData.setSecond(clientData.second+60);
  }
  outfile.write(reinterpret_cast< const char* >( &clientData ),//纠错:const char *
  sizeof(Client));
  cout<<clientData.address[0]<<"."<<clientData.address[1]
   <<"."<<clientData.address[2]<<"."<<clientData.address[3]
   <<" "<<clientData.year<<"-"<<setw(2)<<setfill('0')<<clientData.month<<"-"<<setw(2)<<clientData.day
   <<" "<<setw(2)<<clientData.hour<<":"<<setw(2)<<clientData.minute<<":"<<setw(2)<<clientData.second<<endl;
 }
 outfile.close();
 ifstream infile("record.txt",ios::in|ios::binary);
 Client clientData;
 infile.read(reinterpret_cast<char *>(&clientData),sizeof(Client));
 int a=0,b=0,c=0,d=0,e=0;
 while(infile&&!infile.eof())
 {
  if(clientData.address[0]>=0&&clientData.address[0]<=127)a++;
  if(clientData.address[0]>=128&&clientData.address[0]<=191)b++;
  if(clientData.address[0]>=192&&clientData.address[0]<=223)c++;
  if(clientData.address[0]>=224&&clientData.address[0]<=239)d++;
  if(clientData.address[0]>=240&&clientData.address[0]<=247)e++;
  infile.read(reinterpret_cast<char *>(&clientData),sizeof(Client));
 }
 cout<<"Number of class A addresses:"<<a<<endl
  <<"Number of class B addresses:"<<b<<endl
  <<"Number of class C addresses:"<<c<<endl
  <<"Number of class D addresses:"<<d<<endl
  <<"Number of class E addresses:"<<e<<endl;
 system("pause");
 return 0;
}	
犯的错误:
 谁会料想infile一直无法为空,
 是我文件名打错成 .text, 是 .txt啊,aaaaa,太马大哈了

Ex 2:

Create a simple random-access file-processing program that might be used by professors to help manage their student records.
For each student, the program should obtain an ID number, the student’s first name, the student’s last name and the student’s grade.
The data obtained for each student constitutes a record for the student and should be stored in an object of a class called Student.
The program should save the records in a binary file specified by the user (for example “file.dat”).

The program should also be able to:
(1) Display all records (together with students’ average score)
(2) Add/delete the record
(3) Edit each record (i.e. change the ID, name and/or grade of each record)
【我的代码】

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
class Student
{
public:
 Student(int num=0,char* f="",char* l="",double g=0.0)
 {
  setidNumber(num);
  setFirstName(f);
  setLastName(l);
  setGrade(g);
 }
 void setidNumber(int num)
 {
  idNumber=num;
 }
 void setFirstName( char* f)
 {
  strcpy(firstName,f);
 }
 void setLastName(char* l)
 {
  strcpy(lastName,l);
 }
 void setGrade(double g)
 {
  grade=g;
 }
 int idNumber;
 char lastName[15];
 char firstName[10];
 double grade;
};
void outputLine( ostream &output, const Student &record )
{
 output << left << setw( 10 ) << record.idNumber 
  << setw( 16 ) << record.lastName<< setw( 11 ) << record.firstName
  << setw( 10 ) << setprecision( 2 ) << right << fixed 
  << showpoint << record.grade << endl;
}
int main()
{
 int idNumber;
 char lastName[15];
 char firstName[10];
 double grade;
 double averageScore;//students’ average score
 ofstream outFile( "file.dat",ios::out | ios::binary );
 if (!outFile)
 {
  cerr << "File could not be opened." << endl;
  exit(1);
 }

cout << "Enter student's ID number to add the record(0 to end input)\n? ";
 Student onestudent;
 cin>>idNumber;
 int i=0;
 double score=0;
 while (idNumber>0)
 {
  i++;
  cout << "Enter lastname, firstname, grade\n? ";
  cin >> setw(15) >> lastName;
  cin >> setw(10) >> firstName;
  cin >> grade;
  score+=grade;
  onestudent.setidNumber( idNumber );
  onestudent.setLastName( lastName );
  onestudent.setFirstName( firstName );
  onestudent.setGrade( grade );
  outFile.seekp((onestudent.idNumber-1 ) *sizeof(Student));
  outFile.write( reinterpret_cast< const char * >(&onestudent),sizeof( Student ) );
  
  cout << "Enter student's ID number(0 to end input)\n? ";
  cin >>idNumber;
 } 
 averageScore=score/i;
 outFile.flush();//关键

ifstream inFile( "file.dat",ios::in|ios::binary );
 Student onestudent1;
 cout << "\nEnter student's ID number to edit the record(0 to end input)\n? ";
 cin>>idNumber;
 int k=0;
 while ( idNumber > 0)
 {
  cout << "Which one do you want to edit?\n1:ID number\n2:lastname\n3:firstname\n4:grade\n? ";
  cin>>k;
  inFile.seekg((idNumber-1)*sizeof(Student));
  inFile.read( reinterpret_cast< char * >(&onestudent1),sizeof( Student ) );
  cout<<onestudent1.grade<<endl;
  switch(k)
  {
  case 1:cin>>idNumber;onestudent1.setidNumber(idNumber);break;
  case 2:cin>>setw(15)>>lastName;onestudent1.setLastName(lastName);break;
  case 3:cin>>setw(10)>>firstName;onestudent1.setFirstName(firstName);break;
  case 4:cin>>grade;score=score-onestudent1.grade+grade;onestudent1.setGrade(grade );break;
  default:cout<<"wrong edit"<<endl;break;
  }
  outFile.seekp((idNumber-1) *sizeof(Student));
  outFile.write( reinterpret_cast< const char * >(&onestudent1),sizeof(Student));
  cout << "Enter student's ID number to edit the record(0 to end input)\n? ";
  cin >>idNumber;
 } 
 averageScore=score/i;
 outFile.flush();//关键

cout << "\nEnter student's ID number to delete the record(0 to end input)\n? ";
 cin>>idNumber;
 int j=0;
 while ( idNumber>0)
 {
  j++;
  inFile.seekg((idNumber-1)*sizeof(Student));
  inFile.read(reinterpret_cast<char *>(&onestudent1),sizeof(Student));
  cout<<onestudent1.grade<<endl;
  score-=onestudent1.grade;
  cout<<score<<endl;
  onestudent1.setidNumber(0);
  onestudent1.setLastName("");
  onestudent1.setFirstName("");
  onestudent1.setGrade(-1);
  outFile.seekp((idNumber-1)*sizeof(Student));
  outFile.write(reinterpret_cast<const char *>(&onestudent1),sizeof(Student));
  cout << "Enter student's ID number to delete the record(0 to end input)\n? ";
  cin >>idNumber;
 } 
 averageScore=score/(i-j);
 outFile.flush();//关键

cout <<left<<setw(10)<<"ID number"<<setw(16)<<"Last Name"<<setw(11)<<"First Name"<<left<< setw(10)<<right<<"Grade"<< endl;
 Student onestudent2; 
 inFile.seekg(0);
 inFile.read( reinterpret_cast< char* >( &onestudent2 ),sizeof(Student) );
 while( inFile && !inFile.eof()) 
 {
  if( onestudent2.idNumber!= 0)
   outputLine(cout,onestudent2);
  inFile.read( reinterpret_cast<char *>(&onestudent2),sizeof( Student ) );
 } 
 cout<<"Students' average score is: "<<averageScore<<endl;
 system("pause");
 return 0;
}
犯的错误:
. flush()函数的使用,很关键,很关键,很关键!!!

首先,得明确write方法并不直接将数据写入文件,
而是先写入内存中特定的缓冲区。
正常情况下缓冲区满时,
或者进行下一步操作(如write)的时候,
操作系统才会自动将缓冲数据写入到文件中。

因此,在add record ,edit record,delete record后
都得用flush()函数,刷新缓冲区,
即将缓冲区中的数据立刻写入文件,同时清空缓冲区。
这样每一个block操作后才能正确写进数据!!!

下午出去嗨了一下,在堆积一堆ddl时还如此嚣张,不过真的玩得特别开心,晚上回来就结束一下bolg,干不动ddl了。。。
总算补完blog了 !hhhh,累了累了。。。
OK,晚安,wish up up up!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值