Reading string from vector until whitespace in C++ [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is probably easy but im not sure how. I tried searching multiple websites and yes Google and couldn't find anything on this.
My vector result[0] looks like this
A3 * * B4 * *
Declaration
vector<string> result = v.formVectorFile("Prj3 Config.txt");
I know that cin reads until whitespace so I was trying to use this to figure it out.
If I read straight from fstream I can read until whitespace, but im trying to do this with a string inside a vector and something like result[0] >> s; obviously doesnt work.
I need to read until it hits a whitespace then read the next one until whitespace. Etc...
So extract A3 by itself. Operate on it then extract * etc...

Your question is unclear because you don't tell us precisely what result is.
If we can assume that result is a std::vector<std::string>, then you can do something like this:
std::istringstream iss(result[0]); // consider only first string in vector
std::string item;
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}
If we assume that result is std::vector<char>, then you can do this:
std::string s(result.begin(), result.end()); // consider entire vector as single string
std::istringstream iss(s);
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}

Related

Compare strings word by word C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have two vectors containing strings. I want to compare each string of vector1 with each string of vector2 and check how many words are the same in both strings. The code I have only works if the two strings are perfectly similar :
Compare::Compare(vector<string> text1, vector<string> text2, int ratio)
{
text1Size_ = text1.size();
text2Size_ = text2.size();
if(text1Size_ > text2Size_)
{
totalWords_ = text1Size_;
}
else
{
totalWords_ = text2Size_;
}
it = text1.begin();
for(int i = 0; i < text1Size_; i++)
{
it2 = text2.begin();
for(int i = 0; i < text2Size_; i++)
{
if(*it == *it2)
{
cout << "Perfect match";
}
it2++;
}
it++;
}
}
I need to return each similar string if they have at least the ratio of similar words.
Is there a easier way than to parse each string, put each word in an array and compare them?
-EDIT-
By word I mean a written word like "bird". I'll give an example.
Let says I only have one string per vector and I need a 70% ratio of similarities:
string1 : The blue bird.
string2 : The bird.
What I want to do is to check if there is at least 60% of the written words that match in both sentences.
Here I have "The" and "Bird" that match. So I have 2/3 similar words (66.666%). So theses strings will be accepted.
-EDIT 2-
I don't think I can use ".compare()" here since it will check each character and not each written word...
Use a string stream to split a string into words:
#include <sstream>
bool is_similar(string str1, string str2)
{
vector<string> words1, words2;
string temp;
// Convert the first string to a list of words
std::stringstream stringstream1(str1);
while (stringstream1 >> temp)
words1.push_back(temp);
// Convert the second string to a list of words
std::stringstream stringstream2(str2);
while (stringstream2 >> temp)
words2.push_back(temp);
int num_of_identical_words = 0;
// Now, use the code you already have to count identical words
...
double ratio = (double)num_of_identical_words / words2.size();
return ratio > 0.6;
}

characters and files in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to make a program where the filename is composed of two parts: the first one is fixed and the second one can change during the program e.g "fixpart_integer.dat". I tried to do this in C++ but I did not succeed. The fisrt probelm is: how can i convert a number to a char ; and how can i concanate these 2 characters ; and how to declare this final char in the right way in order to open this filename ?
Many questions but I did find an easy way to do this.
#include <string>
#include <sstream>
std::string make_filename(std::string prefix, int id) {
std::stringstream ss;
ss << prefix << "_" << id << ".dat";
return ss.str();
}
and then I convert the string to char.
if you are working with c you may try the function described in here itoa
else if you are working woth c++ you may try something like this:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

