Thread: need working read file function

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    need working read file function

    I have a problem with reading files.
    I use this code to read a file:

    Code:
    			FILE *t;
    			long lSize;
    			char * buffer;
    			char *l;
    			//read a file.
    			t = fopen(arg,"rb");
    			if (t!=NULL) {
    				fseek (t , 0 , SEEK_END);
    				lSize = ftell (t);
    					rewind (t);
    					buffer = (char*) malloc (lSize);
    					fread (buffer,1,lSize,t);
    					l = strtok(buffer,"\r\n");
    					while(l != NULL) { 
    						if (strlen(l)>0) {
    							safeircfsend("PRIVMSG %s :%s\r\n",nick, l);
    						}
    						l = strtok(NULL,"\r\n");
    					}
    					fclose(t);
    					free (buffer);
    OK works fine for Text files, but binary files such as exe Files have characters that break the loop an I only get a little big of the real file.
    I need a function which definatly read the whole file and give me the output.
    Any ideas?

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    What kind of output? Text output? Binary files may have unprintable characters.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Dante Shamest
    What kind of output? Text output? Binary files may have unprintable characters.
    The output goes over sockets (send). So the unprintable characters shouldn't be the problem.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is safeircfsend your own function? You may want to write another version for binary data -- much like the send that takes a pointer to the start and the total length (rather than relying on a C-string's null terminator).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Dave_Sinkula
    Is safeircfsend your own function? You may want to write another version for binary data -- much like the send that takes a pointer to the start and the total length (rather than relying on a C-string's null terminator).
    Yes safeircfsend is my own function which sends the data over sockets.
    But I need a better function for reading files (also binary files), can someone help me with a code example?

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You've already done it. fread() reads binary files. Just don't do any text processing and rewrite safeircfsend to send bytes.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Dante Shamest
    You've already done it. fread() reads binary files. Just don't do any text processing and rewrite safeircfsend to send bytes.
    So you mean at this point: fread (buffer,1,lSize,t);
    I have the whole file in buffer, and the mistake must be at another point for example in my function safeircfsend?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A pseudo-code example:
    Code:
    void foo(const char *arg, const char *nick)
    {
       FILE *file = fopen(arg, "rb");
       if ( file != NULL )
       {
          char *buffer;
          long lSize;
          
          fseek(file, 0, SEEK_END);
          lSize = ftell(file);
          rewind(file);
          
          buffer = (char*)malloc(lSize);
          if ( buffer != NULL )
          {
             if ( fread(buffer, 1, lSize, file) == lSize )
             {
                safeircfsendbin(nick, buffer, lSize);
             }
             free(buffer);
          }
          fclose(file);
       }
    }
    [edit]Write the safeircfsendbin function to send the buffer as one big pile of bytes. Add a preamble and closing if needed.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM