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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with buffer in fread and fwrite

problem with buffer in fread and fwrite

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 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.
  • H Offline
    H Offline
    hamsha_ir
    wrote on last edited by
    #1

    Hi everyone I have some problem with fread and fwrite in VS2005 beta2 I have the following code, in it i generate a buffer and after assigning some values to it , i write it to a file , then i try to read it back to a new buffer which is not successful. for variable bigVal=100 i get "k=100" for writing but for reading i get "k=77" , i would be pleased if someone can help me. #include "stdafx.h" #include #include #include typedef double dtype; int _tmain(int argc, _TCHAR* argv[]) { dtype *b; int i,j,k; int bigVal; char filename[]="c:\\temp.data"; FILE *fp; bigVal=100; b=(dtype*) malloc(bigVal * sizeof(dtype)); for(i=0 ; i

    K L 2 Replies Last reply
    0
    • H hamsha_ir

      Hi everyone I have some problem with fread and fwrite in VS2005 beta2 I have the following code, in it i generate a buffer and after assigning some values to it , i write it to a file , then i try to read it back to a new buffer which is not successful. for variable bigVal=100 i get "k=100" for writing but for reading i get "k=77" , i would be pleased if someone can help me. #include "stdafx.h" #include #include #include typedef double dtype; int _tmain(int argc, _TCHAR* argv[]) { dtype *b; int i,j,k; int bigVal; char filename[]="c:\\temp.data"; FILE *fp; bigVal=100; b=(dtype*) malloc(bigVal * sizeof(dtype)); for(i=0 ; i

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #2

      Hello. I'm not sure if it's your only problem, but you should definately use binary mode for your files. So use: fopen(filename,"wb") and fopen(filename,"rb") Kakan

      1 Reply Last reply
      0
      • H hamsha_ir

        Hi everyone I have some problem with fread and fwrite in VS2005 beta2 I have the following code, in it i generate a buffer and after assigning some values to it , i write it to a file , then i try to read it back to a new buffer which is not successful. for variable bigVal=100 i get "k=100" for writing but for reading i get "k=77" , i would be pleased if someone can help me. #include "stdafx.h" #include #include #include typedef double dtype; int _tmain(int argc, _TCHAR* argv[]) { dtype *b; int i,j,k; int bigVal; char filename[]="c:\\temp.data"; FILE *fp; bigVal=100; b=(dtype*) malloc(bigVal * sizeof(dtype)); for(i=0 ; i

        L Offline
        L Offline
        Laxman9
        wrote on last edited by
        #3

        in fread and fwrite the first parameter is "void *" so try typecasting the first parameter as following fread((void*)b,sizeof(dtype),bigVal,fp); fwrite((void*)b,sizeof(dtype),bigVal,fp); it worked when i tried past.:) FAILURE is the first step towards SUCCESS

        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