Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a program that takes in one integer and two strings from a text file "./myprog < text.txt" but I want it to be able to do this using command line arguments without the "<", like "./myprog text.txt" where the text file has 3 input values.
3 <- integer
AAAAAA <- string1
AAAAAA <- string2
If the reading of the parameters must be done solely from the file having it's name, the idiomatic way is, I would say, to use getline().
std::ifstream ifs("text.txt");
if (!ifs)
std::cerr << "couldn't open text.txt for reading\n";
std::string line;
std::getline(ifs, line);
int integer = std::stoi(line);
std::getline(ifs, line);
std::string string1 = line;
std::getline(ifs, line);
std::string string2 = line;
Because there are little lines in your file, we can allow ourselves some repetition. But as it becomes larger, you might need to read them into a vector:
std::vector<std::string> arguments;
std::string line;
while (getline(ifs, line))
arguments.push_back(line);
There some optimizations possible, as reusing the line buffer in the first example and using std::move(), but they are omitted for clarity.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I would like to parse a sentence beginning with a number:
2 random sentece.
5 another one.
8 this is really long sentence.
Into int number holding the beginning number and the rest in std::string sentence. So in the first line, the parsing output will be number == 2 and sentence == "random sentence". The input is read from stdin, but the classical std::cin >> number >> sentence does'n work, since the parsing of string would end once it reaches a space. But I want to make the string beginning after the initial number to the end of line \n. So, how to do it in C++?
You can make use of std::getline and std::istringstream as shown below. In particular in the given program, std::getline is used to read line by line and std::istringstream is used to read first the integer and then the remaining sentence.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to print out the number entered by the user as a whole and want to ignore the spaces.
Like this:
int aValue;
cin >> aValue;
Here, suppose the user enters 49 506, I want to print it as 49506.
First you need to get a string from a user with spaces, note you need to use std::getline() for that, as operator>> would not accept spaces:
std::string str;
std::getline( cin, str );
then you use std::remove_if() together with std::isspace() to remove spaces from your string:
auto it = std::remove_if( str.begin(), str.end(), []( unsigned char c ) { return std::isspace(c); } );
str.erase( it, str.end() );
and then you convert your string to an int using std::stoi():
auto aValue = std::stoi( str );
you should also add error checking to your code handling error conditions as described in documentation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My .txt file is like this:
question1 answer1
question2 answer2
question3 answer3
How can I place question1 and answer1 into two separate variables? I can use getLine(), but it will return the question and answer.
If each question is terminated with a question mark then you can write
std::string line;
while ( std::getline( FileStream, line ) )
{
std::istringstream is( line );
std::string question;
std::string answer;
std::getline( is, question, '?' );
question += '?';
std::getline( is, answer );
// some processing of question and answer
}
If there is used some other separator then you need to substitute the question mark for this separator and maybe to remove line
question += '?';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse the following file so it takes the item as a string then skip the # sign and then take the price as a float.
text file:
hammer#9.95
saw#20.15
shovel#35.40
how would I go about doing this?
In case when you have std::string in presented format you could use something like this:
std::string test {"test#5.23"};
std::cout << std::stof(std::string{test.begin() + test.rfind('#') + 1, test.end()});
Note that std::stof is C++11 function
Read the file line by line into a string. Find # and parse second part as float.
std::ifstream file("input.txt");
for (std::string line; std::getline(file, line); )
{
auto sharp = line.find('#'); // std::size_t sharp = ...
if (sharp != std::string::npos)
{
std::string name(line, 0, sharp);
line.erase(0, sharp+1);
float price = std::stof(line);
std::cout << name << " " << price << "\n";
}
}
Note: I didn't some error checking, do them yourself as an exercise. and also you should know about std::string, std::ifstream, std::getline and std::stof.