473,289 Members | 1,896 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,289 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 15033
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.