hi all,
I'm new to the linux programming first of all.
I just wrote this program.
#include <fcntl.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std ;
int main(int argc,char **argv)
{
/* open the file */
string _path_name = "./file";
string _return ;
int fd=open(_path_name.c_str() ,O_RDONLY);
/* buffer to read the file */
char * buffer=new char [100];
buffer[99]=0;
int n=99;
while (n==99)
{
n=read(fd,buffer,99);
if (n<99)
{
buffer[n]=0;
}
/* append them to the string _return */
_return += (char*)buffer;
}
cout << _return ;
/* close the file */
close(fd);
/* unallocate the memory */
delete *buffer;
return 0;
}
and when I create the buffer on the stack using this.
char buffer[100];
Then it won't work properly. I mean that
_return += (char*) buffer;
statement will pass the null character 0 and print the garbage things even in the
buffer. What is the wrong with that?
But when I use the `new char[100]` it works nicely and end attaching when the 0 (null character ) found.What is the reason? can anybody explain this to me?
Any idea?
--Thanks in advance--
sanudn.