Advertisement

two stupid questions about file input

Started by January 09, 2003 01:17 PM
6 comments, last by penetrator 22 years, 1 month ago
Hi, if i open a file for binary reading, how do i read a string of 1024 bytes all in once ? Second question is: how do i convert a value like 0.231200000000000D+04 in 23120 ? Of course using VC++ ... Thanks !
www.web-discovery.net



  FILE *in = fopen(fileName, "rb");if (!in)   allHellBreaksLoose();fseek(in,0,SEEK_END);int length=ftell(in);if (in<1024)   battonDownTheHatches();fseek(in,0,SEEK_SET);BYTE buffer[1024];fread(buffer,1024,1,in);fclose(in);  




  char * string = "0.231200000000000D+04";float value = (float) atof(string);  


| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Advertisement
is that what you meant?

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Hi Riptorn, thanks for your help ! Reading the file works if the buffer is less than 200, if i want to read 1024 chars the application crash ! That''s odd !



www.web-discovery.net

Perhaps just a typo, but maybe he meant:

if(length <> 1024) error();
yes. was a typo.

if (length<1024)

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Advertisement
ok, i got something working but look at this:

char ch[40];
char se_corner[40];
fseek (pFile,0,SEEK_SET);
fread (ch,1,40,pFile);
fseek (pFile,100,SEEK_SET);
fread (se_corner,1,10,pFile);

when i print out se_corner, i get the 40 chars starting from position 100 (and that''s what i want), but i get also the 40 chars of ch appended to it !!!
Cant understan why



www.web-discovery.net

'strings' are simply arrays of bytes. the print command can tell it's hit the end of a string when it gets to a null terminator, ie, '/0' or more simply, the value 0. Since you don't load this, it will keep reading onward until it eventually hits one. (you cannot tell how long an array is simply by a pointer, which is why this logic applies here)

because you defined the two arrays like so:

char ch[40];
char se_corner[40];

they are placed in memory one after the other.
the values in memory after them are likly zero, so when printing from ch[0], it will continue on till just after of se_corner where it will find the first 0.

hence it doesn't mean your arrays are buggered,

to fix the printing when you want to, simply make a copy of the array, but make it 1 longer and set the last value to 0, or '/0' if you prefer, something like:


  void tools::printArray(char * array, int length){  char * textBlock = new char[length+1];  textBlock[length]='/0';  memcpy(textBlock,array,length);  printf(textBlock);  delete [] textBlock;}  


| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |

[edited by - RipTorn on January 10, 2003 8:56:47 AM]

This topic is closed to new replies.

Advertisement