Good evening! My goal right now is just making something small to read out the characters in a file of any type (and put them into a string for use later in the program) but I keep currently running into an issue where when I run the code and it segmentation faults at the line "input[n] = (char)c;" and I've tried troubleshooting by printing the characters and the value of n, and every time (despite me changing my malloc to different sizes) the printf statement will get halfway through the number "134510" or will print the character at offset 134509 before faulting at the line below. I would like to know what my issue is and how to fix it, as it's weird to me that the program is only able to get through about 10% of the file.
Thank you!!
int c = 0; //Counting the current character being read
int n = 0; //Counting the current character being written
char *input; //A string of all characters in the file
FILE *inputFile; //File to read
if(!(inputFile = fopen(argv[1],"r"))){ // Open in read mode
printf("Could not open file"); // Print and exit if file open error
return 1;
}
input = (char *)malloc(sizeof(inputFile) * sizeof(char));
while (1){ //Put all of the characters from the file into the string
c = fgetc(inputFile);
if(feof(inputFile)){ //If it reaches the end of the file, break the loop
break;
}
printf("%c ", c); //Troubleshooting
input[n] = (char)c;
n++;
}