Reading strings from a file
I'm quite new to C and I'm a wee bit stuck here and would be very grateful for any help or advice you guys could share.
Im trying to open a text file and read a string into an array but I don't know if my syntax is wrong or it's just bad programming. Heres the code so far:
Code:
/* Include Header Files */
#include <stdio.h>
#include <stdlib.h>
/* Define Symboli constants */
/* Type Declerations */
typedef struct
{
char name[25];
char country[25];
int age;
float finalScore;
float judgesScore[5];
}gymnast;
/* Function Prototypes */
double GetReal(void);
int GetInteger(void);
/* Global Variables */
FILE *fptr; //Defines a file pointer
/* Main Funtion */
void main(void)
{
int i;
char fileName[30];
gymnast competitors[15];
puts("Please enter the file name with the data");
gets(fileName);
fptr = fopen("fileName", "r"); //Opens the data file that was inputd
if( fptr == 0)
{
puts("An error occured when opening the file ");
exit(1); //Cheacks the file is valid
}
for(i = 0; i <=15; i++)
{
competitors[i].name = fgets(fptr);
puts(competitors[i].name);
}
Please help I'm getting really sick of my own silly mistakes and lack of knowledge :confused:
Cheers Gav