Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding blanks to a string

Status
Not open for further replies.

Bickle

Programmer
Jul 12, 2001
22
0
0
FR

Greetings,

I need to compelete a string with(n) number of blanks that is defined in the following way...

char line[500];

while (f_in.getline(line, sizeof(line)))
f_out << line << endl;;


If the line that is read from a file is only 50 characters, I am creating a new file with a total of 500 characters, so I have to add 450 blanks to the variable char << line << .

Thank you for help

Adrian
 
I am not sure what you are asking...

I believe the getline adds a null terminator so the following will need to be done

int len = strlen(line);
memset(line+len,' ',500-len);
line[499]=0;

if there is no null termination, then this should work

memset(line,' ',500);
line[499]=0;
// call get line

Please check my math... i may be off by one.

On the other hand, if you dont want the blanks...
memset(line,0,500) will do the trick for you

Matt
 

To Clarify : I am reading a file and writing it again filled out with n number of spaces (passed into the programme as an argument.)

fstream f_in;
f_in.open(argv[1], ios::in);

fstream f_out;
f_out.open(argv[2], ios::eek:ut);


int i = atoi(argv[3]);

char line;
while (f_in.getline(line, sizeof(line)))
f_out << line << endl;;

the variable line needs to be padded out with &quot;i&quot; number of spaces...

Thanks Adrian
 
what about something like:

fstream f_in;
f_in.open(argv[1], ios::in);

fstream f_out;
f_out.open(argv[2], ios::eek:ut);


int i = atoi(argv[3]);

char line;
string strLine;

while (f_in.getline(line, sizeof(line)))
{
strLine.assign(line,f_in.gcount());

if( f_in.gcount() < i )
// append enough blanks to bring the line to proper length
strLine.append(i-f_in.gcount(),' ');

f_out << line << endl;;
}

(my changes in bold)

I didn't compile this for syntax, but it should be pretty close.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top