473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reading files word by word

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
5 15055
gpraghuram
1,275 Expert 1GB
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
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
9,208 Expert Mod 8TB
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
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
1,275 Expert 1GB
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

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

Similar topics

4
by: David Erickson | last post by:
I need to read some (about 15,000) word documents and add some wrappers around them for importing into a database. What is the best way to read a ..doc file in VB? I would like to save the...
1
by: hokiegal99 | last post by:
This is not really a Python-centric question, however, I am using Python to solve this problem (as of now) so I thought it appropiate to pose the question here. I have some functions that search...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
4
by: Erpman | last post by:
I am trying to access the data with in a wav file. I am testing with very small files in order to keep the code simple to start with. Basically, im writing the entire wav file to a byte using a...
2
by: Eshban Bahadur | last post by:
Hello, I want to read (RTF) files in my VB.NET programme. How can i do it. I apply the same method of reading text files, but it does not save the formatting of text like (bold, italics,...
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
3
by: ahammad | last post by:
Well, the title pretty much describes what I want to do. I want to be able to read the contents of a Word document (*.doc). I also want to be able to read it to a CString object, and then search that...
17
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream>...
1
by: shrimpy | last post by:
hi every one, i am new to python, and coz i want to write a handy command for my linux machine, to find a word in all the files which are under the current folder. the code is half done, but...
1
navanova
by: navanova | last post by:
Greetings, I have a problem of opening ms word and excel files on my computer. The files are there for a long time. I use to open and modify them. Suddenly, when i try to open the word files, a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.