When I try to copy one file over to another file, character by character, a new output file is created but nothing copied over. Does anyone know what I am doing wrong?
Code:
fopen_s(&filein, argv[i], "rb"); //open source file
sourceLength = strlen(argv[i]) + 1;
strcpy_s(sourceFilename, 100, argv[i]);
//Manipulate source filename by removing the extension
for (int i = sourceLength; i >= 0; i--) //starting from
{
if (sourceFilename[i] == '.')
{
sourceFilename[i] = 0; //append null byte
break;
}
}
strcat_s(sourceFilename, 100, ".bak"); //concatenate
fopen_s(&fileout, sourceFilename, "wb"); //open output
if (fileout == NULL)
{
printf("Cannot open target file."); //output error
fclose(fileout);
exit(1);
}
while (ch != EOF)
{
ch = getchar(); //read character from source file
if (ch == EOF)
{
break; //break out of while loop if EOF is
}
else
{
putchar(ch); //wirte character from source file
}
}