Dealing with files seems pretty awkward and complicated to me and also inconsistent in the results.
With the following code:
#include <stdio.h>
#include <string.h>
Code:
int main()
{
FILE *fp;
char string[40];
int length;
length= strlen(string);
fp = fopen("teamresults.txt", "r");
fgets(string,40,fp);
printf("%d %s \n",length,string);
fclose (fp);
}
I get different results in the length of the string in my text file. I have gotten 26, 44 and 73. The text file is merely "ManchesterUtd01Reading", not in quotes though, which is 22 characters.
Apparently with fgets(), it reads a specified number of characters from the file, or until a new line is reached, or until end of file. Well, I asked for 40 but there were only 22 characters, so why did it not stop at 22 and count 22?
Ultimately what I am trying to do is create a league table, and I am just playing around at the moment. The way I see it is that I'll have to count how many characters there are on the first line, store this and use it in my loop to loop through the characters 1 by 1 until a number is reached, store that string in array as team 1, then loop again looking for numbers and store this in a score array, then loop again from after the score to read in the second team to another array. I don't know what after that because at the moment I can't even count the correct number of characters on the first line.