通过c ++生成的文本文件非常不稳定 [英] a text file generated through c++ to be well understable

查看:87
本文介绍了通过c ++生成的文本文件非常不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++编写了一个程序,用于生成快递帐单/发票

该程序从用户处获取详细信息.
将详细信息保存在文件中.
详细信息可以有组织地再次显示

问题是我希望将详细信息保存在其中的文件准确地保存在再次显示的详细信息中.
因此该文件可用于打印

谢谢

I made a program in c++ for courier billing/ invoice generation

The program takes the details from the user.
Saves the details in a file.
the details can be displayed again in an organized manner

The problem is that I want the file in which the details are being saved to the details exactly as they are being displayed again.
So the file can be used for printing

Thanks

//***************************************************************
//                   HEADER FILE USED IN PROJECT
//****************************************************************

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>

//***************************************************************
//                   CLASS USED IN PROJECT
//****************************************************************



class entry
{
	char cn[10];
	char dest[50];
	char ccode[20];
	char date[20];
	int wt,rate,ch,wtf;
  public:
	void create_entry()
	{
		cout<<"\nEntry Details...\n";
		cout<<"\nEnter the date";
		gets(date);
		cout<<"\n\nEnter Client Code";
		gets(ccode);
		cout<<"\n\nEnter Cong Number";
		gets(cn);
		cout<<"\n\nEnter Destination";
		gets(dest);
		cout<<"\n\nEnter Weight";
		cin>>wt;
		cout<<"\n\n\nEntry Completed..";
		cust_entry();
	}

	void show_entry()
	{
		cout<<"\nDate : ";
		puts(date);
		cout<<"\nClient Code : ";
		puts(ccode);
		cout<<"\nDestination : ";
		puts(dest);
		cout<<"\nWeight : "<<wt;
		cout<<"\n\nRate : "<<rate;

	}

	void modify_entry()
	{
		cout<<"\nEnter Cong Number : ";
		gets(cn);
		cout<<"\nModify Client Code : ";
		gets(ccode);
		cout<<"\nModify Destination : ";
		gets(dest);
		cout<<"Modify Date : ";
		gets(date);
		cout<<"\nModify Weight : ";
		cin>>wt;
		cust_entry();
	}

	void cust_entry()
	{
	 if(strcmp(ccode,"AB000")==0)
	 {
	  if(strcmp(dest,"GN")==0 || strcmp(dest,"NOD")==0 || strcmp(dest,"ND")==0 || strcmp(dest,"NCR")==0)
	  {
	   if(wt<=100)
	    rate=20;
	   else if(wt>=101 && wt<=250)
	    rate=30;
	   else if(wt>=251 && wt<=500)
	    rate=100;
	   else
	    {
	     wtf=wt;
	     if(wtf%500==0)
	     {
	      wtf=wtf/500;
	      wtf=wtf-1;
	      rate=(100+(wtf*25));
	     }
	     else
	     {
	     wtf=wtf/500;
	     rate=(100+(wtf*25));
	     }
	    }
	  }
	  else if(strcmp(dest,"NORTH")==0)
	   rate=50;
	  else if(strcmp(dest,"METRO")==0)
	   rate=70;
	  else
	   rate=100;
	 }
	if(strcmp(ccode,"CAS00")==0)
	 {
	  if(strcmp(dest,"GN")==0 || strcmp(dest,"NOD")==0 || strcmp(dest,"ND")==0 || strcmp(dest,"NCR")==0)
	   rate=30;
	  else if(strcmp(dest,"NORTH")==0)
	   rate=60;
	  else if(strcmp(dest,"METRO")==0)
	   rate=80;
	  else
	   rate=110;
	 }

	}
/*
	void amount_entry()
	{

	      if(strcmp(dest,"up")==0)
	       rate=50;
	      else if(strcmp(dest,"nd")==0)
	       rate=20;
	      else
	       rate=100;

	}
	    */
	char* retcn()
	{
		return cn;
	}

	void report()
	{
	 cout<<cn<<setw(15)<<date<<setw(15)<<ccode<<setw(15)<<dest<<setw(10)<<wt<<setw(10)<<rate<<endl;}


	};         //class ends here


//***************************************************************
//    	global declaration for stream object, object
//****************************************************************

fstream fp,fp1;
entry dt ;


//***************************************************************
//    	function to write in file
//****************************************************************

void write_entry()
{
	char ch;
	fp.open("entry.dat",ios::out|ios::app);
	do
	{
		clrscr();
		dt.create_entry();
		fp.write((char*)&dt,sizeof(entry));
		cout<<"\n\nDo you want to add more record..(y/n?)";
		cin>>ch;
	}while(ch=='y'||ch=='Y');
	fp.close();
}


//***************************************************************
//    	function to read specific record from file
//****************************************************************


void display_spb(char n[])
{
	cout<<"\nEntry DETAILS\n";
	int flag=0;
	fp.open("entry.dat",ios::in);
	while(fp.read((char*)&dt,sizeof(entry)))
	{
		if(strcmpi(dt.retcn(),n)==0)
		{
			dt.show_entry();
			flag=1;
		}
	}

	fp.close();
	if(flag==0)
		cout<<"\n\nEntry does not exist";
	getch();
}


//***************************************************************
//    	function to modify record of file
//****************************************************************


