Hi all.
I have created the below code to copy text files, and would now like to write code to copy non-text files(pics, music, video).
I think that I need to use fread() and fwrite() for binary files. Something like:
Code:
while((fread(...)) != NULL)
fwrite(...)
But I don't know how to determine the size and n x size arguments for fread() and fwrite().
So I have some questions:
1. How do I know if a jpg or video file is made up of int or float etc?
2. Do I need code to detect the size of a file?
3. Can I use fseek() or feof() to detect the size of a file?
4. Do I actually need to use fread() & fwrite() or are there better functions for the job?
Thanks in advanced.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINELENGTH 100
#define FILENAMELENGTH 30
int main(void)
{
char file1[FILENAMELENGTH], file2[FILENAMELENGTH], line[MAXLINELENGTH];
FILE *openfile1, *openfile2;
printf("Enter file to copy: ");
fgets(file1, FILENAMELENGTH, stdin);
file1[strlen(file1) - 1] = '\0';
if((openfile1 = fopen(file1, "r")) != NULL)
printf("%s opened successfully\n", file1);
else
{
perror("error");
exit(1);
}
printf("Enter file to copy to: ");
fgets(file2, MAXLINELENGTH, stdin);
file2[strlen(file2) - 1] = '\0';
if((openfile2 = fopen(file2, "w")) != NULL)
printf("%s was copied successfully to %s\n", file1, file2);
else
{
perror("error");
exit(1);
}
while((fgets(line, MAXLINELENGTH, openfile1)) != NULL)
fputs(line, openfile2);
fclose(openfile1);
fclose(openfile2);
return 0;
}