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??
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 ...
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??
Make sure you open in binary mode.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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); }
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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...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: %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); }
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
Buffer needs to be allocated using malloc with appropriate size.Code:char name[100];
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);
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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?
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
>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.
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)
What size are those files? Do you check the malloc for a NULL return value, e.g.
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).Code:buffer = malloc(size); if (buffer == NULL) { printf("Could not allocate memory\n"); exit(1); }
--
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.
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.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)
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
.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...
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.