I'm still a noobie to C as well. Correct me on anything if I'm wrong guys! Only been doing this since January. My past 4 or so assignments have been dealing with files and plenty more.I'm not sure of another way than dynamic memory allocation to get a different size for the filename. If you're not worried about that just put a reasonable size in the brackets for filename.
I researched fgets and not quite sure what it does or how to use it
fgets - C++ Reference fgets has 3 parts and looks like this
Code:
fgets(string, number_of_characters_read, pointer_to_file)
it uses the pointer to the file to store that line into your character of strings (which in the code I put is "buffer"). The middle part is the number of characters it reads in that string (which is why I always just put sizeof(buffer)).Here's what I came up with for your code. Worked on my computer. But like I said, I'm still new. I'd take any advice from the people above me first!
Code:
#include <stdio.h>#include <stdlib.h>
int main()
{
FILE *filep;
char filename[50]; //Defines up to how many characters in the filename
char buffer[50]; //Stores the data in each line in buffer
float x, y;
printf("Please enter name of file to be arranged and stored\n");
scanf("%s",&filename);
//Opens the file
if ( (filep = fopen(filename, "r")) == NULL )
{
printf("Couldn't open file\n");
exit(1);
}
//Reads the file line by line
while( fgets(buffer, sizeof(buffer), filep))
{
sscanf(buffer,"%f %f", &x, &y);
printf("The first value is %f\nThe second value is %f\n",x,y);
getchar();
}
fclose(filep);
return (0);
}