Files help

This is a discussion on Files help within the C Programming forums, part of the General Programming Boards category; Hi im doing a program that requires sending a message with the lenght of the file and its content. How ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    Files help

    Hi im doing a program that requires sending a message with the lenght of the file and its content. How can i do this. I know how to send the content of the file, if the file contains characters, but how can i do this if the file can be of any tipe, like jpg or gif.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    You can read the file as a binary file into a buffer..and then send it byte by byte.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Tanks but can you be more especific.
    I do this:

    Code:
    File *fp;
    fp = fopen("...", "rb");
    and then wath? i have to read the file and save is content in some place, but how can i do that.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    For reading the file in binary mode, you can use the function fread()
    IIRC, its syntax is fread(void *ptr,size_t size,int num,FILE* source);
    here,
    1) ptr is the buffer where the data is to be read into,
    2) size is the size of each item read (in bytes)
    3) num is the number of items to be read.
    4) source is the pointer to the input stream.

    Go through the tutorials on the site if you aren't familiar with pointers and arrays.

    Also, for sending the data, you can use two methods..
    1) read the whole file into a buffer and then send it.
    2) read certain number of bytes into the buffer, send them and continue this way till the file ends.

    Personally i prefer method 2 as it is better and saves space as well as time.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Ok i have done something like this

    Code:
    int array[file_lenght];
    
    fread(array,sizeof(int), file_length, file);
    i want to do this to read jpg files, the problem is i dont know the lenght of the file so how can i know it

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, to know the length/size of the file you can use these functions fseek() and ftell(). make the file pointer to point at the last element and the call ftell. u will get the size. sample code

    Code:
    fseek(fp,0,SEEK_END);   // making the fuile pointer point at last
    
    size = ftell(fp);
    ssharish2005

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Ok ive done something like this.

    Code:
    FILE *fp;
    
    if(fp != NULL) {
       fseek(fp,0,SEEK_END);
       bytes = ftell(fp);
       int array[bytes];
       fread(array, sizeof(int), bytes, fp);
    }
    Now i want to to print the file content, how can i do it.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you print anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    I guess you need to work more with arrays and pointers before you jump into file I/O
    1) int array[bytes] . Variable length arrays are not allowed in C.
    2) fseek(fp,0,SEEK_END); This will set the file pointer to the end of the file, no use reading from the end now, is there? nothing that is wanted will be read into the buffer.
    3) The fopen() call is missing.

    You need to have more experience with C before jumping into file I/O.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    I guess you need to work more with arrays and pointers before you jump into file I/O
    1) int array[bytes] . Variable length arrays are not allowed in C.
    2) fseek(fp,0,SEEK_END); This will set the file pointer to the end of the file, no use reading from the end now, is there? nothing that is wanted will be read into the buffer.
    3) The fopen() call is missing.
    This is just a piece of my code. The fopen() call is there. And why cannot
    i make this int array[bytes] . bytes its an int.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by maritos
    This is just a piece of my code. The fopen() call is there. And why cannot
    i make this int array[bytes] . bytes its an int.
    You can, only if you have a C99 compiler. If you don't, it is illegal in C. In otherwords, before C99, you could not have arrays declared whose size was dependant on another variable.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by maritos
    Ok ive done something like this.

    Code:
    FILE *fp;
    
    if(fp != NULL) {
       fseek(fp,0,SEEK_END);
       bytes = ftell(fp);
       int array[bytes];
       fread(array, sizeof(int), bytes, fp);
    }
    Now i want to to print the file content, how can i do it.
    Are you sure you want to try to read sizeof(int) * bytes bytes from the file? Wouldn't bytes bytes be what you want?
    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. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21