1

I am a beginner in C++. I have input the values of the variables(belonging to a class) through an array of objects. Now how do I write them to the text file column wise as follows?? Thanks.........

SLNO    NAME      ADDRESS        PHONE NO     TYPE      
   1.   ABC       xyzagsgshsh    27438927     Mobile
   2.   QWE       qwhjbbdh       78982338     Landline

This is my code for storing the data. How do I make it into a text file with contents as below?

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class emp
{
    string name,address,phone,type;
    public:
    void getdata();
}obj[5];

void emp::getdata()
{
    cout<<"\nEnter the details:";
    cout<<"\nName: ";cin>>name;
    cout<<"Address:";
    cin>>address;
    cout<<"Phone number: "; cin>>phone;
    cout<<"\nType of phone? (Landline/Mobile) :";
    cin>>type;
}

int main()
{
    ofstream ptr;
    ptr.open("Phone.dat",ios::out);
    cout<<"\nEnter the no.of.records: ";
    int n,i;
    cin>>n;
    for(i=0;i<n;i++)
    {
        obj[i].getdata();
        ptr.write((char*)&obj[i],sizeof(obj[i]));
    }
    return 0;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
User111213
  • 168
  • 1
  • 4
  • 11
  • 1
    If you've input these values then you've done the hard bit so I'm a bit puzzled why you're stuck on the output. Can you show the code you've written so far, or explain what it is that you're stuck on. Your question is a bit vague as it is. – john Mar 28 '13 at 11:37
  • 1
    Start by learning the formatted output capabilities of the standard library. You can find out all about them on [this website](http://en.cppreference.com/w/cpp/io), or lookup "C++ formatted output" on google. – WhozCraig Mar 28 '13 at 11:39
  • @john: The code is as above. I need to make the Phone.dat file into a text file as mentioned. – User111213 Mar 28 '13 at 11:59
  • @Razius Well you don't use `write`, which is for binary data, just `ptr << obj[i].name << '' obj[i].address << '\n';` etc. is a start. Use `ptr` just like you'd use `cout`. Since you already know how to use `cout` you already know how to write to a file. – john Mar 28 '13 at 13:07

3 Answers3

2

Since you already created a filestream, you can take advantage of the output flags (std::left, std::right and std::setw):

http://www.cplusplus.com/reference/iomanip/setw/

http://www.cplusplus.com/reference/ios/left/

Now, in order to ensure that any string stored in any object of the emp class doesn't exceed the size that you allocate to the ofstream/ostream through std::setw, you can use string::resize.

Ruben Cordeiro
  • 322
  • 3
  • 6
0

you could use a string/file stream, newlines, and std::setw

ofstream myfile;
myfile.open ("example.txt");
myfile << "SLNO" << std::setw(10) << "NAME" << std::setw(10) << "ADDRESS" << std::setw(10) << "PHONE NO" << std::setw(10) << "TYPE\n";

this will space all the text with 10-text lenght spaces and put it into example.txt

remember to check the file for validity and close the file.

  • Note that by default, `ostream` right justifies. (This is generally what is wanted for numeric values.) Because of this, it's often easier to use `resize` directly on the strings you're outputting. – James Kanze Mar 28 '13 at 11:41
  • but will the values I enter be stored to the text file under the right columns? – User111213 Mar 28 '13 at 12:00
  • if you enter the amount of spaces (just make sure your column text is never going to be of greater lenght than setw(x), where x is the amount of total characters+spaces your column can have), yes. –  Mar 28 '13 at 12:25
  • for example if you have setw(10) before a column but the text you enter in it is 12 characters long, the whole line will be misaligned. –  Mar 28 '13 at 12:26
0

It depends on the context. For simple output to a console window (or if you have a fixed width font elsewhere, but that's rather rare), you can use the std::setw before each element, specifying the width of the field. For text (std::string), however, it's often easier to just use resize, and make it the correct size to begin with.

James Kanze
  • 150,581
  • 18
  • 184
  • 329