Saving many images with its dynamic name [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to save images captured from camera just like
"D:\storage\img1" then I press "s" another time and program should save
"D:\storage\img2" and then
"D:\storage\img3"
so everytime I press a custom key it will save an image with different name.
How to do that?
Thanks for all your responses
This code concatenates (adds) the value of int i to string filename. This is done through IntToStr() . And int i loops from 0 to 20 and thus creating your "dynamic name".
File output :
PhotoImage0.txt
PhotoImage1.txt
PhotoImage2.txt
..
..
PhotoImage19.txt
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string IntToStr(int n)
{
stringstream result;
result << n;
return result.str();
}
int main ()
{
ofstream PhotoImageFile;
int Number_of_files=20;
string filename;
for (int i=0;i<Number_of_files;i++)
{
filename="c:\\PhotoImage" + IntToStr(i) +".txt";
cout<< filename << " \n";
PhotoImageFile.open(filename.c_str());
PhotoImageFile << filename<<" : Writing this to a file.\n";
PhotoImageFile.close();
}
return 0;
}
On startup, iterate the folder with an 'img*.*' mask - how you do this is up to your file system API. Use string functions or a loop to extract the part of the filename representing the number and convert it to an int. Every time you need to save a file, add 1 to the int, convert back to number-string & then concat the path, "img", number-string and extension to assemble the new file spec.

c++ textfile borland [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I ask about the function that seek in the K-th line in a text file and the one that read the text file by line or by character in C++! knowing that i'm working with borland.
fpeek is an open source application that does exactly that. Check the sources and see how its done.
I took a quick look and I believe you'll end up with something like this (I haven't tested this code):
std::ifstream file(filename);
std::string line;
int pos = 1;
while (std::getline(file, line))
{
// Find if current line should be displayed
if (15 == pos) // looking for the 15th line in the file
{
std::cout << pos << ": " << line << std::endl;
}
pos++;
}

Problems with garbage characters when reading file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm having trouble reading data from a file, and concatenating selected parts of the data (text) into a buffer of my own.
The code is like follows:
char buffer[1000];
char* allNewData = (char *)malloc(10000);
while (! myfile.eof() )
{
myfile.getline (buffer, 1000);
pch = strstr (buffer,"bla bla");
if(pch == NULL)
{
char* temp = buffer;
strcat(allNewData, temp);
strcat(allNewData, "\n");
}
else
{
strcat(allNewData, "here's bla bla");
strcat(allNewData, "\n");
}
}
cout<<allNewData<<endl;
When I run the program, allNewData first has some garbage text, followed by the proper/expected results, like this:
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii <-rubbish data
hello <- actual data
I need to get rid of this rubbish data, how can I change the code to achieve this?
You need to clear your newly allocated buffer before using string concatenation functions. They expect a valid string, to be able to find the end and thus the start of where to concatenate.
Use:
allNewData[0] = '\0';
this makes allNewData into an empty string. Do this before the loop, before you start concatenating all the found data.
Also, your code needs to better take care of the various "gotchas" when it comes to I/O and handling memory:
Don't check for EOF before doing a read access.
Check that the read was successful, before using the results of the read.
Make sure you don't exceed the capacity of your buffer when storing data.
Some comments, which you may find helpful or disregard:
What if there is a line longer than 1000 characters? (and say, that 1001-1008 is 'blah blah')? The line will be split into two in your new file and there will be an extra line before "here's blah blah"? Is this now a bug or desired functionality?
What if the line is longer than 1000, but "blah" is 996-1000 and the second "blah" is on the second segment - now you've lost one
What if your file is longer than 10000 characters?
They may sound like trivial questions, but answering them correctly will mean that you'll have to change your approach, I suggest purer C++ approach:
ifstream f_in(<file>);
ostringstream s_out;
string line;
while(f_in.good())
{
getline(f_in, line); // global getline function in <string>
if (line.find("blah blah") != string::npos)
{
s_out << "here's blah blah" << endl;
}
else
{
s_out << line << endl;
}
}
This way you don't have to worry about any of the questions above...
You can also use a combination of getline and ignore
Again... you have to check that your IO operation don't fail and eof() should be used only after a failed IO operation.