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?
5 15055
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. -
char arr[] = "defghi";
-
if(arr[0] == 'd')
-
{
-
/*This word starts with d*/
-
}
-
else
-
{
-
/*skip it*/
-
}
-
Raghuram
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. -
char arr[] = "defghi";
-
if(arr[0] == 'd')
-
{
-
/*This word starts with d*/
-
}
-
else
-
{
-
/*skip it*/
-
}
-
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: - 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.
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.
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:
What is the use of this line -
while(file == "dtext.txt")
-
This will loop continuously.
Change to if loop or else remove it.
Raghuram
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |