Reading a text file in C

Associate
Joined
22 Feb 2004
Posts
2,417
Location
Essex
trying to read a text file in C, should be simple, but its proving to be hard :(

the text file has multiple lines, which is possibly where im going wrong, keep getting Stream != NULL errors...


Does anyone have a quick bit of code to read a text file :)

Code:
int ReadINI() {
	char string;

	FILE *fp;
	fp = fopen("Countries.ini", "r");
	if (!fp) {
		printf("Error opening file");
		return 0;
	}
	while (!feof(fp)) {
		fscanf(fp,"%s",string);
		printf("%s",string);
	}
	fclose(fp);
	return 0;
}


edit: defined the size of my string, and all is well.

now it is interpreting spaces as line breaks :(
 
Last edited:
ok, new problem ;)

say i have several structures

player1
player2
player3
player4
...

is there a way to loop through each somehow

for (a=0;a<b;a++) {
playera.score = 0;
}
 
Swanster said:
Exactly the same way as you would a normal array, but use the structure name as the type. eg "<struct name> player[3]"
then access the elements as "player[0].score" ... "player[3].score"


oh, i tried that and it wasn't having it


:edit: ah... forgot a * :o

cheers for your help m8 :D
 
Last edited:
ok, so i've made an array of structures and an array of pointers pointing to the structures.

What i want to do is pass the array of pointers into a new function and then, in the new function, alter the properties of each structure... not sure how to do this though

heres what i have so far:

Code:
typedef struct country {
	char *name;
	int border[8];
	int armies;
	int owner;
} Country;

LoadCountries(int total, Country *co);

Country *newcountry() {
  Country *p = malloc(sizeof *p);
  return p;
}

int main(void) {
	int a;
	int total = totalcountries();
	Country *country;

	country = malloc(total * sizeof *country);

	for (a=0;a<total;a++) {
		country[a] = *newcountry();
	}
	LoadCountries(total,&country);
	return 0;
}

LoadCountries(int total, Country *co) {
	co[1].armies = 6;
}
 
Last edited:
thanks for your help guys

pretty much got the whole game sorted now (game of risk, if you're interested)

i've got all the attacking/defending/moving sorted, just one thing still bugs me.

i can't seem to read the name of the country into the structure:

Code:
typedef struct country {
	char *name;
	int border[10];
	int armies;
	int owner;
} Country;

int main(void) {
	int total = totalcountries();
	Country *country;
	country = malloc((total + 1 )* sizeof *country); //using + 1 as country ID starts from 1, not 0
	LoadCountries(total,country);
	return 0;
}

now, the LoadCountries function takes the ini file (here) and parses it to the structure. however, pch2 (using strtok) is meant to return the name of the current country. it does when used in a printf() statement, and if it is read back immediatly after it is assigned to co[n].name returns the current name.

However, if i try to access the name outside of the function, i get nothing. if i set the name to be "something" then i can read it outside of the function
Code:
int LoadCountries(int total, Country *co) {
	int i = 0;
	int border;
	int currentcountry;
	char *pch;
	char *pch2;

	char chrst[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
	int flag_countries = 0;
	int flag_continents = 0;
	char string[255];

	FILE *fp;
	fp = fopen("Countries.ini", "r");
	if (!fp) {
		printf("Error opening file");
	}
	
	while (!feof(fp)) {
		fscanf(fp,"%s",&string);
		
		if (strstr(string,"[Countries]") != NULL) {
			flag_countries = 1;
			flag_continents = 0;
		}
		if (strstr(string,"[Continents]") != NULL) {
			flag_countries = 0;
			flag_continents = 1;
		}

		if (flag_countries == 1) {
			pch = strtok (string,",");
			while (pch != NULL) {
				border = atoi(pch);
				if ((border != 0) && (!strstr(pch,"="))) {
					co[currentcountry].border[i] = border;
					i++;
				}
				if (strstr(pch,"=")) {
					currentcountry = border;
					pch2 = strpbrk(pch,chrst);

					
					co[currentcountry].name = pch2;
					
					i = 0;
				}
				pch = strtok(NULL, ",");
			 }
		}
	}
	fclose(fp);
	return 0;
}


entire source found here
 
wow, thank you for your detailed reply, really appreciate it :)

this is the first thing i've done in C, and its been an experience :p

Will try those suggestions, and let you know how i get on
 
Back
Top Bottom