void modify_entry()
{
	char n[6];
	int found=0;
	clrscr();
	cout<<"\n\n\tMODIFY Entry REOCORD.... ";
	cout<<"\n\n\tEnter The cong no. of The entry";
	cin>>n;
	fp.open("entry.dat",ios::in|ios::out);
	while(fp.read((char*)&dt,sizeof(entry)) && found==0)
	{
		if(strcmpi(dt.retcn(),n)==0)
		{
			dt.show_entry();
			cout<<"\nEnter The New Details of cong"<<endl;
			dt.modify_entry();
			int pos=-1*sizeof(dt);
			fp.seekp(pos,ios::cur);
			fp.write((char*)&dt,sizeof(entry));
			cout<<"\n\n\t Record Updated";
			found=1;
		}
	}

	fp.close();
	if(found==0)
		cout<<"\n\n Record Not Found ";
	getch();
}


//***************************************************************
//    	function to delete record of file
//****************************************************************


void delete_entry()
{
	char n[6];
	clrscr();
	cout<<"\n\n\n\tDELETE Entry ...";
	cout<<"\n\nEnter The cong no. of the Entry You Want To Delete : ";
	cin>>n;
	fp.open("entry.dat",ios::in|ios::out);
	fstream fp2;
	fp2.open("Temp.dat",ios::out);
	fp.seekg(0,ios::beg);
	while(fp.read((char*)&dt,sizeof(entry)))
	{
		if(strcmpi(dt.retcn(),n)!=0)
		{
			fp2.write((char*)&dt,sizeof(entry));
		}
	}

	fp2.close();
	fp.close();
	remove("entry.dat");
	rename("Temp.dat","entry.dat");
	cout<<"\n\n\tRecord Deleted ..";
	getch();
}


//***************************************************************
//    	function to display Entry list
//****************************************************************

void display_allb()
{
	clrscr();
	fp.open("entry.dat",ios::in);
	if(!fp)
	{
		cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
		getch();
		return;
	}

	cout<<"\n\n\t\tEntry LIST\n\n";
	cout<<"===============================================================================\n";
	cout<<"Cong Number"<<setw(10)<<"Date"<<setw(20)<<"Client Code"<<setw(15)<<"Destination"<<setw(10)<<"Weight"<<setw(10)<<"Rate"<<"\n";
	cout<<"===============================================================================\n";

	while(fp.read((char*)&dt,sizeof(entry)))
	{
		dt.report();
	}
	fp.close();
	getch();
}


//***************************************************************
//    	INTRODUCTION FUNCTION
//****************************************************************

void intro()
{
	clrscr();
	gotoxy(36,11);
	cout<<"COURIER";
	gotoxy(35,14);
	cout<<"MANAGEMENT";
	gotoxy(36,17);
	cout<<"SYSTEM";
	getch();
}



//***************************************************************
//    	ADMINISTRATOR MENU FUNCTION
//****************************************************************

void admin_menu()
{
	clrscr();
	int ch2;
	cout<<"\n\n\n\tADMINISTRATOR MENU";
	cout<<"\n\n\t1.Enter Data ";
	cout<<"\n\n\t2.DISPLAY ALL Entries ";
	cout<<"\n\n\t3.DISPLAY SPECIFIC Entry ";
	cout<<"\n\n\t4.MODIFY Entry ";
	cout<<"\n\n\t5.DELETE Entry ";
	cout<<"\n\n\t6.BACK TO MAIN MENU";
	cout<<"\n\n\tPlease Enter Your Choice (4-6) ";
	cin>>ch2;
	switch(ch2)
	{
		case 1: clrscr();
			write_entry();break;
		case 2: display_allb();break;
		case 3: {
			char num[6];
			clrscr();
			cout<<"\n\n\tPlease Enter The Cong No. ";
			cin>>num;
			display_spb(num);
			break;
			}
		case 4: modify_entry();break;
		case 5: delete_entry	();break;
		case 6: return;
		default:cout<<"\a";
	}
	admin_menu();
}


//***************************************************************
//    	THE MAIN FUNCTION OF PROGRAM
//****************************************************************


void main()
{
	char ch;
	intro();
	do
	{
		clrscr();
		cout<<"\n\n\n\tMAIN MENU";
		cout<<"\n\n\t01. Billing Entry ";
		cout<<"\n\n\t02. Sorting Entry";
		cout<<"\n\n\t03. ADMINISTRATOR MENU";
		cout<<"\n\n\t04. EXIT";
		cout<<"\n\n\tPlease Select Your Option (1-4) ";
		ch=getche();
		switch(ch)
		{
			case '1':clrscr();
				 break;
			case '2':break;
			case '3':admin_menu();
				 break;
			case '4':exit(0);
			default :cout<<"\a";
		}
	}while(ch!='4');
}

//***************************************************************
//    			END OF PROJECT
//***************************************************************

推荐答案

哇-这必须是迄今为止最难以理解的问题.如果您编写了程序,则输出在您的控制之下.如果希望输出看起来不同,请更改代码.如果您有特定问题,请发布代码,并具体说明出了什么问题以及为什么会卡住.
Wow - that has to be the most illegible question, ever. If you wrote the program, then the output is under your control. If you want the output to look different, change your code. If you have a specific question, post the code and explain specifically what''s wrong and why you''re stuck.


我不知道这是否是您的问题,但是我在这里粘贴了一个链接,它说明了文件的使用方式:

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/ [ ^ ]

您可以使用不同的方法来创建文件,如果要以一种可以使用记事本(例如)读取文件的方式来创建文件,则必须使用文本模式.如果您以二进制模式序列化一个完整的结构,那么您将无法轻松读取它.

请查看此链接,如果需要进一步的帮助,请改善您的问题.

祝你好运.
I don''t know if this is your problem or not, but here I''m pasting you a link were it explains the ways files can be used:

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/[^]

you can create the file in different methods, and if you want to create it in a way in which you can use notepad (as an example) to read it then you must use text mode. if you serialize a complete structure in binary mode then you won''t be able to read it easily.

Take a look at this link and improve your question if you need further help.

Good luck.


这篇关于通过c ++生成的文本文件非常不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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