Read Word by Word from File in C++

Reading from a file word by word

You can just use fscanf to read one word at a time:

void read_words (FILE *f) {
char x[1024];
/* assumes no word exceeds length of 1023 */
while (fscanf(f, " %1023s", x) == 1) {
puts(x);
}
}

If you don't know the maximum length of each word, you can use something similar to this answer to get the complete line, then use sscanf instead, using a buffer as large as the one created to read in the complete line. Or, you could use strtok to slice the read in line up into words.

Program to read words from a file and count their occurrence in the file

If you want to print every line contained in the file just once, you have to save the strings you have read in a given data structure. For example, a sorted array could do the trick. The code might look as follow:

#include <stddef.h>

size_t numberOfLine = getNumberOfLine (file);
char **previousStrings = allocArray (numberOfLine, maxStringSize);
size_t i;

for (i = 0; i < numberOfLine; i++)
{
char *currentString = readNextLine (file);

if (!containString (previousStrings, currentString))
{
printString (currentString);
insertString (previousStrings, currentString);
}
}

You may use binary search to code the functions containString and insertString in an efficient way. See here for further informations.

Reading word by word from text file in C

char *getWord(FILE *fp){
char word[100];
int ch, i=0;

while(EOF!=(ch=fgetc(fp)) && !isalpha(ch))
;//skip
if(ch == EOF)
return NULL;
do{
word[i++] = tolower(ch);
}while(EOF!=(ch=fgetc(fp)) && isalpha(ch));

word[i]='\0';
return strdup(word);
}
void read(FILE *fp){
char *word;
while(word=getWord(fp)){
insert(&tree, word);
}
//rewind(fp1);
fclose(fp);
}

read word by word from file in C++

First of all, don't loop while (!eof()), it will not work as you expect it to because the eofbit will not be set until after a failed read due to end of file.

Secondly, the normal input operator >> separates on whitespace and so can be used to read "words":

std::string word;
while (file >> word)
{
...
}

Reading and storing whole words from file to array

Currently you have space to store single string.

char arr[100]; -->char arr[100][100];

Then your reading changes as below.

while ((c = fgetc(inp)) != EOF) {
if (c == ' ' || c == '\n') {
printf("\n");

arr[word_count][char_count] = '\0'; //Terminate the string
char_count = 0; //Reset the counter.
word_count++;
}
else {
arr[word_count][char_count] = c;
printf("%c",arr[word_count][char_count]);

if (char_count < 99)
char_count++;
else
char_count = 0;
}
}

And your printing changes as below.

for(i = 0; i < word_count; i++) {
printf("%s",arr[i]);
}

Reading a text file word by word

The String lib provides an easy way of doing this: string.Split(): if you read the entire string in, C# can automatically split it on every space:

string[] words = reader.ReadToEnd().Split(' ');

The words array now contains all of the words in the file and you can do whatever you want with them.

Additionally, you may want to investigate the File.ReadAllText method in the System.IO namespace - it may make your life much easier for file imports to text.

Edit: I guess this assumes that your file is not abhorrently large; as long as the entire thing can be reasonably read into memory, this will work most easily. If you have gigabytes of data to read in, you'll probably want to shy away from this. I'd suggest using this approach though, if possible: it makes better use of the framework that you have at your disposal.



Related Topics



Leave a reply



Submit