is memset really needed here?

G

Guest

I had this:
fx_filepath = new TCHAR[path_length];
after copying and reading some string from file into fx_filepath, when I
called:
fprintf(stdout, "%s\n", fx_filepath);
I ended up with 5 random (altough always the same) characters appended to
fx_filepath.
What is more strange, fx_filepath is a member of a class and only the first
object created had this 5 characters appended, although subsequent objects
had exactly the same data fed into fx_filepath (and path_length was of
course the same in all objects).

memset(fx_filepath, '\0', path_length);
seems to do the trick, but I've never knew this is necessery. Is it?
 
F

Frederick Gotham

posted:
I had this:
fx_filepath = new TCHAR[path_length];
after copying and reading some string from file into fx_filepath, when I
called:


Do you copy a null-terminator also?

fprintf(stdout, "%s\n", fx_filepath);
I ended up with 5 random (altough always the same) characters appended
to fx_filepath.
What is more strange, fx_filepath is a member of a class and only the
first object created had this 5 characters appended, although subsequent
objects had exactly the same data fed into fx_filepath (and path_length
was of course the same in all objects).

memset(fx_filepath, '\0', path_length);
seems to do the trick, but I've never knew this is necessery. Is it?


fx_filepath = new TCHAR[path_length]();

The empty parentheses after the square brackets default-initialises each
member of the array.
 
G

Guest

Do you copy a null-terminator also?

I read all the characters from file until
!feof(p_file) so I think null terminator is also read.
fx_filepath = new TCHAR[path_length]();

The empty parentheses after the square brackets default-initialises each
member of the array.

Yes, this also works.
 
G

Guest

TCHAR temp_buf;
while (fgets(&temp_buf, 1, p_file)!=NULL)
{
count++;
}

freezes my app, whereas:

while (!feof(p_file))
{
fread(&temp_buf, sizeof(TCHAR), 1, p_file);
count++;
}

works fine.
 
R

red floyd

TCHAR temp_buf;
while (fgets(&temp_buf, 1, p_file)!=NULL)
{
count++;
}

freezes my app, whereas:

while (!feof(p_file))
{
fread(&temp_buf, sizeof(TCHAR), 1, p_file);
count++;
}
This idiom is improper.

also, there is no such thing as TCHAR.
 
G

Guest

red floyd said:
This idiom is improper.

also, there is no such thing as TCHAR.

typedef wchar_t TCHAR

ok, I came to a point where I no longer care whether something is improper
or collides with some standard - I just want it to work;) Just be so nice to
give me a working solution if you say that mine is wrong.
 
R

Ron House

ok, I came to a point where I no longer care whether something is improper
or collides with some standard - I just want it to work;)

Translation: I came to a point where I no longer care whether something
has its best chance of working reliably, portably, and through changes
in compiler versions or brands - I just want it to work :(
 
P

Pete C

TCHAR temp_buf;
while (fgets(&temp_buf, 1, p_file)!=NULL)
{
count++;
}

freezes my app

Which you should expect if you read the documentation for fgets:

char *fgets(char *s, int size, FILE *stream);
fgets() reads in at most one less than 'size' characters from 'stream'
and stores them into the buffer pointed to by 's'.

So you are reading in 'at most' 0 characters from your stream each
time, and placing a single null character into temp_buf. But even this
will be wrong if TCHAR is typedefed as a wchar_t (as you say it is). If
you really want to read wchar_t's (is the file in a multibyte
encoding?) then you should be using the fgetws function.
 
G

Guest

Thomas J. Gritzan said:
There are no null-terminators in text files.

And reading until feof(file) in C and file.eof() in C++ is equally wrong.

See the C FAQ:
http://c-faq.com/stdio/feof.html

And the C++ FAQ for iostream input:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

But look at the example from Microsoft docs (I'm working with Visual 8):

// crt_feof.c
// This program uses feof to indicate when
// it reaches the end of the file CRT_FEOF.TXT. It also
// checks for errors with ferror.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int count, total = 0;
char buffer[100];
FILE *stream;

fopen_s( &stream, "crt_feof.txt", "r" );
if( stream == NULL )
exit( 1 );

// Cycle until end of file reached:
while( !feof( stream ) )
{
// Attempt to read in 100 bytes:
count = fread( buffer, sizeof( char ), 100, stream );
if( ferror( stream ) ) {
perror( "Read error" );
break;
}

// Total up actual bytes read
total += count;
}
printf( "Number of bytes read = %d\n", total );
fclose( stream );
}Do you mean they are wrong? Or does this code work only with Microsoft
compiler?
 
G

Guest

Pete C said:
Which you should expect if you read the documentation for fgets:

char *fgets(char *s, int size, FILE *stream);
fgets() reads in at most one less than 'size' characters from 'stream'
and stores them into the buffer pointed to by 's'.

So you are reading in 'at most' 0 characters from your stream each
time, and placing a single null character into temp_buf. But even this
will be wrong if TCHAR is typedefed as a wchar_t (as you say it is). If
you really want to read wchar_t's (is the file in a multibyte
encoding?) then you should be using the fgetws function.

actually I have no idea about encoding, I've just read somewhere that using
TCHAR and TCHAR.H routines can lead to more portable code. So is using
_fgetts ok here?
 
P

peter koch

typedef wchar_t TCHAR

ok, I came to a point where I no longer care whether something is improper
or collides with some standard - I just want it to work;) Just be so nice to
give me a working solution if you say that mine is wrong.

Your biggest problem is to use a low-level C interface. This definitely
is a huge mistake - especially as you post here at comp.lang.c++ ;-)
Using std::string and streams would reduce your program to very few
lines - and those lines would "just work" without any worries about
memory leaks, buffer overflows and related stuff. It is to late now,
but next time do start your program as C++, not C. Ill bet you will
reach the finish line sooner that way (and if not, you'll surely do so
the next time).

/Peter
 
I

IvoShalev

I'm not sure what exactly is the problem, but
if you have a void constructor you willnot have such problems.
For example:

struct TArr
{
TArr(void) { a='\0'; }

char a;
} TCHAR;

in this way everytime you create a new variable a will be automaticaly
'\0'

www.pegashometour.com

yours Ivo
 
V

Victor Bazarov

I'm not sure what exactly is the problem, but
if you have a void constructor you willnot have such problems.
For example:

struct TArr
{
TArr(void) { a='\0'; }

char a;
} TCHAR;

in this way everytime you create a new variable a will be automaticaly
'\0'

An alternative (often preferred) implementation of the c-tor would be

TArr() : a(0) {}

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top