By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,407 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,407 IT Pros & Developers. It's quick & easy.

Reading files word by word

P: 74
I can't figure out how to read a file word by word. I want to open the file and then read it one word at a time. For example, I'm looking for words that start with d. How would I scan each word of the file to see if it starts with the letter d?
Apr 1 '08 #1
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
I can't figure out how to read a file word by word. I want to open the file and then read it one word at a time. For example, I'm looking for words that start with d. How would I scan each word of the file to see if it starts with the letter d?
After reading the word check whether the position 0 of the string contains d .
If it is then its the word you are lokking for or else skip it.
Expand|Select|Wrap|Line Numbers
  1. char arr[] = "defghi";
  2. if(arr[0] == 'd')
  3. {
  4.  /*This word starts with d*/
  5. }
  6. else
  7. {
  8. /*skip it*/
  9. }
  10.  
Raghuram
Apr 1 '08 #2

P: 74
After reading the word check whether the position 0 of the string contains d .
If it is then its the word you are lokking for or else skip it.
Expand|Select|Wrap|Line Numbers
  1. char arr[] = "defghi";
  2. if(arr[0] == 'd')
  3. {
  4.  /*This word starts with d*/
  5. }
  6. else
  7. {
  8. /*skip it*/
  9. }
  10.  
Raghuram
How would I get the word though? I don't know how to get one word at a time. I only know how to use getline a little bit.

Here's the code I have:

Expand|Select|Wrap|Line Numbers
  1.                 if(myfile.is_open())
  2.                 {
  3.                         while(! myfile.eof())
  4.                         {
  5.                                 myfile >> word;
  6.                                 cout << word << endl;
  7. //                              getline(myfile, word);
  8. //                              cout << word  << endl;
  9. //                              getline(myfile,line);
  10. //                              cout << line << endl;
  11.                         }
  12.                         myfile.close();
  13.                 }
  14.         }
  15.  
  16.         return 0;
  17. }
  18.  
It reads the file and outputs one word on a new line, like this:

hello.
my
name
is
joe.
Apr 1 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
if(myfile.is_open())
{
while(! myfile.eof())
{
myfile >> word;
cout << word << endl;
// getline(myfile, word);
// cout << word << endl;
// getline(myfile,line);
// cout << line << endl;
}
myfile.close();
}
}

return 0;
}


It reads the file and outputs one word on a new line, like this:

hello.
my
name
is
joe.
I'm sorry, but you are reading the file word by word. Just do what it says in Post #2.
Apr 1 '08 #4

P: 74
I wrote more code. I added two linked lists I need and tried to open the file, read a word at a time, and insert it into the linked list. I don't think I did it right though. When I run the program, it asks the user to input a filename like it's supposed to and accepts the response, but after that, nothing happens. The program just keeps running without doing anything. Feedback of what I did wrong and what I have to do to fix it would be appreciated. Thanks.

Here's my new code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct dlist
  7. {
  8.     char dword[30];        //holds a word up to 30 characters
  9.     int dcount;        //number of times a word appears
  10.     dlist *dnext;        //pointer to next node
  11. };
  12.  
  13. struct anylist
  14. {
  15.     char anyword[30];
  16.     int anycount;
  17.     anylist *anynext;
  18. };
  19.  
  20. dlist *dhead = NULL;
  21. dlist *dpos;
  22. anylist *anyhead = NULL;
  23. anylist *anypos;
  24.  
  25. void insert();
  26. void display();
  27.  
  28. int main()
  29. {
  30.     dhead = NULL;
  31.     anyhead = NULL;
  32.     insert();
  33.     display();
  34.  
  35.     cout << "test";
  36.  
  37.     return 0;
  38. }
  39.  
  40. void insert()
  41. {
  42. //    head = NULL;
  43.     dlist *dtemp, *dtemp2;
  44.     anylist *anytemp, *anytemp2;
  45.     string file, word;
  46.  
  47.     dtemp = new dlist;
  48.     anytemp = new anylist;
  49.  
  50.     cout << "\nEnter a file to scan: ";
  51.     cin >> file;
  52.  
  53.     if(file != "dtext.txt")
  54.  
  55.         cerr << "\nInvalid file.\n" << endl;
  56.  
  57.     ifstream myfile("dtext.txt");
  58.  
  59.     while(file == "dtext.txt")
  60.     {
  61.         if(myfile.is_open())
  62.         {
  63.         cout << "file open test";
  64.             while(! myfile.eof())
  65.             {
  66.                 myfile >> word;
  67.  
  68.                 if(word[0] == 'd')
  69.                 {
  70. //                    cout << word << endl;
  71.                     dtemp->dword;
  72. //                    dcount++;
  73.                     dtemp->dnext = NULL;
  74.                 }
  75.                 else
  76.                 {
  77.                     anytemp->anyword;
  78. //                    anycount++;
  79.                     anytemp->anynext = NULL;
  80.                 }
  81.  
  82.                 if(dhead == NULL)
  83.                 {
  84.                     dhead = dtemp;
  85.                     dpos = dhead;
  86.                 }
  87.                 else
  88.                 {
  89.                     dtemp2 = dhead;
  90.  
  91.                     while(dtemp2->dnext != NULL)
  92.                         dtemp2 = dtemp2->dnext;
  93.                     dtemp2->dnext = dtemp;
  94.                 }
  95.             myfile.close();
  96.             }
  97.         }
  98.     }
  99.  
  100.     return;
  101. }
  102.  
  103. void display()
  104. {
  105.     dlist *dtemp;
  106.     anylist *anytemp;
  107.  
  108.     dtemp = dhead;
  109.     anytemp = anyhead;
  110.  
  111.     if(dtemp == NULL)
  112.         cout << "The list is empty!" << endl;
  113.     else
  114.     {
  115.         while(dtemp != NULL)
  116.         {
  117.             cout << "D List: " << dtemp->dword << endl;
  118.  
  119.             dtemp = dtemp->dnext;
  120.         }
  121.     }
  122.  
  123.     if(anytemp == NULL)
  124.         cout << "The any list is empty" << endl;
  125.     else
  126.     {
  127.         while(anytemp != NULL)
  128.         {
  129.             cout << "Any List: " << anytemp->anyword << endl;
  130.  
  131.             anytemp = anytemp->anynext;
  132.         }
  133.     }
  134. }    
  135.  
Apr 3 '08 #5

gpraghuram
Expert 100+
P: 1,275
What is the use of this line

Expand|Select|Wrap|Line Numbers
  1.     while(file == "dtext.txt")
  2.  
This will loop continuously.
Change to if loop or else remove it.


Raghuram
Apr 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.