fread, fwrite..need help for this

This is a discussion on fread, fwrite..need help for this within the C Programming forums, part of the General Programming Boards category; when i try to read a text file and write it again using these commands..i can do it without any ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    146

    fread, fwrite..need help for this

    when i try to read a text file and write it again using these commands..i can do it without any problems..but when i try to do it with image files..it cannot copy it..it creates a blank file...
    Do I need some other functions to do this??

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Make sure you open in binary mode.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Thanks for quick reply..
    yes i open it in binary mode only..
    I got the size of the file with fseek as u suggested, but now facing problems with copying image files..I have pasted the code here...
    Code:
    void main()
    {
    clrscr();
    printf("\nEnter the file path:");
    gets(name);
    fp=fopen(name,"rb");
    size=sizeoffile(fp);
    printf("\nSize of the file is: %ld",size);
    printf("\nEntr the path of copy file:");
    gets(temp);
    fw=fopen(temp,"wb");
    copyoffile(fp,size);
    fclose(fw);
    fclose(fp);
    getch();
    }
    void copyoffile(FILE *fp,long int size)
    {
    fread(&buffer,size,1,fp);
    fwrite(&buffer,size,1,fw);
    }
    
    long sizeoffile(FILE *fp)
    {
       long curpos, length;
       curpos = ftell(fp);
       fseek(fp, 0L, SEEK_END);
       length = ftell(fp);
       fseek(fp, curpos, SEEK_SET);
       return(length);
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Ident more properly. Don't use void main. Don't use gets. Don't use getch. Where are buffer and name defined?
    Use int main, fgets and getchar instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    FILE *fp,*fw;
    char *name,*temp;
    long int size,t1;
    void *buffer;
    long sizeoffile(FILE *fp);
    void copyoffile(FILE *fp,long int size);
    
    int main()
    {
    clrscr();
    printf("\nEnter the file path:");
    gets(name);
    fp=fopen(name,"rb");
    size=sizeoffile(fp);
    printf("\nSize of the file is: &#37;ld",size);
    printf("\nEntr the path of copy file:");
    gets(temp);
    fw=fopen(temp,"wb");
    copyoffile(fp,size);
    fclose(fw);
    fclose(fp);
    return(0);
    }
    
    void copyoffile(FILE *fp,long int size)
    {
        fread(&buffer,size,1,fp);
        fwrite(&buffer,size,1,fw);
    }
    
    
    long sizeoffile(FILE *fp)
    {
       long curpos, length;
       curpos = ftell(fp);
       fseek(fp, 0L, SEEK_END);
       length = ftell(fp);
       fseek(fp, curpos, SEEK_SET);
       return(length);
    }
    sorry...I defined name and buffer as global variables, I used gets to scan the input,that is path of the file to be copied as in (c:\documents\file1.txt)..changed void main to int..still it's not working...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    You should change them to local variables and pass appropriate arguments to functions instead. You still need to indent your main. The biggest issue here is that you are reading into pointer variables with first allocating memory, so you're on undefined territory here.
    Use local buffers on the stack instead, like
    Code:
    char name[100];
    Buffer needs to be allocated using malloc with appropriate size.
    You still need to substitute gets with fgets.
    And the end of the program, you can use getchar() to prevent the window from closing.
    Code:
    fgets(name, sizeof(name), stdin);
    Code:
    buffer = malloc(size);
    Code:
    free(buffer);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    sorry, if i m questioning a lot...have never used memory allocation functions, read a little about it. what prototype should i use for malloc?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by edesign View Post
    sorry, if i m questioning a lot...have never used memory allocation functions, read a little about it. what prototype should i use for malloc?
    You should include <stdlib.h> [or if you use a REALLY old compiler, <malloc.h>].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    malloc problem is now resolved but still no luck to copy image files, it actually creates the file with .bmp extension but empty..
    another thing..I think "gets" is right because i guess fgets is to read from a file and I am not reading contents of the file, but user input

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think "gets" is right
    gets is never right. It's impossible to make gets safe, so just go ahead and forget it exists.

    >because i guess fgets is to read from a file and I am not
    >reading contents of the file, but user input
    fgets can read from "user input", ie. stdin, as well as files:
    Code:
    fgets ( buffer, sizeof buffer, stdin ); // safer gets
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    ok..i tried that also,actually that part of program works absolutely fine.. can anyone answer why can't it copy any other type of files than .txt?? (can copy .doc but not if pictures/wordart included)

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What size are those files? Do you check the malloc for a NULL return value, e.g.
    Code:
    buffer = malloc(size);
    if (buffer == NULL) 
    { 
       printf("Could not allocate memory\n");
       exit(1);
    }
    If the file is big enough, you may not be able to allocate enough memory (particularly if you are using some old compiler, like Turbo C or such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    ok..i tried that also,actually that part of program works absolutely fine.. can anyone answer why can't it copy any other type of files than .txt?? (can copy .doc but not if pictures/wordart included)
    Office documents are a proprietary format that you will have to learn how to parse yourself if you intend to do it correctly. Microsoft does publish the ins and outs of their formats I believe, but its probably not worth it unless you already have Office's code base handy. You could try searching for it on the internet.

    Try sticking with plain text while you're learning to program. You can accomplish a great deal without having to employ more complex parsing algorithms. Also consider what some other experts may have to say about it, they might back me up.
    http://www.eskimo.com/~scs/cclass/int/sx3.html

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    .doc file which i tried to copy is of 24576 bytes and .bmp is of 66614 bytes.
    I checked buffer is not equal to NULL...

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what compiler do you use? I don't see why it wouldn't work. Try checking the amount you get back from fread() and fwrite(), e.g:
    Code:
    int readBytes;
    int writtenBytes;
    
    readBytes = fread(...);
    if (readBytes != size) 
       printf("read %d bytes, expected %d bytes\n", readBytes, size);
    
    writtenBytes = fwrite(...)
    if (writtenBytes != size)
       printf("written %d bytes, expected %d bytes\n", readBytes, size);
    
    // ... means the arguments you provided before.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 9 123456789 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. fwrite / fread question
    By Takteek in forum C Programming
    Replies: 5
    Last Post: 11-27-2008, 04:18 AM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

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