You should use memcpy instead, as strcpy will only copy until the first '\0'-character. This works badly for binary files. The real question is why you want to copy the contents of the file in memory though... You could just write out the original buffer to a new file.
Up vote 0 down vote favorite share g+ share fb share tw.
I have a question with strcpy() function. What I am trying to do is the user enters a file name and I basicly open the file, gets contents and create copy of file. However, I decided to do some error checking to check if the content read() is the same as content written in copy file before writing it.
So, I read() content into dynamic array using the file size of file read, so buffer is the right size for data. I then want to create a copy of that into another dynamic buffer, and use the strcmp() to see if they the same, if so then I write the copy buffer to the output file? This works fine for certain files but problem with video files(mpeg) etc, when opening a video file get error 'Could not determine type of stream', heres the idea char* buffer1 = malloc(filessize); char* buffer2 = malloc(filessize); read(file, buffer1, filesize); strcpy(buffer2, buffer1); //copy buffer1 into buffer2 if(strcmp(buffer1, buffer2) == 0) { write(outputfile, buffer2, filesize); //write copied buffer to file } free(buffer1); free(buffer2); Well the reason why I created another copy of buffer in memory is so I can compare the actual bit data, not just size, so I know that the data being written is the same as data being read?
C file io strcpy link|improve this question edited Jan 11 '11 at 13:33 asked Jan 11 '11 at 13:15daza166225211 82% accept rate.
Str* functions are designed to deal with null-terminated (text) strings, which file contents are not. For example, if there's '\0' character in read file, then it will not be fully copied (unless it is last character), but if there no '\0' you'll get undefined behaviour (even crash or strange messages or...). Look at memcpy, memcmp and similar functions first.
– Vovanium Jan 11 '11 at 13:26.
– daza166 Jan 11 '11 at 13:26 It doesn't make sense to check the buffer against the same buffer you read. If they're not the same, you have a bug in your program (or your hardware is broken), and trying to detect your own bugs is going to be rather challenging. The checks the program you wrote should perform is 1.
The return value of read(). You want to detect EOF or any errors, you want to check whether you've actually read the whole file. 2.
The return value of write, you want to check whether you actually wrote the whole file. – nos Jan 11 '11 at 13:34 If you need to verify that the data written is the same as the original file, you need to read back the new file and compare with the old one. If you compare before you write the data, chances are something fouls up when writing and you won't detect it anyways.
– harald Jan 11 '11 at 14:30.
Strcpy and strcmp are for C strings, which are 0 terminated. If your video files have any byte with a value of 0, they will stop right there. You should look at memcpy and memcmp instead, which won't interpret your buffers as 0 terminated strings.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.