I've been trying to open a file using the char** argv parameter. But unfortunately, I stumped upon a problem reading the path to the file when I pass it in this format: program.exe Function SourceFile DestFile.
I'm using notepad++ to write the code and GCC to compile and pass the arguments to the function
UPDATE: I fixed the code, should be working right now...
#include <stdio.h>
#include <string.h>
void textCopy(FILE* sourceFile, FILE* destinationFile);
void binaryCopy(FILE* sourceFile, FILE* destinationFile);
int main(int argc, char** argv)
{
printf(argv[2]);
if ((strcmp(argv[1], "textCopy") != 0 && strcmp(argv[1], "binaryCopy") != 0))
{
printf("Error: Function Doesn't Exist");
return 1;
}
FILE* sourceFile = fopen(argv[2], "r");
if (sourceFile == NULL)
{
printf("Error: Source File Doesn't Exist");
return 1;
}
FILE* destinationFile = fopen(argv[3], "w");
if (destinationFile == NULL)
{
printf("Error: Destination File Doesn't Exist");
}
if (strcmp(argv[1], "textCopy") == 0)
{
textCopy(sourceFile, destinationFile);
}
else
{
binaryCopy(sourceFile, destinationFile);
}
getchar();
return 0;
}
void textCopy(FILE* sourceFile, FILE* destinationFile)
{
char letter = 0;
while (letter != EOF)
{
letter = fgetc(sourceFile);
fputc(letter, destinationFile);
}
}
void binaryCopy(FILE* sourceFile, FILE* destinationFile)
{
printf("Ignore");
}
I searched for solutions in the internet but nothing seemed to work out, when i read argv[2] i only get the C:\ part from the path and not the whole path...
Thanks!
Try something in the spirit of:
program.exe textCopy "C:\path to\the source file\my source file.ext" "C:\path to\the destination file\my destination file.ext"
It's the double quotes around the file path in the command line you're missing.