Question:
C program that counts numbers from a file?
bioteacher
2010-11-30 16:15:17 UTC
I know how to write a program that opens a file

what if there are numbers in the file
and I want the program to count how many are divisible by 2 and how many are divisible by 3?
I am only using stdio.h
I know the logic, I just don't know the parts of the program. Do I use an index? counter?

i do something like
if (number % 3) ==0)
index ++

or count_of_multiples_of_3 ++
I'm unfamiliar with the language
????
Three answers:
?
2010-11-30 16:38:19 UTC
...

#include



void get_numbers(void)

{

FILE *f = fopen("numbers.txt", "rt");

int iThree = 0, iTwo = 0, iInput = 0;

if (f) {

/* This line reads in your number!!! */

while(fscanf(f, "%d \n", &iInput) == 1){

if (iInput % 2 == 0) {

++iTwo;

}

else if (iInput % 3 == 0) {

++iThree;

}

// this is some debug output, you may or may not be interested in!

// fprintf(stdout, "%d ", iInput);

}

fclose(f);

f = NULL;

fprintf(stdout, "\nThere were %d number divisible by 2 and\nthere were %d numbers divisible by 3\n", iTwo, iThree);

}

}

int main()

{

get_numbers();

return 0;

}



/* The numbers.txt file was looking like this:

After the 20 there was a new line */

1 3 5 7 9 11 13 15 17 19 21

23 2 4 6 8 10 12 14 16 18 20
?
2016-05-31 09:20:49 UTC
Awesooooome!!! That's the most inefficient implementation ever! Given your implementation, you don't need to read anything to count the characters, just use fseek() to go to the end of the file, and then get the position using ftell(). BTW, conio.h is a non-standard header, you should avoid it, as it's only available on some DOS and Windows SDKs, use getchar() instead of getch(). The other problem on your program is that you're not reading anything, you're passing the pointer infile to countchar, so it's reading from a random memory area... try some other compiler+IDE, that correctly reports the errors on your code, I recommend Code::Blocks + MinGW, or go straight to GNU/Linux where are the best development tools. For counting the words/lines, you could optimize it by reading by 4/8 bytes (depending on if you're on x86 or x86_64) until the end of the string, where you must previously write zeros until it gets aligned (after the read). Since x86 and x86_64 are little-endian architectures, you just compare the low 8bits, shift by 8bits, compare again... that's all, it's a lot faster :). Also you could use XOR to detect the spaces on the whole register, that would improve the performance a bit further, or even use specialized SIMD instructions (but that's going too far XD). I can't understand why everybody talks about "1 byte = 1 char", specially when nobody uses 7bit ASCII or any 8bit encodings anymore. This implementation is not compatible with UTF-8, to make it compatible you must check the lenght of every char.
Richard S
2010-11-30 16:18:11 UTC
if(Num % 2 == 0)

{

// It's even

}

else

{

// It's odd

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...