This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Can't read full file.

Hi.

I use CCs 3.3 and c6713 simulator. I want read file with binary data from file. I wrote this program:

 


 

#include <stdio.h>

#define BUFSIZE 80

void main()
{
   char      fileStr[BUFSIZE];
   size_t    readSize=0;
   FILE      *fptr=NULL;

fprintf (stderr, "reading from file\n");
                   
   if (NULL == (fptr = fopen("file.bin", "r")))
{
fprintf(stderr, "can't open file");
return 0;
}
                     
while (! feof(fptr))
{
    readSize = fread(fileStr, sizeof(char), BUFSIZE, fptr);
    fprintf(stderr, "Read a %d byte \n", readSize);
}

fprintf(stderr, "file is over\n");

   fclose(fptr);
   
}

program output:

reading from file

Read a 472 byte

file is over


But  the program reads only part of file, only 472 bytes. I tried to change the BUFSIZE, but it does not help, the program reads only 472 bytes, then the feof reports that file ended.

Does this restriction only for debugging on simulator?

  • lexa fedotov,

    This is not a C6000 DSP hardware question, so it will be moved to the TI C/C++ Compiler forum, in case my advice is not enough.

    lexa fedotov said:
    fopen("file.bin", "r")

    This opens the file for ASCII or text access. Try "rb" to make the accesses be for binary data.

    Regards,
    RandyP

     

    If you need more help, please reply back. If this answers the question, please click  Verify Answer  , below.

  • If it is a binary file, you must use "rb" to open it.  Failing to do so can cause problems when 0x00 or 0xff appears in the binary file (or Ctrl-Z on DOS).  Please try it again with fopen("file.bin", "rb").

  • Thank you. It's works for me. Can you explain why fopen for ascii and binary files so different? According linux manpage:




    The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the  twocharacter strings described above.  This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux.



    Does this mean ccs not posix compatible?

  • lexa fedotov said:
    Does this mean ccs not posix compatible?

    I presume you are executing CCS under MicroSoft Windows.  It is ultimately low-level operations in Windows which open the file and read it.  I don't specifically know whether Windows is Posix compatible, but I doubt it.

    Thanks and regards,

    -George

  • That manual page is specific to Linux, and does not reflect the Windows behavior.  On Linux, the 'b' indeed has no effect, but on Windows, it does.  POSIX is a Unix standard.