copying contents of text file defined at runtime into an array
Hi Guys,
Having an issue with using command line arguments and data manipulation.
The program I am trying to write allows user to specify input and output file at runtime e.g
./suchthefool -input.txt -output.txt
This is working fine and handling errors, but I need to copy the contents of the input file into a string or a 1d array without knowing the size of the input file.
I have been searching for a few days and not found anything. If i define the inputfile in the code rather than at runtime i can do it but not when using argc *argv[]
e.g. char inputstring[] = "input.txt";
I'm going to leave out my error handling for the command line aguments, will post them if any of you think I might be going wrong in there..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fptr1, *fptr2;
int c;
fptr1 = fopen (argv[1], "r");
fptr2 = fopen (argv[2], "w");
char inputstring[] = "char *argv[1]";
//////////////////////////////////////////////////////////
// strcpy(inputstring, argv[1]);
// Tried this and it didnt work either
/////////////////////////////////////////////////////////
printf("%s\n\nThis is the string\n\n", inputstring);
//////////////////////////////////////////////////////////
// debug copy from input to output without string
// while((c = getc(fptr1)) != EOF)
// {
// putc(c, fptr2);
// putc(c, stdout);
// }
//////////////////////////////////////////////////////////
// code to print inputfile to screen for debugging
// char x;
//
// while ((x = fgetc(fptr1)) != EOF)
// {
// printf("%c", x);
// }
//////////////////////////////////////////////////////////
fclose(fptr1);
fclose(fptr2);
return 0;
}
I am wanting the contents of input.txt to be copied into
Code:
//pseudo
char inputContents[sizeof/strlen(argv[1])];
There are so many tutorials on google etc that show me how to do something like
Code:
char inputContents[] = "input.txt";
but this doesn't let me specify the input file when I run the program
Any help much appreciated!
Ben