3

i have created one class of doctor and i want to insert data of doctor in note pad file (Doctor.txt) but data do not have a symmetry because there is no fixed length of my inputs in the line...

        int DocID;
        string Name,Department,Specification,Address,PhNo ;
        Doctor D;
        ofstream myfile;
        myfile.open ("Doctor.txt",ios::app);
          cout<<"please enter doctor ID: ";
          cin>> DocID;
          cout<<"please enter doctor Name: ";
          cin>> Name;
          cout<<"please enter doctor Department name: ";
          cin>> Department;
          cout<<"please enter doctor specification: ";
          cin>> Specification;
          cout<<"please enter doctor Address: ";
          cin>> Address;
          cout<<"please enter doctor Phone Number: ";
          cin>> PhNo;

          D.set_data(DocID,Name,Department,Specification,Address,PhNo);
          myfile <<endl<<D.get_DocID()<<"  "<<D.get_Name()<<" "<<D.get_Department()<<" "<<D.get_Specification()<<" "<<D.get_Address()<<" "<<D.get_PhNo();
          myfile.close();

how can i generate a symmetrical .txt file using fixed width of columns of inputs

Anwar Ali
  • 31
  • 2
  • 1
    If you Doctor.txt file is long, than it may not be a good practice to read each entry in it, reformat entry and write it again. So, if I were you my main problem wouldn't be fixed-length entries. It would be "what if the doctor name has a space in his/her name?" because you use space to separate variables. That causes confusion. – jnbrq -Canberk Sönmez Sep 30 '15 at 10:28
  • "Use basic maths" comes to mind .. though you may also wish to look into I/O Manipulators. jnbrq has a point, though - you probably ought to render proper, well-formed CSV instead if you want your format to be of any actual practical use. – Lightness Races in Orbit Sep 30 '15 at 10:39
  • @jnbrq-CanberkSönmez, how the spaces would be handled?.. I think it would be better to introduce "-" in place of spaces between the entries. – Awais Mahmood Sep 30 '15 at 10:43
  • You might use . see [this](http://stackoverflow.com/questions/2436004/how-do-i-correctly-organize-output-into-columns). It already addresses your problem. – Awais Mahmood Sep 30 '15 at 10:45
  • @AwaisMahmood There are some non-printable characters, such as "\a". Those character will be possibly not given for any kind of data field. So you may consider using them instead of space. – jnbrq -Canberk Sönmez Sep 30 '15 at 11:07

1 Answers1

1

You might be looking for setw width manipulator function provided by <iomanip> header file and using it before or after or however you want to write each entry to the file.

cpx
  • 17,009
  • 20
  • 87
  • 142