I am practicing input-output, I read two vectors from files f1 and f2, they are shown in the figure.
I want to use the content of two files for the output file's name. So the code is
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
vector<double> f1Series;
vector<double> f2Series;
double tmp;
std::ifstream input;
input.open("Input/f1.txt");
while (input >> tmp) f1Series.push_back(tmp);
input.close();
input.open("Input/f2.txt");
while (input >> tmp) f2Series.push_back(tmp);
input.close();
std::ostringstream oss1;
std::ostringstream oss2;
oss1 << f1Series[1];
oss2 << f2Series[1];
std::string f1 = oss1.str();
std::string f2 = oss2.str();
std::ofstream st("Output/Results" + f1 + "_" + f2 + ".txt");
}
However, the current result is
Results1e07_0.001000.txt
What I want is
Results1e7_1e-3.txt
Try std::scientific
double c = 1.0e-10;
std::cout << std::scientific; //setting output format
std::cout << c;
The output will be
1.00000e-010
The printf way, easy to remember if you are used to C, but definitely C-ish and old fashioned:
char temp[16];
snprintf(temp, sizeof(temp), "Results%.0e.txt", f1Series[1]);
std::ostream st(temp);
The C++ way using scientific and setprecision:
std::ostringstream oss;
oss << "Results" <<std::scientific << std::setprecision(0) << f1Series[1] << ".txt";
std::string filename = oss.str();
Remember the lines you read as strings, not converted to double. You'll need to later convert to double when you are ready to use it as a number, but you are remembering the characters you actually scanned so you can reproduce that in the file name.
vector<string> f1Series;
vector<string> f2Series;
Related
I would like to get some advice on an issue I've encountered once working on small adjustments to an existing program.
The program itself has to:
Open a file and read it line-by-line preferably
Pack the lines into istringstream and then split to 2 strings on a ':' separator
Insert those 2 strings line1 and line2 into an existing std::map container
Than I can do more stuff with the map and the data from it.
My code looks like that:
int main()
{
FILE *fpFile;
map<string, string>mapOfPci;
std::string tempBuff="";
std::string line1="", line2="";
fpFile = fopen(PCI_MAPPING_PATH, "r");
if(!fpFile)
return false;
while(getline(fpFile, tempBuff)){
istringstream iSs(tempBuff);
iSs >> line1;
iSs.ignore(numeric_limits<std::streamsize>::max(), ':');
iSs >> line2;
mapOfPci.insert(make_pair(line1, line2));
}
for(const auto &m : mapOfPci){
cout << m.first << " : " << m.second << "\n";
}
fclose(fpFile);
return (0);
}
Now what I'm getting in my compiler feedback is:
mismatched types 'std::basic_istream<_CharT, _Traits>' and 'FILE* {aka _iobuf*}'
while(getline(fpFile, tempBuff))
At this point I presume that this is due to the usage of FILE* file handling method.
I might not be able to use the C++ std::ifstream, std::fstream, so is there any method to move this further with the current FILE* usage?
std::getline() expects an std::istream-derived class, like std::ifstream, so you simply can't pass your own FILE* to it (unless you wrap it inside of a custom std::streambuf-derived object assigned to a standard std::istream object. std::ifstream uses std::filebuf, which uses FILE* internally, but you can't supply it with your own FILE*).
Otherwise, you can use C's getline() function instead, but it doesn't work with std::string, as it allocates its own output char[] buffer which you will then have to free afterwards (you can assign the contents of that buffer to a std::string, though), eg:
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <limits>
#include <cstdio>
using namespace std;
int main()
{
FILE *fpFile = fopen(PCI_MAPPING_PATH, "r");
if (!fpFile)
return false;
map<string, string> mapOfPci;
char *tempBuff = nullptr;
size_t size = 0;
int nRead;
while ((nRead = getline(&tempBuff, &size, fpFile)) != -1){
istringstream iSs(string(tempBuff, nRead));
string line1, line2;
iSs >> line1;
iSs.ignore(numeric_limits<streamsize>::max(), ':');
iSs >> line2;
mapOfPci.insert(make_pair(line1, line2));
free(tempBuff); tempBuff = nullptr;
size = 0;
}
free(tempBuff);
for(const auto &m : mapOfPci){
cout << m.first << " : " << m.second << "\n";
}
fclose(fpFile);
return 0;
}
But, since you are using other C++ standard classes, there really is no good reason not to use std::ifstream instead, eg:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <limits>
using namespace std;
int main()
{
ifstream fpFile(PCI_MAPPING_PATH);
if (!fpFile.is_open())
return false;
map<string, string> mapOfPci;
string tempBuff;
while (getline(fpFile, tempBuff)){
istringstream iSs(tempBuff);
string line1, line2;
iSs >> line1;
iSs.ignore(numeric_limits<streamsize>::max(), ':');
iSs >> line2;
mapOfPci.insert(make_pair(line1, line2));
}
for(const auto &m : mapOfPci){
cout << m.first << " : " << m.second << "\n";
}
return 0;
}
I want to read a csv data to vector of struct in cpp, This is what I wrote, I want to store the iris dataset in pointer of struct vector csv std::vector<Csv> *csv = new std::vector<Csv>;
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
struct Csv{
float a;
float b;
float c;
float d;
std::string e;
};
int main(){
std::string colname;
// Iris csv dataset downloaded from
// https://gist.github.com/curran/a08a1080b88344b0c8a7
std::ifstream *myFile = new std::ifstream("iris.csv");
std::vector<Csv> *csv = new std::vector<Csv>;
std::string line;
// Read the column names
if(myFile->good())
{
// Extract the first line in the file
std::getline(*myFile, line);
// Create a stringstream from line
std::stringstream ss(line);
// Extract each column name
while(std::getline(ss, colname, ',')){
std::cout<<colname<<std::endl;
}
}
// Read data, line by line
while(std::getline(*myFile, line))
{
// Create a stringstream of the current line
std::stringstream ss(line);
}
return 0;
}
I dont know how to implement this part of the code which outputs line with both float and string.
// Read data, line by line
while(std::getline(*myFile, line))
{
// Create a stringstream of the current line
std::stringstream ss(line);
}
Evolution
We start with you program and complete it with your current programm style. Then we analyze your code and refactor it to a more C++ style solution. In the end we show a modern C++ solution using more OO methods.
First your completed code:
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
struct Csv {
float a;
float b;
float c;
float d;
std::string e;
};
int main() {
std::string colname;
// Iris csv dataset downloaded from
// https://gist.github.com/curran/a08a1080b88344b0c8a7
std::ifstream* myFile = new std::ifstream("r:\\iris.csv");
std::vector<Csv>* csv = new std::vector<Csv>;
std::string line;
// Read the column names
if (myFile->good())
{
// Extract the first line in the file
std::getline(*myFile, line);
// Create a stringstream from line
std::stringstream ss(line);
// Extract each column name
while (std::getline(ss, colname, ',')) {
std::cout << colname << std::endl;
}
}
// Read data, line by line
while (std::getline(*myFile, line))
{
// Create a stringstream of the current line
std::stringstream ss(line);
// Extract each column
std::string column;
std::vector<std::string> columns{};
while (std::getline(ss, column, ',')) {
columns.push_back(column);
}
// Convert
Csv csvTemp{};
csvTemp.a = std::stod(columns[0]);
csvTemp.b = std::stod(columns[1]);
csvTemp.c = std::stod(columns[2]);
csvTemp.d = std::stod(columns[3]);
csvTemp.e = columns[4];
// STore new row data
csv->push_back(csvTemp);
}
// Show everything
for (const Csv& row : *csv)
std::cout << row.a << '\t' << row.b << '\t' << row.c << '\t' << row.d << '\t' << row.e << '\n';
return 0;
}
The question that you have regarding the reading of the columns from your Csv file, can be answered like that:
You need a temporary vector. Then you use the std::getline function, to split the data in the std::istringstream and to copy the resulting substrings into the vector. After that, we use string conversion functions and assign the rsults in a temporary Csv struct variable. After all conversions have been done, we move the temporary into the resulting csv vector that holds all row data.
Analysis of the program.
First, and most important, in C++ we do not use raw pointers for owned memory. We should ven not use new in most case. If at all, std::unique_ptrand std::make_unique should be used.
But we do not need dynamic memory allocation on the heap at all. You can simply define the std::vector on the functions stack. Same like in your line std::string colname; you can also define the std::vector and the std::ifstream as a normal local variable. Like for example std::vector<Csv> csv{};. Only, if you pass this variable to another function, then use pointers, but smart pointers.
Next, if you open a file, like in std::ifstream myFile("r:\\iris.csv"); you do not need to test the file streams condition with if (myFile->good()). The std::fstreams bool operator is overwritten, to give you exactly this information. Please see here.
Now, next and most important.
The structure of your source file is well known. There is a header with 5 elements and then 4 doubles and at then end a string without spaces. This makes life very easy.
If we would need to validate the input or if there would be spaces within an string, then we would need to implement other methods. But with this structure, we can use the build in iostream facilities. The snippet
// Read all data
Csv tmp{};
char comma;
while (myFile >> tmp.a >> comma >> tmp.b >> comma >> tmp.c >> comma >> tmp.d >> comma >> tmp.e)
csv.push_back(std::move(tmp));
will do the trick. Very simple.
So, the refactored solution could look like this:
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
struct Csv {
float a;
float b;
float c;
float d;
std::string e;
};
int main() {
std::vector<Csv> csv{};
std::ifstream myFile("r:\\iris.csv");
if (myFile) {
if (std::string header{}; std::getline(myFile, header)) std::cout << header << '\n';
// Read all data
Csv tmp{};
char comma;
while (myFile >> tmp.a >> comma >> tmp.b >> comma >> tmp.c >> comma >> tmp.d >> comma >> tmp.e)
csv.push_back(std::move(tmp));
// Show everything
for (const Csv& row : csv)
std::cout << row.a << '\t' << row.b << '\t' << row.c << '\t' << row.d << '\t' << row.e << '\n';
}
return 0;
}
This is already much more compact. But there is more . . .
In the next step, we want to add a more Object Oriented approch.
The key is that data and methods, operating on this data, should be encapsulated in an Object / class / struct. Only the Csv struct should know, how to read and write its data.
Hence, we overwrite the extractor and inserter operator for the Csv struct. We use the same approach than before. We just encapsulate the reading and writing in the struct Csv.
After that, the main function will be even more compact and the usage is more logical.
Now we have:
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
struct Csv {
float a;
float b;
float c;
float d;
std::string e;
friend std::istream& operator >> (std::istream& is, Csv& c) {
char comma;
return is >> c.a >> comma >> c.b >> comma >> c.c >> comma >> c.d >> comma >> c.e;
}
friend std::ostream& operator << (std::ostream& os, const Csv& c) {
return os << c.a << '\t' << c.b << '\t' << c.c << '\t' << c.d << '\t' << c.e << '\n';
}
};
int main() {
std::vector<Csv> csv{};
if (std::ifstream myFileStream("r:\\iris.csv"); myFileStream) {
if (std::string header{}; std::getline(myFileStream, header)) std::cout << header << '\n';
// Read all data
Csv tmp{};
while (myFileStream >> tmp)
csv.push_back(std::move(tmp));
// Show everything
for (const Csv& row : csv)
std::cout << row;
}
return 0;
}
OK. Alread rather good. Bit there is even more possible.
We can see that the source data has a header and then Csv data.
Also this can be modelled into a struct. We call it Iris. And we also add an extractor and inserter overwrite to encapsulate all IO-operations.
Additionally we use now modern algorithms, regex, and IO-iterators. I am not sure, if this is too complex now. If you are interested, then I can give you further information. But for now, I will just show you the code.
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <regex>
#include <iterator>
const std::regex re{ "," };
struct Csv {
float a;
float b;
float c;
float d;
std::string e;
// Overwrite extratcor for simple reading of data
friend std::istream& operator >> (std::istream& is, Csv& c) {
char comma;
return is >> c.a >> comma >> c.b >> comma >> c.c >> comma >> c.d >> comma >> c.e;
}
// Ultra simple inserter
friend std::ostream& operator << (std::ostream& os, const Csv& c) {
return os << c.a << "\t\t" << c.b << "\t\t" << c.c << "\t\t" << c.d << "\t\t" << c.e << '\n';
}
};
struct Iris {
// Iris data consits of header and then Csv Data
std::vector<std::string> header{};
std::vector<Csv> csv{};
// Overwrite extractor for generic reading from streams
friend std::istream& operator >> (std::istream& is, Iris& i) {
// First read header values;
if (std::string line{}; std::getline(is, line))
std::copy(std::sregex_token_iterator(line.begin(), line.end(), re, -1), {}, std::back_inserter(i.header));
// Read all csv data
std::copy(std::istream_iterator<Csv>(is), {}, std::back_inserter(i.csv));
return is;
}
// Simple output. Copy data to stream os
friend std::ostream& operator << (std::ostream& os, const Iris& i) {
std::copy(i.header.begin(), i.header.end(), std::ostream_iterator<std::string>(os, "\t")); std::cout << '\n';
std::copy(i.csv.begin(), i.csv.end(), std::ostream_iterator<Csv>(os));
return os;
}
};
// Driver Code
int main() {
if (std::ifstream myFileStream("r:\\iris.csv"); myFileStream) {
Iris iris{};
// Read all data
myFileStream >> iris;
// SHow result
std::cout << iris;
}
return 0;
}
Look at the main function and how easy it is.
If you have questions, then please ask.
Language: C++17
Compiled and tested with MS Visual Studio 2019, community edition
For a project im working i want to grab text before the space and after the space.
to find the space i use the isspace method, any ideas how to get this done
Like 0x499602D2 noted in the commments, you can use std::stringstream to read from string just as you would do with any other stream (like std::cin):
#include <sstream>
#include <iostream>
#include <string>
int main()
{
int a, b;
std::stringstream ss(std::stringstream::in | std::stringstream::out);
ss << "12 14";
ss >> a;
ss >> b;
std::cout << a << std::endl;
std::cout << b << std::endl;
return 0;
}
Nice thing about this is that if you know how to work with std::cin or any other stream, you know how to work with stringstream, so you can adapt this example for you needs.
I want to copy the contents of a float to a string in C++. This doesn't work.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
float ans = getFloat();
stringstream ss;
string strAns;
ss >> ans;
strAns = ss.str();
cout << strAns << "\n"; // displays "0"
return 0;
}
How do I do this?
I think
ss>>ans;
should be
ss<<ans;
Edit:
As James Kanze noted, you are better off using std::ostringstream instead of std::stringstream as you are not using the bidirectional functionality of the first one. This way the compiler would also throw an error that you extracting ans from the string instead of inserting it into the string.
ss << ans; instead of ss >> ans and it will work
To work with stringstreams, you have to use the PUT TO operator( << ), with an object on the right hand side. That will convert the operator to a string(if the operator is defined for the particular type)(this operator<< is already defined for a stringstream object with float object).
Then, convert the string stream to a string.. and you will have successfully converted the object to string.
As the other answers show, it should be ss << ans, since << is used for ostreams and >> is used for istreams.
If you want just to print the float to cout, you can of course avoid the detour and just write std::cout << ans;, but I guess you want to use the string otherwise.
You should however be aware of the simplifications provided by Boost's and C++11's libraries:
#include <iostream>
#include <string> //for std::string and std::to_string
#include <boost/lexical_cast.hpp>
using namesapce std;
int main() {
float ans=getFloat();
string strAns1 = boost::lexical_cast<string>(ans); //boost way
auto strAns2 = std::to_string(ans); //C++11 way
cout << "boost: " << strAns1 << "\n"
<< "C++11: " << strAns2 << "\n";
}
You are using wrong operator:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
float ans=getFloat();
stringstream ss;
string strAns;
ss << ans;
strAns=ss.str();
cout<<strAns<<"\n"; // displays "0"
return 0;
}
Just one line wrong here by the look of it. You need to stream the float into the stringsteram like this:
ss << ans;
Use
strAns = std::to_string(ans);
Like I have a stringstream variable contains "abc gg rrr ff"
When I use >> on that stringstream variable, it gives me "abc". How can I get the remaining string: " gg rrr ff"? It seems neither str() nor rdbuf() does this work.
You can use std::getline to get the rest of the string from the stream:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss("abc gg rrr ff");
string s1, s2;
ss >> s1;
getline(ss, s2); //get rest of the string!
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
Output:
abc
gg rrr ff
Demo : http://www.ideone.com/R4kfV
There is an overloaded std::getline function in which a third parameter takes a delimiter upto which you can read the string. See the documentation of std::getline:
std::getline
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string str("123 abc");
int a;
istringstream is(str);
is >> a;
// here we extract a copy of the "remainder"
string rem(is.str().substr(is.tellg()));
cout << "Remaining: [" << rem << "]\n";
}
std::istringstream input;
int extracted;
input >> extracted;
IMO, the simplest thing you could possibly do is this:
std::stringstream tmp;
tmp << input.rdbuf();
std::string remainder = tmp.str();
This is not optimal in terms of performance. Otherwise, directly access the stringbuffer (probably using rbuf().pubseekpos and tellg on the stream... haven't tested that).
std::getline
getline reads characters from an input stream and places them into a string
Alternatively, if you need the rest of the string verbatim you can use std::getline(my_stringstream, result, EOF) where result is a std::string containing the result.
Keep using operator>>, it will extract the rest of it in whitespace-delimited pieces.