Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. need help with fread

need help with fread

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ns
    wrote on last edited by
    #1

    I just posted and deleted my post because I feel its incredibly dumb. (What I want to do I know how to with CSdioFIle and atof, but I want to try with fread). I have a txt file that looks like this:

        0
    0.0500
    0.0990
    0.1490
    0.1980
    0.2430
    0.2870
    0.3310
    0.3740
    0.4150
    0.4530
    0.4920
    0.5300
    

    with the leading spaces (4 spaces). I copied this data from matlab and pasted into notepad. I want to load this data into an array of doubles. I did the following but evidently I dont know how to use fread inspite of re-reading the docs and googling. It just doesnt click :(

    FILE\* fpRed = fopen("red64.txt", "r");
    if (!fpRed ) return;
    
    double red64\[64\];
    
    while  (!feof(fpRed))
    {
    	for (int i = 0; i < 64; i++)
    	{
    		fread(red64+i, sizeof(double), 10, fpRed);
    
    		double r = red64\[i\];
    	}
    }
    	fclose(fpRed);
    

    I put down 10 because its 4 white spaces + 6 spaces in the number itself. If theres invisible stuff in the file I dont know...... :confused: Thanks for helping, ns

    V A 2 Replies Last reply
    0
    • N ns

      I just posted and deleted my post because I feel its incredibly dumb. (What I want to do I know how to with CSdioFIle and atof, but I want to try with fread). I have a txt file that looks like this:

          0
      0.0500
      0.0990
      0.1490
      0.1980
      0.2430
      0.2870
      0.3310
      0.3740
      0.4150
      0.4530
      0.4920
      0.5300
      

      with the leading spaces (4 spaces). I copied this data from matlab and pasted into notepad. I want to load this data into an array of doubles. I did the following but evidently I dont know how to use fread inspite of re-reading the docs and googling. It just doesnt click :(

      FILE\* fpRed = fopen("red64.txt", "r");
      if (!fpRed ) return;
      
      double red64\[64\];
      
      while  (!feof(fpRed))
      {
      	for (int i = 0; i < 64; i++)
      	{
      		fread(red64+i, sizeof(double), 10, fpRed);
      
      		double r = red64\[i\];
      	}
      }
      	fclose(fpRed);
      

      I put down 10 because its 4 white spaces + 6 spaces in the number itself. If theres invisible stuff in the file I dont know...... :confused: Thanks for helping, ns

      V Offline
      V Offline
      Vancouver
      wrote on last edited by
      #2

      Are you sure there are four spaces at the beginning, and not one tab?

      N 1 Reply Last reply
      0
      • V Vancouver

        Are you sure there are four spaces at the beginning, and not one tab?

        N Offline
        N Offline
        ns
        wrote on last edited by
        #3

        It backs up one by one four spaces. If it were a tab what would I do for nItemstoRead? Anyways for the first few elements I removed the spaces so there numbers start flush at the left, and yet when I step through I get garbage. This time I said nItems = 6 since 0.5000 etc. Thanks for helping, ns

        1 Reply Last reply
        0
        • N ns

          I just posted and deleted my post because I feel its incredibly dumb. (What I want to do I know how to with CSdioFIle and atof, but I want to try with fread). I have a txt file that looks like this:

              0
          0.0500
          0.0990
          0.1490
          0.1980
          0.2430
          0.2870
          0.3310
          0.3740
          0.4150
          0.4530
          0.4920
          0.5300
          

          with the leading spaces (4 spaces). I copied this data from matlab and pasted into notepad. I want to load this data into an array of doubles. I did the following but evidently I dont know how to use fread inspite of re-reading the docs and googling. It just doesnt click :(

          FILE\* fpRed = fopen("red64.txt", "r");
          if (!fpRed ) return;
          
          double red64\[64\];
          
          while  (!feof(fpRed))
          {
          	for (int i = 0; i < 64; i++)
          	{
          		fread(red64+i, sizeof(double), 10, fpRed);
          
          		double r = red64\[i\];
          	}
          }
          	fclose(fpRed);
          

          I put down 10 because its 4 white spaces + 6 spaces in the number itself. If theres invisible stuff in the file I dont know...... :confused: Thanks for helping, ns

          A Offline
          A Offline
          Antti Keskinen
          wrote on last edited by
          #4

          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");

          if ( !fInput )
          return;

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

          // Access the file
          int nDoublePos = 0;

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

            // Step through the line, copying digits and decimal points
            int nPosition = 0;
            int nLinePos = 0;
          
            while ( buffer\[nLinePos\] != '\\n' )
            {
                 if ( isdigit( buffer\[nLinePos\] ) || buffer\[nLinePos\] == '.' )
                 {
                        // Copy it to the buffer
                        number\[nPosition++\] = buffer\[nLinePos\];
                 }
          
                  // Move to next character on the line
                 nLinePos++;
             }
          
             // Now we have readed one double value into a character string, so convert
             if ( number\[0\] != NULL )
                  red64\[nDoublePos++\] = atof( &number\[0\] );
          
             // 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

          N J 2 Replies Last reply
          0
          • A Antti Keskinen

            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");

            if ( !fInput )
            return;

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

            // Access the file
            int nDoublePos = 0;

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

              // Step through the line, copying digits and decimal points
              int nPosition = 0;
              int nLinePos = 0;
            
              while ( buffer\[nLinePos\] != '\\n' )
              {
                   if ( isdigit( buffer\[nLinePos\] ) || buffer\[nLinePos\] == '.' )
                   {
                          // Copy it to the buffer
                          number\[nPosition++\] = buffer\[nLinePos\];
                   }
            
                    // Move to next character on the line
                   nLinePos++;
               }
            
               // Now we have readed one double value into a character string, so convert
               if ( number\[0\] != NULL )
                    red64\[nDoublePos++\] = atof( &number\[0\] );
            
               // 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

            N Offline
            N Offline
            nss
            wrote on last edited by
            #5

            Thank you so much for your detailed response! I see I had totally misunderstood fread! I'll check it out. I had thought CStdioFile and atof was too circuitous but I now see that fread is also doing chars..... thanks, ns

            1 Reply Last reply
            0
            • A Antti Keskinen

              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");

              if ( !fInput )
              return;

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

              // Access the file
              int nDoublePos = 0;

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

                // Step through the line, copying digits and decimal points
                int nPosition = 0;
                int nLinePos = 0;
              
                while ( buffer\[nLinePos\] != '\\n' )
                {
                     if ( isdigit( buffer\[nLinePos\] ) || buffer\[nLinePos\] == '.' )
                     {
                            // Copy it to the buffer
                            number\[nPosition++\] = buffer\[nLinePos\];
                     }
              
                      // Move to next character on the line
                     nLinePos++;
                 }
              
                 // Now we have readed one double value into a character string, so convert
                 if ( number\[0\] != NULL )
                      red64\[nDoublePos++\] = atof( &number\[0\] );
              
                 // 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

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              :)Good answer! Minor detail: char number[2] = "32"; // Outch: char number[2] = {'3','2','\0'} Should be char number[3] = "32"; // Ok even in our sleep. ;)Sorry automatic reaction when I see a line of code (even example code) that makes my heart skip a beat. INTP

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups