Click here to Skip to main content
15,917,795 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: to confinning users for del,rename & format Pin
John R. Shaw20-Nov-03 12:33
John R. Shaw20-Nov-03 12:33 
GeneralRe: to confinning users for del,rename & format Pin
Peter Molnar20-Nov-03 13:29
Peter Molnar20-Nov-03 13:29 
QuestionQuestion of scope? Pin
dave@home20-Nov-03 9:50
dave@home20-Nov-03 9:50 
AnswerRe: Question of scope? Pin
dave@home20-Nov-03 9:56
dave@home20-Nov-03 9:56 
Generalneed help with fread Pin
ns20-Nov-03 9:47
ns20-Nov-03 9:47 
Generalcheck for tab Pin
Vancouver20-Nov-03 9:53
Vancouver20-Nov-03 9:53 
GeneralRe: check for tab Pin
ns20-Nov-03 10:02
ns20-Nov-03 10:02 
GeneralRe: need help with fread Pin
Antti Keskinen20-Nov-03 10:44
Antti Keskinen20-Nov-03 10:44 
First of, fread fills a buffer you specify with characters read from the file. Characters are different from numbers. For example, a char number[2] = "32" is NOT the same thing as int a = 32

Secondly, a file is just lots of characters placed sequentically, with occasional control markers placed between. There are NO NUMBERS there. So you just cannot read doubles from a file. That just does not work.

Now, let's redo this thing..

// Open the file in text mode
FILE* fInput = fopen("red64.txt", "rt");<DIV>

if ( !fInput )
   return;<DIV>

// Create buffers to hold readed data and temporary data
char buffer[20] = { NULL };
char number[8] = { NULL };
double red64[64];<DIV>

// Access the file
int nDoublePos = 0;<DIV>

while ( !feof(fInput) )
{
      // Read a line from the file
      fgets( &buffer[0], 19, fInput );<DIV>

      // Step through the line, copying digits and decimal points
      int nPosition = 0;
      int nLinePos = 0;<DIV>
      while ( buffer[nLinePos] != '\n' )
      {
           if ( isdigit( buffer[nLinePos] ) || buffer[nLinePos] == '.' )
           {
                  // Copy it to the buffer
                  number[nPosition++] = buffer[nLinePos];
           }<DIV>
            // Move to next character on the line
           nLinePos++;
       }<DIV>

       // Now we have readed one double value into a character string, so convert
       if ( number[0] != NULL )
            red64[nDoublePos++] = atof( &number[0] );<DIV>

       // Clear the buffers & move to the next line
       memset( &buffer[0], 0, sizeof(buffer) );
       memset( &number[0], 0, sizeof(number) );
       
}

Here's a basic idea on how the conversion is implemented. First we read a line to the buffer string. Then we step through the line, copying characters that are either numbers or a '.' character. Then we convert from a string of numbers to a double value using atof.

Don't use the above code blindly, check it for errors, as the forum formatting might've tricked me again Smile | :)

An alternative method would be to read the whole crap into one big buffer, then obtain a pointer to the start of that buffer and always when detecting a number, using atof to convert the number to a double.

-Antti Keskinen


----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
GeneralRe: need help with fread Pin
nss20-Nov-03 10:59
nss20-Nov-03 10:59 
GeneralRe: need help with fread Pin
John R. Shaw20-Nov-03 12:17
John R. Shaw20-Nov-03 12:17 
GeneralCreateDC failure in Citrix 2003 Pin
Fred Eckertson20-Nov-03 9:24
Fred Eckertson20-Nov-03 9:24 
GeneralReading Windows data into unix Pin
Anonymous20-Nov-03 9:03
Anonymous20-Nov-03 9:03 
GeneralRe: Reading Windows data into unix Pin
Antti Keskinen20-Nov-03 9:33
Antti Keskinen20-Nov-03 9:33 
GeneralRe: Reading Windows data into unix Pin
JWood20-Nov-03 9:37
JWood20-Nov-03 9:37 
GeneralRe: Reading Windows data into unix Pin
Johnny ²20-Nov-03 11:12
Johnny ²20-Nov-03 11:12 
General__in64 and DWORD strange size Pin
Anonymous20-Nov-03 8:34
Anonymous20-Nov-03 8:34 
GeneralAlignment Pin
Vancouver20-Nov-03 8:53
Vancouver20-Nov-03 8:53 
GeneralRe: __in64 and DWORD strange size Pin
JWood20-Nov-03 8:54
JWood20-Nov-03 8:54 
GeneralRe: __in64 and DWORD strange size Pin
cmk20-Nov-03 9:01
cmk20-Nov-03 9:01 
Generalcmk is right Pin
Vancouver20-Nov-03 9:11
Vancouver20-Nov-03 9:11 
GeneralText does not appear on colored buttons Pin
Vancouver20-Nov-03 8:13
Vancouver20-Nov-03 8:13 
GeneralSolution Pin
Vancouver20-Nov-03 10:25
Vancouver20-Nov-03 10:25 
GeneralRe: Solution Pin
cmk20-Nov-03 16:13
cmk20-Nov-03 16:13 
GeneralFor cmk Pin
Vancouver21-Nov-03 9:09
Vancouver21-Nov-03 9:09 
GeneralRe: For cmk - back to Vancouver Pin
cmk21-Nov-03 12:02
cmk21-Nov-03 12:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.