C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 4th, 2012, 05:01 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation Power: 0
How to get certain word length from text file

okay im trying to get a word from a text file the text file words are examples like this

like
tore
care
gone

im trying to just get one word at a time for a little game im making from it so here is part of what my code looks like

Code:
int i;
	char words[10];
	FILE *File;
	File=fopen("wordfile.txt","r");
	for(i=0;i<10;i++){
	fscanf(File," %c\n",&words[i]);
	}

	for(i=0;i<14;i++){
	printf("%c",words[i]);
	}


but when my program prints the words it will print the word like but also part of the other words so it becomes liketoreca something like it

how do i just make it so it gets one word at a time so if it picks the word it stops when it gets to the last letter

i was guessing something like char words[length] something like it
I'd appreciate your help
Comments on this post
Lux Perpetua disagrees: Ever heard of punctuation?

Reply With Quote
  #2  
Old November 4th, 2012, 05:05 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
checkout emacs dissociated press mode
and Markov chains.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 4th, 2012, 05:17 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
The conversion specifier "%c" reads a single character from the file. That's what you do in a loop.

Yiu may want to change your program and use the conversion specifier "%s" which reads a bunch of characters in one go.

Code:
	int i;
	char words[10];
	FILE *File;
	File = fopen("wordfile.txt", "r");

	for (i = 0; i < 4; i++) {
		fscanf(File, "%9s", words); /* read a whole word (9 chars or less) */
		printf("%s\n", words); /* print the whole word */
	}

	fclose(File);

Reply With Quote
  #4  
Old November 4th, 2012, 05:28 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
The conversion specifier "%c" reads a single character from the file. That's what you do in a loop.

Yiu may want to change your program and use the conversion specifier "%s" which reads a bunch of characters in one go.

Code:
	int i;
	char words[10];
	FILE *File;
	File = fopen("wordfile.txt", "r");

	for (i = 0; i < 4; i++) {
		fscanf(File, "%9s", words); /* read a whole word (9 chars or less) */
		printf("%s\n", words); /* print the whole word */
	}

	fclose(File);


do i still use char for the type im not familar with c i know in c++ i think you just use string but with c im not sure
ill play around with it thanks

Reply With Quote
  #5  
Old November 4th, 2012, 05:34 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation Power: 0
ok thanks it worked the for loop though printed all the words
but i took it out and it read the first word which is what i wanted thank you so much

Reply With Quote
  #6  
Old November 4th, 2012, 05:54 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
You need to look at the input files you're using and determine what different characters will be used to separate your words, e.g. space, carriage return, line feed etc.

Then look up the ascii values for these characters. Read characters similar to how you're already doing, but stop reading into the array after reading one of those characters, and store the number of valid characters read.

If you want you could print the array similar to how you're doing but stop after printing the number of valid characters you've previously stored.

Alternatively, you could null terminate the array by assigning 0 to the character after the last valid character you read. So suppose i has the index of the last valid character read but not the last character in the array (if it is then you need a bigger array), then do:

words[i+1] = 0;

This will allow you to print the word simply like this:

printf("%s\n", words);

instead of writing code which explicitly loops through your array and prints it out one character at a time.

Reply With Quote
  #7  
Old November 4th, 2012, 06:07 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
The conversion specifier "%c" reads a single character from the file. That's what you do in a loop.

Yiu may want to change your program and use the conversion specifier "%s" which reads a bunch of characters in one go.

Code:
	int i;
	char words[10];
	FILE *File;
	File = fopen("wordfile.txt", "r");

	for (i = 0; i < 4; i++) {
		fscanf(File, "%9s", words); /* read a whole word (9 chars or less) */
		printf("%s\n", words); /* print the whole word */
	}

	fclose(File);


do i have to specify the lenght of char words to 10 char words[10]
or is there a way to make it end at the letter without specifying the lenght like is there some way like char words[lengthofword]; or char words[endatlastletter] without having to specify the length

Reply With Quote
  #8  
Old November 4th, 2012, 06:34 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
You could make a dynamically allocated array and resize it if you encounter larger words.

A quick google search of "c dynamic array" will turn up a wealth of knowledge which will help you toward that end.

Reply With Quote
  #9  
Old November 5th, 2012, 03:45 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by program57
do i have to specify the lenght of char words to 10 char words[10]
or is there a way to make it end at the letter without specifying the lenght like is there some way like char words[lengthofword]; or char words[endatlastletter] without having to specify the length


There is no "easy" way to do that in C.

You either use a constant size (like 10 above; but you can specify a size large enough for all intended purposes; maybe 20000) or dynamic memory (you have to manually manage allocations and deallocations).

If you have a C99 implementation (kinda like anything but Microsoft's Visual Studio) you have another option: use VLA (Variable Length Arrays). They work just like arrays with a constant size except that the size is determined at run time.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to get certain word length from text file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap