I have a text file with ManchesterUtd01Reading, I open it for reading, count the number of characters on the first line and use it in a loop, the loop will insert a single character in an array called team while the character is not a number. Then I try to print out what is in array team.
I only get gobble, like
Team 1 is: ܃����ȧ��I��g����(���u���0���(���u���
Is this because the characters are not going in to the array, so it is printing something random from memory? I am just printing to the terminal in Ubuntu and my text file is in Gedit.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char string[40]; /*This is to store the first line of the text file as a string */
int length; /* This is to store the length of the string above to use in a loop */
char team1[20];
char team2[20];
char score[2];
char c; /* Store single character from file, used in a loop */
fp = fopen("teamresults.txt", "r");
fgets(string,40,fp); /* Read up to 40 characters from text file and put in string array */
length= strlen(string); /* Count how many characters are in the text file up to 40 or new line or EOF */
c = fgetc( fp ) ; /* fgetc function gets a character from file specified by what fp points to and stores it in c */
while ( c != EOF )/* while not at the end of the file, get a character from the file */
{
int i;
for (i=0;length;++i) {
c = fgetc( fp ) ;
while ( c >64 && c <123) { /* while character from text file is a non-number character */
team1[i]=c;
}
}
}
printf("Team 1 is: %s",team1);
fclose (fp);
}