Opening a file and then printing the output?
Hello, I'm trying to write a program that prompts the user to enter a filename, open that filename (3 paragraph story) and then print it out as it's shown in the input file. Theres a few parts that make it a little more difficult than that. I'm fine with prompting the user, storing the filename in a string, opening the file via the string name, and checking to make sure the file input is correct (code below).
Now, I need to scan through the input, determine how many characters it is and declare a string of that size, we aren't allowed to just declare a string of a set size we know would be big enough. I've heard the function malloc() will determine the overall length, and then I can just assign that to a variable, but I'm not too sure how the function works, I'm still messing with that. The other part I'm stumped on is how to print the output so it looks just like the input file. For the code I have, I just declared a string with a size of 500 to see if I could get it right, all of the text is just clumped toether, there are no spaces in between and it doesn't start a new line as is shown on the input file. Any tips, help, or suggestions would be greatly appreciated, thanks!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *in_file;
char dataFile[20];
char story[500];
int i;
printf("Enter the file you wish to open ");
scanf("%s", dataFile);
in_file = fopen(dataFile, "r");
if (in_file == NULL)
{
printf ("An Error occured while attempting to open the file\n");
exit (-1);
}
for (i = 0; i < 500; i++)
{
fscanf(in_file, "%s", &story[i]);
printf("%s", &story[i]);
}
fclose(in_file);
return 0;
}