Getchezaa
Getchezaa

Reputation: 1

Write to space separated 2 column text without specifying delimiter in c++

I am using C++ 98. Here is my code. It is getting the text from a lineEdit of a Qt4 form.

strcpy(Name,ui->lineEdit->setText(QString::fromStdString(Name)) );
strcpy(Class,ui->lineEdit_1->setText(QString::fromStdString(Class)));
strcpy(Grade,ui->lineEdit_2->setText(QString::fromStdString(Grade)));

std::ofstream myfile;
myfile.open(mypath,std::fstream::in | std::fstream::out );
myfile<<Name<<"\t"<<"Name"<<"\n";
myfile<<Class<<"\t"<<"Class"<<"\n";
myfile<<Grade<<"\t"<<"Grade"<<"\n";

Here is 2 column space separated text file sample.conf:

AA.    Name
BB     Class
CC     Grade

It is updating the file correctly

My question is there any way to write above text file in without using delimiter like in the above case is \t .

By using other datatype in like FILE or ofstream or some other that don't use delimiter(\t) for writing file ?

That can directly write to file column

Note: My purpose is not restrict file writing to it's delimiter(tab,space,.etc)

Upvotes: 0

Views: 150

Answers (1)

Marcus Hampel
Marcus Hampel

Reputation: 63

You cannot write directly into columns, but you can format your output with IO manipulators:

#include <iomanip>
...
std::cout << std::setw(20) << std::left << "Column 1"
          << std::setw(10) << std::left << "Column 2"
          << '\n';

But beware: std::setw does not truncate any data. If the texts are longer, the column in this row will be longer than specified.

Upvotes: 0

Related Questions