#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep 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?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    checkout emacs dissociated press mode
    and Markov chains.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    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);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep 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
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    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.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    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
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    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.

IMN logo majestic logo threadwatch logo seochat tools logo