Reading the first number from a file

 

Hi I need to read the first number from a file. The number is formatted in the file like this:

0.000000000000000000e+00

I don't want the program to take anything into account other than the first number (the first 0).


My current code is:

int ReadReceived()
   {
   int filehandle=FileOpen("modelout.txt",FILE_READ|FILE_SHARE_READ|FILE_SHARE_WRITE);
   int modelnum = FileReadInteger(filehandle, 4);

   return(modelnum);   
   
   FileOpen(filehandle,FILE_WRITE|FILE_CSV|FILE_SHARE_READ|FILE_SHARE_WRITE,',');
   FileClose(filehandle);
   
   }

However, it seems to intepret everything as a 1.


Any help would be greatly appreciated. Thanks!

 
  1. Your number is double type, not integer.
  2. If you want to read from txt file use FileReadNumber not FileReadInteger
  3. Use proper flags on FileOpen - FILE_READ | FILE_TXT should be enough if the file is not opened by another process, otherwise use FILE_SHARE_READ
  4. Do not call FileOpen twice - no need for it
  5. Close the file BEFORE return.
  6. You can add error handling - try yourself :)
double ReadReceived()
  {
   int filehandle=FileOpen("modelout.txt",FILE_READ|FILE_TXT);
   double modelnum=filehandle!=INVALID_HANDLE?FileReadNumber(filehandle):0.0;
   FileClose(filehandle);
   return(modelnum);
  }



Reason: