Here's a simple one... I think...
Here is the situation....
I''m writting a program that will read in a text file. I have no idea as to how long the text file will be, and I don''t want to waste space creating a char array of a couple thousand cells.
My plan was to simpley use fseek( file, 0, END ). After doing this I was going to the use ftell( file ) to get the position. I was then going to take the number returned from that function call and create a new array ( char string = new char[ size ] ). After creating the array I then used fread( string, sizeof( char ), size, file ) to read in the text file.
The problem is that this does not work. I end up with a bunch of extra garbage at the end of the string. How can I get this to work? Or does someone have a better way of dynamically creating an array to hold the text file?
This is begging to really agravate me.
Actually, I think it is working! I think the problem however is that a C-style string always is null terminated. C doesn''t initalize memory to 0, and even if it did if you read in exactly the same number of bytes as you''ve allocated, you''ll overwrite your null terminator. The solution of course is just to write a 0 to the end of the array string.
If you are already checking for that, and are referring to data inside your array rather than just what you would get if you tried to printf that array, my apologies, I have no idea then.
- Remnant
- (Steve Schmitt)
- Remnant- (Steve Schmitt)
I don''t believe that to be the problem. Here are my reasons:
1. I initialize the memory to 0 myself using memset(string,0,sizeof(string).
2. The string that is created always ends up being longer than the text. I have no idea as to why.
I think I will write some small clean code (currently its just a mess) and repost this question tommorrow with source code.
Thanks for trying. I would try simply inserting a \0 at the end of the string, but I can''t find the end of the string. You''ll see what I mean when I post the source code (it''s not long).
1. I initialize the memory to 0 myself using memset(string,0,sizeof(string).
2. The string that is created always ends up being longer than the text. I have no idea as to why.
I think I will write some small clean code (currently its just a mess) and repost this question tommorrow with source code.
Thanks for trying. I would try simply inserting a \0 at the end of the string, but I can''t find the end of the string. You''ll see what I mean when I post the source code (it''s not long).
memset(string,0,sizeof(string)
This wouldn''t work because sizeof is determined at compile time. Thus, given this
char* strMyString = new char[200];
sizeof(strMyString) would just give the size of the char*, 1 byte I think
Use this
char* strMyString = new char[200];
memset(strMyString ,0,sizeof(char)*200 )
This wouldn''t work because sizeof is determined at compile time. Thus, given this
char* strMyString = new char[200];
sizeof(strMyString) would just give the size of the char*, 1 byte I think
Use this
char* strMyString = new char[200];
memset(strMyString ,0,sizeof(char)*200 )
January 18, 2000 11:27 PM
Just off the top of my head...
chararray=new char[filelen+1];
//read file into chararray
chararray[filelen]=''\0'';
From your description, it sounds like you''re writing all the way to the end of the array, and then when you do a printf (or whatever) you''re getting garbage following the string in memory, because the last character isn''t a null terminator. (So I''m suggesting you add an extra space, and stuff the terminator in.)
chararray=new char[filelen+1];
//read file into chararray
chararray[filelen]=''\0'';
From your description, it sounds like you''re writing all the way to the end of the array, and then when you do a printf (or whatever) you''re getting garbage following the string in memory, because the last character isn''t a null terminator. (So I''m suggesting you add an extra space, and stuff the terminator in.)
If you use sizeof() on a dynamically allocated array, it''s gonna return the size of a pointer, 4 bytes on a 32 bit machine. If you dynamically allocate an array, you have to keep the size recorded yourself.
------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
You may need to add 1 to the size of the array you created to NULL terminate the string. If you are printing out the file, what is probably happening is you are reading it into the array just fine, but since it is not null terminated, the computer does not know when to stop so it just keeps putting what is in memory after your file until it finally hits a 0. Hope this fixes it.
EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG
Thanks for the help. I got it to work. Just incase you wondered, here is the code i'm using:
Basicaly I needed to change the memset as _dot_ suggested and I needed to add the null turminater at the end as a few people suggested. Let me know if anyone knows a better way to do this.
Edited by - Mike on 1/19/00 11:47:26 AM
void ReadTextFile( char* text_file, char* &string ){ ifstream in_stream; int size; in_stream.open( text_file ); in_stream.seekg( 0, ios::end ); size = in_stream.tellg(); string = new char[ size+1 ]; memset( string, 0, sizeof( char )*(size+1) ); in_stream.seekg( 0, ios::beg ); in_stream.read( string, sizeof( char )*size ); in_stream.close(); string[ size ] = '\0';}
Basicaly I needed to change the memset as _dot_ suggested and I needed to add the null turminater at the end as a few people suggested. Let me know if anyone knows a better way to do this.
Edited by - Mike on 1/19/00 11:47:26 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement