• 0

C - Reading numbers from txt file


Question

Hi. Im currently doing my assignment, but Im stuck with something. I need to read data from a txt file and store them in two different arrays, which I did. The txt file contains names and numbers. The problem is, I have to make a function that adds the numbers together to find the total, but the numbers aren't the type int, how can I convert them?

My current code:

#include <stdio.h>
#include <stdlib.h>


FILE *f;

int total = 0;
char city[300];
int population[300];

int total_population(int pop[300]){
	 int i=0;
	 int count = 0;

	 //for( i = 0; i < sizeof(pop); i++) {
	 while(pop[i]!='\0'){
		  count += pop[i];
		  i++;
	  }

	 printf("%d", count);
}

main(){
	 f = fopen("stats-population.txt", "r");   

	while(!feof(f)){	 
		 fscanf(f, "%s %s", city, population);
		 printf("%s %s\n", city, population);
	}

	total = total_population(population);

	exit(0);
}

Thx

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0
Hint: %s and population in your fscanf. What does %s do?

I'v tried with %d for population but it doesn't work... its give all the cities the same population number(this nr. doesn't even exit in the txt file).. :s

Link to comment
Share on other sites

  • 0
atoi(string) returns the string as an integer I believe. Maybe give that a go?

How can I use it on a array... I tried some different ways now, but can't it to work.. :s

Link to comment
Share on other sites

  • 0

OK, a couple of points.

You're currently declaring city to be an array of 300 elements but what you actually want is a two dimensional array so that each city can be referenced individually.

For example, say I declare city to be:

char city[20][50];

This means that I can store a total of 20 citys, each with a maximum length of 50 characters.

So so city[0] could be "London", city[1] could be "Edinburgh" etc

As someone else said in the thread you want to use %d rather than %s as you want to store an integer, rather than an array of characters.

I made a few changes and I think it's now working:

#include <stdio.h>
#include <stdlib.h>

FILE *f;

int total = 0;
char city [20][50];
int population[50];

int total_population(int pop[50]){
	 int i=0;
	 int count = 0;

	 //for( i = 0; i < sizeof(pop); i++) {
	 while(pop[i]!='\0'){
		  count += pop[i];
		  i++;
	  }

	 printf("%d", count);
	 return count;
}

int main()
{
	int i = 0;
	f = fopen("test.txt", "r");  

	while(!feof(f)){	
		 fscanf(f, "%s %d", &city[i][0], &population[i]);
		 printf("%s %d\n", city[i], population[i]);
		 i++;
	}

	total = total_population(population);

	return 0;
}

I tested it with this text file and it returns the correct value of 805600

Edinburgh 5000
Glasgow 600
London 800000

Finally, I'm not sure how you're compiling your code but you should always enable the -Wall (all warnings) compiler option. It'll show up quite a few common errors which might leave you wondering why your program isn't working / crashing :)

Link to comment
Share on other sites

  • 0
OK, a couple of points.

You're currently declaring city to be an array of 300 elements but what you actually want is a two dimensional array so that each city can be referenced individually.

For example, say I declare city to be:

char city[20][50];

This means that I can store a total of 20 citys, each with a maximum length of 50 characters.

So so city[0] could be "London", city[1] could be "Edinburgh" etc

As someone else said in the thread you want to use %d rather than %s as you want to store an integer, rather than an array of characters.

I made a few changes and I think it's now working:

#include <stdio.h>
#include <stdlib.h>

FILE *f;

int total = 0;
char city [20][50];
int population[50];

int total_population(int pop[50]){
	 int i=0;
	 int count = 0;

	 //for( i = 0; i < sizeof(pop); i++) {
	 while(pop[i]!='\0'){
		  count += pop[i];
		  i++;
	  }

	 printf("%d", count);
	 return count;
}

int main()
{
	int i = 0;
	f = fopen("test.txt", "r");  

	while(!feof(f)){	
		 fscanf(f, "%s %d", &city[i][0], &population[i]);
		 printf("%s %d\n", city[i], population[i]);
		 i++;
	}

	total = total_population(population);

	return 0;
}

I tested it with this text file and it returns the correct value of 805600

Edinburgh 5000
Glasgow 600
London 800000

Finally, I'm not sure how you're compiling your code but you should always enable the -Wall (all warnings) compiler option. It'll show up quite a few common errors which might leave you wondering why your program isn't working / crashing :)

Thx alot man :)... I now understand the parts you have changed and its working :)... didn't know about the -Wall, thx for tip :)

Link to comment
Share on other sites

  • 0
Thx alot man :) ... I now understand the parts you have changed and its working :) ... didn't know about the -Wall, thx for tip :)

Cool glad it helped. There are plenty of other warnings you can enable too, but -Wall catches quite a lot of things.

Link to comment
Share on other sites

  • 0
Cool glad it helped. There are plenty of other warnings you can enable too, but -Wall catches quite a lot of things.

:) One litte question

fscanf(f, "%s %d", &city[i][0], &population[i]);

city[0] <= why is the last one zero?

Link to comment
Share on other sites

  • 0
:) One litte question

fscanf(f, "%s %d", &amp;city[i][0], &amp;population[i]);

city[0] <= why is the last one zero?

Here's an example. Say I declare a two-dimensional array city[5][10] so we have 5 rows (0 - 4) and 10 columns ( 0 - 9)

|	|	0	1	2	3	4	5	6	7	8	9
---------------------------------------------------------
0	|	L	o	n	d	o	n

1	|	C	a	i	r	o

2	|	T	o	k	y	o

3	|	M	a	d	r	i	d

4	|	B	a	r	c	e	l	o	n	a

Each time we increment i, we move down a row. so the first row we read london and store it in the array. Then we increment i, and move down a row. Next we read Cairo, and then repeat. The [0] is because we want to begin storing the city name in the first element of the new row :)

Now if you compare this to the population array. We're only storing one number for each row and the number will fit within one element (integer).

Link to comment
Share on other sites

  • 0
Here's an example. Say I declare a two-dimensional array city[5][10] so we have 5 rows (0 - 4) and 10 columns ( 0 - 9)

|	|	0	1	2	3	4	5	6	7	8	9
---------------------------------------------------------
0	|	L	o	n	d	o	n

1	|	C	a	i	r	o

2	|	T	o	k	y	o

3	|	M	a	d	r	i	d

4	|	B	a	r	c	e	l	o	n	a

Each time we increment i, we move down a row. so the first row we read london and store it in the array. Then we increment i, and move down a row. Next we read Cairo, and then repeat. The [0] is because we want to begin storing the city name in the first element of the new row :)

Now if you compare this to the population array. We're only storing one number for each row and the number will fit within one element (integer).

it gives sense now... thx alot :D

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.