C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2008, 02:49 PM   #1
Registered User
 
master_vst's Avatar
 
Join Date: Nov 2008
Posts: 4
Question need help with 'character counting from a .txt file'

Hi,

I am new here and I have a problem with my latest program writing-attempt.
I hope someone corrects what I have done wrong..

The question is that I was writing a message on a online text-based game and I realized that they allowed max 20000 characters to send.

Inspired from this I wanted to make program that counts the chracters (up to 25000) of whatever I wrote (or copy-pasted) in a certain .txt file.

Then I programmed this:
-----------------------------------------------------------
Code:
#include <stdio.h>

int main ()
{
	FILE *body;
	char content[25000];
	long c = 0;
	long int s;

	/* First, I get the textbody into a flow named 'body'. */
	/* Then I get the contents into an array named 'content'. */

	body = fopen("C:\\Documents and Settings\\USER\\Desktop\\char_counter\\body.txt", "r");
	fgets(content, 25000, body);
	
	while (content[c] != EOF) /* Then I increase c every time at 1 before the EndOfFile to get the number of the characters. */
	{
	c = c + 1;
	} 
	printf("number of all characters = %ld\n\n", c);
	
 	scanf("%lf", &s);  /* I put this here because somehow console closes itself and I can't read anything.*/
	return 0;
}
-----------------------------------------------------------

Then I wrote "master" in the file and expected to see '6' but I got '19748' !
I thought that windows adds something at the end of my file so that I cant take the true value. I changed the program into this and tried to manually decrease that value into expected values:
-----------------------------------------------------------
Code:
#include <stdio.h>

int main ()
{
	FILE *body;
	char content[44742]; /* 25k + stupid */
	long stupid = 19742; /* stupid number added by windows (I think) */
	long c = 0;
	long b = 0;
	long int s;
	/* First, I get the textbody into a flow named 'body'. */
	/* Then I get the contents into an array named 'content'. */
	body = fopen("C:\\Documents and Settings\\USER\\Desktop\\char_counter\\body.txt", "r");
	fgets(content, 44742, body);
	
	while (content[c] != EOF) /* Then I increase c every time at 1 before the EndOfFile. */
	{
	c = c + 1;
	} 
	c = c - stupid; /* When there are 19742 characters more than the expected value then I just decrease them to reach the expected value.. */
	printf("number of all characters = %ld\n\n", c);
	
	while (b < c) /* I am trying to see which characters are these '19742' characters.. :S */
	{
	printf("%c", content[b]);
	b = b + 1;
	}
	
 	scanf("%lf", &s); /* I put this here because somehow console closes itself and I can't read anything.*/
	return 0;
}
-----------------------------------------------------------

The result was not better .

"number of all characters = 19750"
"master "
" "
" "
(It goes down like more than 250 - 300 lines.. I couldnt count :S..)




I hope I am not boring anyone with these codes and stuff please help me...
I need help to correct it. I have made lots of searches but I still keep getting lost..
I really want this thing work..

(I am using Dev-C++ on Windows XP. and notebook for .txt )

thanks,
volkan
(master_vst)

Last edited by master_vst; 11-09-2008 at 02:21 PM.
master_vst is offline   Reply With Quote
Old 11-08-2008, 03:05 PM   #2
Registered User
 
Join Date: Oct 2008
Posts: 60
fgets() reads from the file into a null-terminated string. The EOF is not stored. EOF is not even actually a character. It is an int so that its bit-pattern can be distinguished from all chars. The upshot of all this is that you need to check for a 0 instead of EOF.
nucleon is offline   Reply With Quote
Old 11-08-2008, 03:18 PM   #3
Registered User
 
master_vst's Avatar
 
Join Date: Nov 2008
Posts: 4
Thank you very much!

It worked..

It is funny that it was such a stupid mistake (instead of stupid number )..
master_vst is offline   Reply With Quote
Old 11-08-2008, 03:28 PM   #4
Registered User
 
master_vst's Avatar
 
Join Date: Nov 2008
Posts: 4
And I have another problem about this now..
It worked only for simple texts..

I actually wanted it for testing the forum-type texts where there are codes used..
like ([/color]), ( [ u ] [ / u ]) or I use these (------------------------------------------------------------) to make head lines look better..

And when my program encounters with these it cant continue counting and just gives me the number untill these and finishes..

How can I make them countable?
master_vst is offline   Reply With Quote
Old 11-08-2008, 03:36 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
I suspect you're only calling fgets() once, which means that you only read one line from the file. This is surely not what you want.

If you're trying to read a file in line by line, and process each line individually, a common idiom goes something like this:
Code:
FILE *fp = /* ... */;
char buffer[SIZE];
while(fgets(buffer, sizeof(buffer), fp)) {
    /* do something with buffer */
}
In your case, it would be quite simple to count the characters in each line yourself. Something like this, which could be made shorter if you know about for loops:
Code:
int x = 0;
while(buffer[x] != 0) {
    characters ++;
    x ++;
}
However, you're really just over-complicating the problem by reading in each line at a time. You could just read from the file character by character and be done with it.
Code:
FILE *fp = /* ... */;
int count = 0;

int c = getc(fp);
while(c != EOF) {
    count ++;
    c = getc(fp);
}

printf("There are %d characters in the file.\n", count);
Something like that.

(I've tried to write my code such that you should be able to understand it. Hopefully. )
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, etc.

New project: nort
dwks is offline   Reply With Quote
Old 11-09-2008, 02:17 PM   #6
Registered User
 
master_vst's Avatar
 
Join Date: Nov 2008
Posts: 4
Ohh, I didnt knew that 'fgets()' reads only one line..

I knew I made it over-complicated but I am new at programming and I was just trying to use what I already know..

Thanks for the examples..

I will try it with your version.. It looks easier.. and simple..
master_vst is offline   Reply With Quote
Reply

Tags
.txt, character, counting, file, notepad

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File transfer- the file sometimes not full transferred shu_fei86 C# Programming 13 03-13-2009 12:44 PM
opening empty file causes access violation trevordunstan C Programming 10 10-21-2008 11:19 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
read from .txt file & put into array? slow brain C Programming 6 02-25-2003 05:16 AM


All times are GMT -6. The time now is 10:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22