• Advertisement

Archived

This topic is now archived and is closed to further replies.

clearing a char *buffer

This topic is 6062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m using a buffer to read in from files, then using atoi to convert back to int. The problem is that the first number read is 5 bytes long as a char, then the others are 3, so whenever it converts the second and third number, it leaves the last two from the first one on. Hmmm...maybe that''s a little confusing. What happens is: fileheader.magic=23503 fileheader.tilesX=12803 fileheader.tilesY=12803 I temporarily fixed it by adding a line in between the first and second reads that says strcpy(buffer, " "); ...but is there a better way to empty the buffer? It''s declared in my code as char *buffer;. -- WNDCLASSEX Reality; ... ... Reality.lpfnWndProc=ComputerGames; ... ... RegisterClassEx(&Reality); Unable to register Reality...what''s wrong? --------- Dan Upton Lead Designer WolfHeart Software

Share this post


Link to post
Share on other sites
Advertisement
Because you are storing the integers as characters in a text file doesn''t mean you must retrieve them as characters. Simply trying something like this:

(Contents of Numbers.dat)
1000 100 200000

(Snippet of Numbers.cpp)
ifstream fInFile(Numbers.dat);
int iNumbers[3];
for (int iIndex = 0; iIndex < 3; iIndex += 1)
fInFile >> iNumbers[iIndex];

Upon the end of this code you''ll have 1000 100 and 200000 in various elements of the iNumbers array. Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Remember that atoi is expecting Null Terminated strings, so put a \0 on the end of every number.

Share this post


Link to post
Share on other sites
try this method

use 2 pointers char* say
char* a;

use a to point at the first sign of character in the text file. Advance the pointer a by one on each character. in that way you don''t have to parse the string all in one, just character by character.

just be sure that other character like tab, space character is bypassed. when null or newline is encountered, proceed to next string in line.

this technique work for me, if you want to know it is to be done in full, please mail me.

Share this post


Link to post
Share on other sites
Alright...thanks, that should help. I had been using ifstream and the >> and << operators, but I switched to using FILE *fp and it wasn''t being friendly. Anyway, now that I hopefully have your attention, is there any convenient way to read read in or cast to an enumerated type? I tried with ifstream >> and it couldn''t convert, so I tried casting it and it couldn''t come up with a suitable cast.

Share this post


Link to post
Share on other sites
the way to make a char* string a NULL string .. meaning empty ... like this one "" ... is to set the first char to NULL ... or more specifically ''\0'' ...

so instead of

strcpy(buffer,"");

use

buffer[0] = ''\0'';


and that''s all there is too it ... setting all the memory to zero is silly ... cause it''ll just hide errors elsewhere in your program ... the best way to zero out a string while debugging is to first fill the whole buffer with a certain letter .. like ''X'' ... and then set just the first char to zero ... this way .. if you screwup your resizing, apending ... or whatever ... you''ll notice it quicker ...

Share this post


Link to post
Share on other sites

  • Advertisement