* Re: Problem with fread
[not found] <2B721C6525F0D411B1E900B0D0226BDD02AAE0AC@mohmsg01.ad.infos ys.com>
@ 2003-05-20 13:23 ` Eljay Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2003-05-20 13:23 UTC (permalink / raw)
To: Ajay Bansal, gcc-help
Hi Ajay,
fread does not append a '\0' (null character) at the end of a buffer.
Regarding your test_fread.cpp attachment...
You shouldn't use #include <string.h>, you should use #include <cstring>.
You shouldn't use #include <stdlib.h>, you should use #include <cstdlib>.
You shouldn't use #include <errno.h>, you should use #include <cerrno>.
I strongly urge you to consider using string objects and the #include
<string> header, instead of fixed character buffers.
I strongly urge you to use fstreams and #include <fstream>, instead of
FILE, fopen, stat, et al.
I strongly urge you to use C++ new/delete instead of C
malloc/calloc/realloc/free.
I'll send you my refactoring of your test_fread.cpp shortly. (Not to the
forum.)
Sincerely,
--Eljay
^ permalink raw reply [flat|nested] 2+ messages in thread
* Problem with fread
@ 2003-05-20 13:15 Ajay Bansal
0 siblings, 0 replies; 2+ messages in thread
From: Ajay Bansal @ 2003-05-20 13:15 UTC (permalink / raw)
To: gcc-help
[-- Attachment #1.1: Type: text/plain, Size: 438 bytes --]
Hi All
We are using fread to read the entire contents of the file in a buffer.
Does fread append "\0" at the end of the buffer or we have to do it
manually?
Attached is a sample program. We are using the test function twice.
Second time when we call the fucntion, give file has a smaller size then
what was given at the first call.
Second time, output is gargled towards end.
any idea???
Regds
Ajay Bansal
[-- Attachment #1.2: Type: text/html, Size: 1572 bytes --]
[-- Attachment #2: test_fread.cpp --]
[-- Type: application/octet-stream, Size: 1884 bytes --]
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
using namespace std;
void test(char *, char*);
int main()
{
char filename[20];
char *buffer;
cout<<"Please enter the file name : ";
cin>>filename;
test(filename, buffer);
cout<<"Please enter the file name : ";
cin>>filename;
test(filename, buffer);
}
void test(char *filename, char *buffer)
{
FILE *fd=NULL;
int nbytes;
struct stat s;
int filesize=0;
buffer=NULL;
fd = fopen(filename, "ro");
if(fd == NULL)
{
cout<<"We have some error"<<endl;
return;
}
if(stat(filename, &s) == -1)
{
cout<<"We have some error"<<endl;
return;
}
nbytes = s.st_size;
cout<<"1. File size is : "<<nbytes<<endl;
buffer = (char*) malloc(nbytes, 0);
fread(buffer, nbytes, 1, fd);
if(ferror(fd))
{
fclose(fd);
cout<<"We have some error"<<endl;
return;
}
for(int i=0;i<nbytes+1;i++)
{
if (buffer[i] == '\0')
{
cout<<"xxxxx"<<endl;
cout<<buffer[i]<<endl;
cout<<"We found the \0"<<endl;
break;
}
else
filesize++;
}
cout<<"2. File size is : "<<filesize<<endl;
cout<<"Buffer is : "<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl<<buffer<<endl<<"********************"<<endl;
free(buffer);
return;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2003-05-20 13:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <2B721C6525F0D411B1E900B0D0226BDD02AAE0AC@mohmsg01.ad.infos ys.com>
2003-05-20 13:23 ` Problem with fread Eljay Love-Jensen
2003-05-20 13:15 Ajay Bansal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).