String in a text file containing a string in C++ - c++

here's a part from my code
string word;
cin >> word;
string keyword;
while (file >> keyword && keyword != word){}
This searches for a word in a file and if it finds that word (keyword) then it starts a string from there later. It's working perfectly at the moment. My problem is that when the line is
"Julia","2 Om.","KA","1 Om. 4:2"
if I enter word Julia I can not find it and use it for my purposes (just FYI I'm counting it). It works if I search for "Julia","2 since this is where space comes in.
I'd like to know how can I change line
while (file >> keyword && keyword != word){}
so I can see when the text/string CONTAINS that string since at the moment it only finds and accepts it if I enter the WHOLE string perfectly.
EDIT: Also what I have found this far is only strstr, strtok, strcmp. But these fit more with printf than with cout.

You can use methods from std::string like find.
#include <string>
#include <iostream>
// ...
std::string keyword;
std::string word;
getline(file, keyword);
do
{
std::cin >> word;
}
while (keyword.find(word) == std::string::npos);

The problem is that you're extracting strings, which by default will extract up until the next space. So at the first iteration, keyword is "Julia","2. If you want to extract everything separated by commas, I suggest using std::getline with , as the delimeter:
while (std::getline(file, keyword, ','))
This will look through all of the quoted strings. Now you can use std::string::find to determine if the input word is found within that quoted string:
while (std::getline(file, keyword, ',') &&
keyword.find(word) == std::string::npos)
Now this will loop through each quoted string until it gets to the one that contains word.

Use this method of istream to get a whole line instead of just a single "word":
http://www.cplusplus.com/reference/istream/istream/getline/
Then use strstr, to find the location of a string (like Julia) in a string (the line of the file):
http://www.cplusplus.com/reference/cstring/strstr/

Related

What types of indicators are there for the end of a string when tokenizing a sentence?

I am trying to take a string holding a sentence and break it up by words to add to a linked list class called wordList.
When dealing with strings in C++, what is the indicator that you have reached the end of a string? Searched here are found that c strings are null terminated and some are indicated with a '\0' but these solutions give me errors.
I know there are other ways to do this (like scanning through individual characters) but I am fuzzy on how to implement.
void lineScan( string line) // Adds words to wordList from line of a file
{
istringstream iss(line);
string lineWord;
getline(iss, lineWord, ' ');
wrds.addWords( lineWord );
while( lineWord!= NULL )
{
getline(iss, lineWord, ' ');
wrds.addWords( lineWord );
}
}
You probably want to skip all whitespace, not use a single space as separator (your code will read empty tokens).
But you're not really dealing with strings here, and in particular not with C strings.
Since you're using istringstream, you're looking for the end of a stream, and it works like all instreams.
void lineScan(string line) // Adds words to wordList from line of a file
{
istringstream iss(line);
string word;
while (iss >> word)
{
wrds.addWords(word);
}
}

c++ Ignore sections of string before or after reading a file

I am writing a program where I read a text file using getline() and store each line in a vector<string>.
ifstream flirfile(flir_time_dir);
vector<string> flir_times;
string flir_line;
while (getline(flirfile, flir_line))
{
flir_times.push_back(flir_line);
}
This is the text file that the program reads:
The program works fine but what I want to do is ignore everything on each line except for the hex string in the middle. So in other words ignore everything before the first underscore and after the second underscore (including the underscores). Is there an easy way to do that? And is it better to ignore the text while the file is being read or afterwards when the lines are stored in the vector? Thanks.
There are ways to split strings on separators, which means you can split the string on the '_' character, and use only the middle-part.
A simple way of doing this is to use std::getline and std::istringstream:
while (getline(flirfile, flir_line))
{
std::istringstream is{flir_line};
std::string dummy;
std::string flir;
std::getline(is, dummy, '_');
std::getline(is, flir, '_');
flir_times.push_back(flir);
}
If your compiler supports C++11 you can use regular expression:
#include <regex>
// ...
std::regex my_regex("_[^_]*_");
while (getline(flirfile, flir_line))
{
std::smatch my_match;
if (std::regex_search(flir_line, my_match, my_regex))
flir_times.push_back(my_match[0]);
}

Parsing a string in c++ using header file and cpp file

I am able to read my text file. Now i would like to parse the string line by line.
I am using header file and cpp file..
can anyone help me with parsing tutorial.
Where can find a good tutorial for parsing?
You can try http://www.cppreference.com/wiki/ and look at examples of using stringstreams.
I don't see what this has to do with header files, but here's how you parse a stream line by line:
void read_line(std::istream& is)
{
// read the lisn from is, for example: reading whitespace-delimited words:
std::string word;
while(is >> word)
process_word(word);
if( !is.eof() ) // some other error?
throw "Dude, you need better error handling!";
}
void read_file(std::istream& is)
{
for(;;)
{
std::string line;
if( !std::getline(is,line) )
break;
std::istringstream iss(line);
read_line(iss);
}
if( !is.eof() ) // some other error?
throw "Dude, you need better error handling!";
}
Try this:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fs("myFile.txt");
string input;
vector<string> sets;
while( getline(fs, input) )
sets.push_back(input);
}
First you need to know if the lines contain fixed length fields or are the fields variable length. Fixed length fields are usually padded with some character such as spaces or zeros. Variable length fields are usually terminated by a character such as a comma or tab.
Variable Length Fields
Use the std::string::find or std::string::find_first to find the ending character; also account for the end of the string as the last field may not contain the terminating character. Use this position to determine the length of the field (ending field position - starting field position). Finally, use std::string::substr to extract the field's content.
Fixed Length Fields
Use the std::string::substr method to extract the text. The starting and ending positions can be calculated using the accumulated lengths of the previous fields, if any, and the size of the current field.
Converting Field Text
The contents of the field may not be a string and will need to be converted to an internal data type. For example, a number. Use std::istringstream to convert the text of the field to an internal data type.

