presenting here a program fragement of my project
two files mainn.txt mp.txt format i have given at the end of this code
the problem is that while printing mp.txt the output is not in format as i expected

DF: 1
SA: 2-2	       DA: R1	 	 D: 3
SA: R1	 	 DA: R1	 	 D: 2
SA: R1	 	 DA: R1		 D: 1
SA: R1		 DA: 		         D: 0
ID: 3



but is of the form



DF: 1
SA: 2-2	  DA: R1	 	 D: 3
SA: R1	    DA: R1	 	 D: 2
SA: R1	    DA: R1	 D: 1
SA: R1	 DA: 	 D: 0
ID: 3  

i tried using tab and setw but output is not coming the way i expect it to be


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<dos.h>
int countk=0,k=4,probab=1;
char str[20],*p,*q,buf[100],*w,d1[10];
class router
 {
	public:
	   void extract(char *p,char str[])
		{
			int i=0;
			while(p && i<4)
			{
				str[i]=*p;
				p++;i++;
			}
			str[i]=NULL;
		}

	   	
	  void mp_entries(fstream &mp)
		{
		  cout<<setw(20)<<"\n In mp_entries function \n\n";
		  char l[20];
		  int i;
		  p=q=w=NULL;
		  fstream t;
		  t.open("c:\\p\\temp",ios::in|ios::out|ios::trunc);
		  mp.seekg(0);
		  while(!mp.eof())
		  {
			mp.getline(buf,100);
			p=strstr(buf,"SA:");
			if(p)
			{
			++countk;
			p+=4;
			extract(p,str);
			q=strstr(buf,"DA:");
			q=q+4;
			w=strstr(buf,"D:");
			w+=2;
			i=atoi(w);
			++i;
			if(*q<='0')
			{

			   t<<"SA: "<<str<<"\t"<<" DA: R1"<<"\t"<<" D: "<<i<<"\n";
			}
			else
			{
			  extract(q,l);
			  t<<"SA: "<<str<<"\t"<<" DA: "<<l<<"\t"<<" D: "<<i<<"\n";
			}
			  }
			 else
			 {
			  p=strstr(buf,"ID");
			   if(p && probab==1 && countk<k)
			{
			  t<<"SA: R1"<<"\t"<<" DA: "<<"\t"<<" D: 0\n";
			}

			  if(p)
			  {
			t<<"ID: "<<countk<<"\n";

			  }
			  else
			  {
			t<<buf<<"\n";
			  }
		   }
		   }

		  mp.close();
		  t.close();
		  remove("c:\\p\\mp.txt");
		  rename("c:\\p\\temp","c:\\p\\mp.txt");
		  fstream file;
		  file.open("c:\\p\\mp.txt",ios::in|ios::nocreate);
		  while(!file.eof())
			{
				file.getline(buf,100);
				cout<<buf<<endl;
			}
		  file.close();
		 }
	   
void main()
{
 fstream f;
 f.open("c:\\p\\mp.txt",ios::in|ios::out|ios::nocreate);
 clrscr();
 router r;
 r.pkt_type(f);
 f.close();
 getch();
  }

Mainn.txt

DF:0
Uflag:1
Source :2-2
Destination :2-22
Data :sdssdds

Mp.txt

DF: 1
SA: 2-2 DA: R1 D: 1
SA: R1 DA: D: 0
ID: 1

Recommended Answers

All 2 Replies

- The code doesn't compile.
- Who calls whom is not clear.
- mp_entries is never called.
- You open c:\p\temp for reading and writing it's only written to. Is this okay?

Finally I personally have always felt it's much much much easier to use fprintf() for formatted printing rather than stream insertion operator (<<).
Have you already tried fprintf() ?

Member Avatar for GreenDay2001

You have used tabs. Use setw instead of that. That would surely work. Look at thi chunk of code

cout << setw(5)  << "ID"
         << setw(40) << "BOOK NAME"
         << setw(30) << "AUTHOR"
        << endl;
cout << setw(5)  << "232"
         << setw(40) << "ABCDEF"
         << setw(30) << "UVWXYZ";

Something like this only,

not like this

cout << "ID"
         << "\tBOOK NAME"
         << "\tAUTHOR";
cout <<  "\n232"
         <<  "\tABCDEF"
         << "\tUVWXYZ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.