Discuss How to get certain word length from text file in the C Programming forum on Dev Shed. How to get certain word length from text file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 156
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);
Posts: 9
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
Posts: 149
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.
Posts: 9
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
Posts: 156
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.