C++: Why does space always terminate a string when read?

Using type std::string to accept a sentence, for practice (I haven't worked with strings in C++ much) I'm checking if a character is a vowel or not. I got this:
for(i = 0; i <= analyse.length(); i++) {
if(analyse[i] == 'a' || analyse[i] == 'e' [..etc..]) {
...vowels++;
} else { ...
...consonants++;
}
This works fine if the string is all one word, but the second I add a space (IE: aeio aatest) it will only count the first block and count the space as a consonant, and quit reading the sentence (exiting the for loop or something).
Does a space count as no character == null? Or some oddity with std::string?, It would be helpful to know why that is happening!
EDIT:
I'm simply accepting the string through std::cin, such as:
std::string analyse = "";
std::cin >> analyse;
I'd guess you're reading your string with something like your_stream >> your_string;. Operator >> for strings is defined to work (about) the same as scanf's %s conversion, which reads up until it encounters whitespace -- therefore, operator>> does the same.
You can read an entire line of input instead with std::getline. You might also want to look at an answer I posted to a previous question (provides some alternatives to std::getline).
I can't tell from the code that you have pasted, but I'm going to go out on a limb and guess that you're reading into the string using the stream extraction operator (stream >> string).
The stream extraction operator stops when it encounters whitespace.
If this isn't what's going on, can you show us how you're populating your string, and what its contents are?
If I'm right, then you're going to want a different method of reading content into the string. std::getline() is probably the easiest method of reading from a file. It stops at newlines instead of at whitespace.
Edit based on edited question:
use this (doublecheck the syntax. I'm not in front of my compiler.):
std::getline(std::cin, analyze);
This ought to stop reading when you press "enter".
If you want to read in an entire line (including the blanks) then you should read using getline. Schematically it looks like this:
#include <string>
istream& std::getline( istream& is, string& s );
To read the whole line you do something like this:
string s;
getline( cin, s );
cout << "You entered " << s << endl;
PS: the word is "consonant", not "consenent".
The >> operator on an istream separates strings on whitespace. If you want to get a whole line, you can use readline(cin,destination_string).

How to read a word into a string ignoring a certain character

I am reading a text file which contains a word with a punctuation mark on it and I would like to read this word into a string without the punctuation marks.
For example, a word may be " Hello, "
I would like the string to get " Hello " (without the comma). How can I do that in C++ using ifstream libraries only.
Can I use the ignore function to ignore the last character?
Thank you in advance.
Try ifstream::get(Ch* p, streamsize n, Ch term).
An example:
char buffer[64];
std::cin.get(buffer, 64, ',');
// will read up to 64 characters until a ',' is found
// For the string "Hello," it would stream in "Hello"
If you need to be more robust than simply a comma, you'll need to post-process the string. The steps might be:
Read the stream into a string
Use string::find_first_of() to help "chunk" the words
Return the word as appropriate.
If I've misunderstood your question, please feel free to elaborate!
If you only want to ignore , then you can use getline.
const int MAX_LEN = 128;
ifstream file("data.txt");
char buffer[MAX_LEN];
while(file.getline(buffer,MAX_LEN,','))
{
cout<<buffer;
}
EDIT: This uses std::string and does away with MAX_LEN
ifstream file("data.txt");
string string_buffer;
while(getline(file,string_buffer,','))
{
cout<<string_buffer;
}
One way would be to use the Boost String Algorithms library. There are several "replace" functions that can be used to replace (or remove) specific characters or strings in strings.
You can also use the Boost Tokenizer library for splitting the string into words after you have removed the punctuation marks.