0

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?

daza166
  • 3,543
  • 10
  • 35
  • 41
  • have you tried opening the video files in binary mode? – programmer Jan 11 '11 at 13:20
  • short sanity check: Why copy the file content in memory? There is absolutely no need for this, as the file content can be direclty written with the contents of the read buffer – king_nak Jan 11 '11 at 13:25
  • Are you sure you doing everyhing right? `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

2 Answers2

6

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.

pgb
  • 24,813
  • 12
  • 83
  • 113
1

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.

harald
  • 5,976
  • 1
  • 24
  • 41
  • well if I wrote the original buffer to file how would I check against it, ie the buffer written to file is the same as the orignal data read from file? – 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