fgets() vs fread()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasuprasadula
    New Member
    • Jul 2007
    • 1

    fgets() vs fread()

    Hi all

    I developed a client server program that uses secure RTP API's. This program opens a text file and read the content of the file and sends it to client. At the client side the content will display on std output. To read the content of the file i used fgets(). The real problem was, when i tried to transfer a file by reading its content using fread() and writing its content at client side using fwrite(), it is not transferring the exact file.
    The client server program is working for transferring strings of a text file but it is unable to transfer the file.

    I will post the code with in short peroid

    Thanks in advance
    vasu
  • vincentt
    New Member
    • Jul 2007
    • 4

    #2
    Originally posted by vasuprasadula
    Hi all

    I developed a client server program that uses secure RTP API's. This program opens a text file and read the content of the file and sends it to client. At the client side the content will display on std output. To read the content of the file i used fgets(). The real problem was, when i tried to transfer a file by reading its content using fread() and writing its content at client side using fwrite(), it is not transferring the exact file.
    The client server program is working for transferring strings of a text file but it is unable to transfer the file.

    I will post the code with in short peroid

    Thanks in advance
    vasu
    HI Vasu,

    Generally fgets() is used to read from text files and fputs() to write to a text file. Fread() and fwrite() are usually associated with binary files or files where you have kind of structured records to be written. You need to give the size of the record etc to read. fread() also does not bother about carriage return when reading thru files, as it can read thru n-length files as well without Carriage return.

    If the files you are reading are text files, then stick to fgets() and fputs(). fgets() will read the length as mentioned in the function itself, or till end of line, ie the \n character, which makes it ideal for reading thru text files.

    Comment

    • TejasBadhe07
      New Member
      • Nov 2022
      • 1

      #3
      Hi Vincent,

      I'm slightly confused here because I wanted the read data from .ecg files. And for this, I need the perform some operations along with the function for reading the data inside that file in human-understanding form for that fread() is working along with the expectations, but also I need to write that data into another file there I got a problem with fwrite(), and the whole application is C++ based so how can I use fgets() instead of fread() for reading and also suggest what should be the ideal approach to write the values of that file. I will attach a code snippet here for better understanding.

      Code:
      int ReadBlockFromFile(std::string aPath, long blockSize, unsigned char* buff) // Reading file 
      {
          FILE* inputstream = fopen(aPath.c_str(), "rb");
          fread(buff, 1, blockSize, inputstream);
      }

      Comment

      • doze121
        New Member
        • Nov 2022
        • 2

        #4
        its good information

        its article are helpful for me
        GBWhatsApp

        Comment

        • IPLka
          New Member
          • Nov 2025
          • 1

          #5
          its good information

          its article are helpful for me

          IPL 2026 ka Ticket Price
          Telegram Search Engine

          Comment

          Working...