CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    C++ fwrite dosnt write to text file , have no idea why

    i have this code that basically read from file and create new file and write the contend from the source to the destination file . it reads to the buffer and creates the file ,
    but fwrite
    dost write the content to the new created file i have no idea why ..
    here is the code . ( i have to use only this with _sopen , its part of legacy code)
    Code:
    std::string szSource = "H:\\cpp\\test1.txt";
    FILE* pfFile;
    int iFileId = _sopen(szSource.c_str(),_O_RDONLY, _SH_DENYNO, _S_IREAD);
    if (iFileId >= 0) 
     pfFile = fdopen(iFileId, "r");
    
    //read file content to buffer 
    char * buffer;
    size_t result;
    long lSize;
    // obtain file size:
    fseek (pfFile , 0 , SEEK_END);
    lSize = ftell (pfFile);
    fseek(pfFile, 0, SEEK_SET);
    
     buffer = (char*)malloc(lSize);
    // also i did :
    //buffer = (char*) malloc (sizeof(char)*lSize); <--same not working
    
    if (buffer == NULL)
    {
    
       return false;
    }
    
    // copy the file into the buffer:
    result = fread (buffer,lSize,1,pfFile);   
    std::string szdes = "H:\\cpp\\test_des.txt";
    FILE* pDesfFile;
    int iFileId2 = _sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
    if (iFileId2 >= 0) 
     pDesfFile = fdopen(iFileId2, "w+");
    
    size_t f = fwrite (buffer , 1, sizeof(buffer),pDesfFile ); //<-- the f returns me 4
    // also did:
    //size_t f = fwrite (buffer , 1, lSize ,pDesfFile ); // bu dosnt work
    fclose (pDesfFile);

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: C++ fwrite dosnt write to text file , have no idea why

    1) If you are using fread/fwrite, you should open the file in binary mode
    in both the read/write.

    2) you have ...

    Code:
    size_t f = fwrite (buffer , 1, sizeof(buffer),pDesfFile );

    buffer is a pointer ... it will have the length of a pointer on your system
    (probably 4). So you should be using the lsize version of the write.

  3. #3
    Join Date
    Aug 2006
    Posts
    230

    Re: C++ fwrite dosnt write to text file , have no idea why

    i did this and changed it to be binary open like this:
    pfFile = fdopen(iFileId, "rb");
    pDesfFile = fdopen(iFileId2, "wb");

    and changed to :
    size_t f = fwrite (buffer , 1, lSize ,pDesfFile );

    but still empty file :
    here is my full tester file , maybe its something in my pc , mybe if someone can run it so it will be great.
    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>
    #include <fcntl.h>
    #include <string>
    #include <share.h>
    #include <sys\stat.h>
    
    
    int main () {
      std::string szSource = "H:\\cpp\\test1.txt";
      FILE* pfFile;
      int iFileId = _sopen(szSource.c_str(),_O_RDONLY, _SH_DENYNO, _S_IREAD);
      if (iFileId >= 0) 
         pfFile = fdopen(iFileId, "rb");
       //read file content to buffer 
       char * buffer;
       size_t result;
       long lSize;
       // obtain file size:
       fseek (pfFile , 0 , SEEK_END);
       lSize = ftell (pfFile);
       fseek(pfFile, 0, SEEK_SET);
     //   buffer = (char*) malloc (sizeof(char)*lSize);
       buffer = (char*) malloc (sizeof(char)*lSize);
    
       if (buffer == NULL)
       {
         
           return false;
       }
    
       // copy the file into the buffer:
       result = fread (buffer,lSize,1,pfFile);   
       std::string szdes = "H:\\cpp\\test_des.txt";
       FILE* pDesfFile;
       int iFileId2 = _sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
      if (iFileId2 >= 0) 
         pDesfFile = fdopen(iFileId2, "wb");
         
       size_t f = fwrite (buffer , 1, sizeof(buffer),pDesfFile );
       //size_t f = fwrite (buffer , 1, lSize ,pDesfFile );
       printf("Error code: %d\n",ferror(pDesfFile));
    
       fclose (pDesfFile);
       
      return 0;
    }

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: C++ fwrite dosnt write to text file , have no idea why

    Maybe the _sopen is not correct. I don't have that function on my
    compiler. The modified version below works for me. Also, on
    windows there are copy file functions in the API, and in C++
    there are easier ways to copy an entire file.

    Code:
    int main () 
    {
       std::string szSource = "H:\\cpp\\test1.txt";
       FILE* pfFile;
       pfFile = fopen(szSource.c_str(), "rb");
    
       //read file content to buffer 
       char * buffer;
       size_t result;
       long lSize;
    
            // obtain file size:
       fseek (pfFile , 0 , SEEK_END);
       lSize = ftell (pfFile);
       fseek(pfFile, 0, SEEK_SET);
       buffer = (char*) malloc (sizeof(char)*lSize);
    
       if (buffer == NULL)
       { 
           return -1;
       }
    
       result = fread (buffer,lSize,1,pfFile); 
       
       // copy the file into the buffer:
       std::string szdes = "H:\\cpp\\test_des.txt";
    
       FILE* pDesfFile;
       pDesfFile = fopen(szdes.c_str(), "wb");
         
       size_t f = fwrite (buffer , 1, lSize ,pDesfFile );
    
       fclose (pfFile);
       fclose (pDesfFile);
    
       free(buffer);
       
      return 0;
    }

  5. #5
    Join Date
    Aug 2006
    Posts
    230

    Re: C++ fwrite dosnt write to text file , have no idea why

    i know this is the problem i have legacy code that i have to use, first time i use this windows _sopen function

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: C++ fwrite dosnt write to text file , have no idea why

    1) Maybe you should add _O_BINARY to both _sopen calls

    2) you are still using sizof(buffer) in your latest test code.

    3) what is the value of lSize ??

    4) what are the return values of the _sopen calls ?
    Last edited by Philip Nicoletti; September 1st, 2011 at 10:31 AM.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: C++ fwrite dosnt write to text file , have no idea why

    I got on a system with Visual C++ 2010. The code below created
    the file correctly. (changes some of the open flags and the fwrite
    to use lSize ...
    Code:
    int main () 
    {
      std::string szSource = "H:\\cpp\\test1.txt";
      FILE* pfFile;
      int iFileId = _sopen(szSource.c_str(), _O_RDONLY | _O_BINARY, _SH_DENYNO, _S_IREAD);
      
      if (iFileId >= 0) 
         pfFile = fdopen(iFileId, "rb");
    
       //read file content to buffer 
       char * buffer;
       size_t result;
       long lSize;
       // obtain file size:
       fseek (pfFile , 0 , SEEK_END);
       lSize = ftell (pfFile);
       fseek(pfFile, 0, SEEK_SET);
     //   buffer = (char*) malloc (sizeof(char)*lSize);
       buffer = (char*) malloc (sizeof(char)*lSize);
    
       if (buffer == NULL)
       {
           return -1;
       }
    
       // copy the file into the buffer:
       result = fread (buffer,lSize,1,pfFile);   
       std::string szdes = "H:\\cpp\\test_des.txt";
       FILE* pDesfFile;
       int iFileId2 = _sopen(szdes.c_str(), _O_CREAT | _O_BINARY | _O_RDWR,_SH_DENYNO,_S_IREAD | _S_IWRITE);
    
      if (iFileId2 >= 0) 
         pDesfFile = fdopen(iFileId2, "wb+");
         
       size_t f = fwrite (buffer , lSize, 1 ,pDesfFile );
    
       printf("Error code: %d\n",ferror(pDesfFile));
    
       fclose (pDesfFile);
       fclose(pfFile);
    
       free(buffer);
       
      return 0;
    }

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: C++ fwrite dosnt write to text file , have no idea why

    Quote Originally Posted by Philip Nicoletti View Post
    1) Maybe you should add _O_BINARY to both _sopen calls

    2) you are still using sizof(buffer) in your latest test code.
    I can only support these.

    In addition, umen, you're only specifying the open mode _O_CREAT when you open the (low-level) output file, which fails if the file already exists; you probably need _O_TRUNC as well. Also, you're not specifying any read/write modes. So I suggest you change the function call in question to this:

    Code:
    int iFileId2 = _sopen(szdes.c_str(),_O_CREAT | _O_TRUNC | _O_WRONLY | _O_BINARY,_SH_DENYNO,_S_IREAD | _S_IWRITE);
    That way it works for me (of course only after applying the corrections proposed by Philip Nicoletti as well).

    At any rate, it's totally obscure to me why you insist on using that fiddly combination of high-level and low-level file access. I somehow can't accept the argument "legacy code": Who the heck needs a command-line file copying program at all, even more so if it's implemented like that (and has hard-coded file specs!)?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Aug 2006
    Posts
    230

    Re: C++ fwrite dosnt write to text file , have no idea why

    First of all thank allot for your answers
    i know this is crap code . but this is what i need to work with . what can i do ...
    Thanks Again
    can someone tell me what is the difference between _sopen and fopen
    maybe i can tell my boss's to drop the _sopen

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: C++ fwrite dosnt write to text file , have no idea why

    There are mainly two differences between _sopen() and fopen(): (1) _sopen() is low-level (like _open()) while fopen() operates on a higher level. (2) _sopen() allows you to specify the sharing mode parameter, fopen() does not. But if you want both high-level access and control over the sharing mode I'd suggest to use _fsopen().
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Aug 2006
    Posts
    230

    Re: C++ fwrite dosnt write to text file , have no idea why

    so for preference its better to use the low_level?

  12. #12
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: C++ fwrite dosnt write to text file , have no idea why

    I wouldn't say so, generally. For instance, the low-level functions don't include an equivalent to fprintf(). I would prefer low-level access only if there's a specific reason to use it (and I don't see one here). (To clarify: _fsopen() is one of the high-level functions: It returns a FILE *.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured