By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

ending null character

P: n/a
las
code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char. I thought getline was
suppse to automatically add a null terminating char, hence the whole word
should be printed.

also tryed using get

for(int index=0; index<5; index++)
{
programs[index]=new char[21];
data.get(programs[lp], 21 ,' ');
data.ignore(1,' '); //remove space
// how do I add null terminating char to programs[lp] ?

}

Thanks for any help.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
las escribió:
code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.


program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.

Regards.
Jul 19 '05 #2

P: n/a
>> code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.


program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.


Or even better, get rid of the char pointer misery altogether:

string programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
getline(file, programs[index], ' ');
cout << programs[index] << endl;
}

The advantages: less code, more robust, no arbitrary string length
limitations and no worries about freeing the memory.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #3

P: n/a

Peter van Merkerk <me*****@deadspam.com> wrote in message
news:bg************@ID-133164.news.uni-berlin.de...
code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.
program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.


Or even better, get rid of the char pointer misery altogether:

string programs[5];


And better yet, also remove the array misery...

vector<string> programs(5);

-Mike
ifstream file("...");

for(int index=0;index<5;index++)
for(vector::size_type index = 0; index < programs.size(); ++i)
{
getline(file, programs[index], ' ');
cout << programs[index] << endl;
}

The advantages: less code, more robust, no arbitrary string length
limitations and no worries about freeing the memory.


And now no arbitrary array limitations. :-)

-Mike

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.