473,692 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading files word by word

74 New Member
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 15115
1,275 Recognized Expert Top Contributor
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. }
Apr 1 '08 #2
74 New Member
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. }
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.         }
  16.         return 0;
  17. }
It reads the file and outputs one word on a new line, like this:

Apr 1 '08 #3
9,208 Recognized Expert Moderator Expert
if(myfile.is_op en())
while(! myfile.eof())
myfile >> word;
cout << word << endl;
// getline(myfile, word);
// cout << word << endl;
// getline(myfile, line);
// cout << line << endl;

return 0;

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

I'm sorry, but you are reading the file word by word. Just do what it says in Post #2.
Apr 1 '08 #4
74 New Member
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;
  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. };
  13. struct anylist
  14. {
  15.     char anyword[30];
  16.     int anycount;
  17.     anylist *anynext;
  18. };
  20. dlist *dhead = NULL;
  21. dlist *dpos;
  22. anylist *anyhead = NULL;
  23. anylist *anypos;
  25. void insert();
  26. void display();
  28. int main()
  29. {
  30.     dhead = NULL;
  31.     anyhead = NULL;
  32.     insert();
  33.     display();
  35.     cout << "test";
  37.     return 0;
  38. }
  40. void insert()
  41. {
  42. //    head = NULL;
  43.     dlist *dtemp, *dtemp2;
  44.     anylist *anytemp, *anytemp2;
  45.     string file, word;
  47.     dtemp = new dlist;
  48.     anytemp = new anylist;
  50.     cout << "\nEnter a file to scan: ";
  51.     cin >> file;
  53.     if(file != "dtext.txt")
  55.         cerr << "\nInvalid file.\n" << endl;
  57.     ifstream myfile("dtext.txt");
  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;
  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.                 }
  82.                 if(dhead == NULL)
  83.                 {
  84.                     dhead = dtemp;
  85.                     dpos = dhead;
  86.                 }
  87.                 else
  88.                 {
  89.                     dtemp2 = dhead;
  91.                     while(dtemp2->dnext != NULL)
  92.                         dtemp2 = dtemp2->dnext;
  93.                     dtemp2->dnext = dtemp;
  94.                 }
  95.             myfile.close();
  96.             }
  97.         }
  98.     }
  100.     return;
  101. }
  103. void display()
  104. {
  105.     dlist *dtemp;
  106.     anylist *anytemp;
  108.     dtemp = dhead;
  109.     anytemp = anyhead;
  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;
  119.             dtemp = dtemp->dnext;
  120.         }
  121.     }
  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;
  131.             anytemp = anytemp->anynext;
  132.         }
  133.     }
  134. }    
Apr 3 '08 #5
1,275 Recognized Expert Top Contributor
What is the use of this line

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

Apr 4 '08 #6

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

Similar topics

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 formatting as .RTF so it can be included. I can do this in Word VBA, but I would prefer to do it in straight VB (either VB6 or .Net). Thanks for any help on this.
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 for files that contain certian strings and if the files found to have these string do not already have a filename extension (such as '.doc' or '.xls') the function will append that to the files and rename them. So, if a file named 'report' was...
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 "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
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 fileStream. The problem is that when reading back the data word by word and printing the results on a general form, once the data chunk header has been read, i.e, "data" and "chunk size", all the rest of the bytes in the array have the value...
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, underline) etc. So , how can i make programme which allows me to read (RTF) Rich Text Format files or Microsoft WORD files in VB.NET through FILE HANDLING. Plz help me
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 switch/case to take action based on that value in the cookie. I want to set the cookie manually, so I don't need setcookie(). I see that in PHP it stores the cookie in memory. Is there a way to have PHP just read a cookie from the desktop machine?...
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 CString object for specific substrings. The problem I am having is reading the Word file itself. I have tried numerous things but no luck. Also, all the sources I found on the net are for older versions of VC++ and MS Word. I am using Visual...
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> #include <string> using namespace std; int main () {
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 when i run it, it complain, and i don`t know why??? can anyone help me have a look at it? here is the message it complain
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 dialog box appears that says "There was an error opening the file". when i try to open the excel files, a dialog box appears that says "File format not valid". I have tried to create a new word and excel files, save them and when i try to open these...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.