I have .txt file which contains data as follows
[12,25],[36,45] i.e numbers are enclosed in square brackets separated by comma from each other
I want to read such file through C++ program
I referred to string toolkit available,specifically regex facility can be used but i'm not able put in program can someone please help me??
just use scanf or fscanf like this:
if(scanf("[%d,%d]",&a[i],&b[i])==2){
++i;
while(scanf(",[%d,%d]",&a[i],&b[i])==2) ++i;
}
don't forget that C I/O functions are valid C++.
Would I be correct in guessing that those are co-ordinates, if so have you thought about writing a short parser for them? So you could read out a list of vertices?
Alternatively, if your really want to go down the regex path, you might want to look into downloading the boost library, boost.regex works a dream :)
#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool isSeparator(const char c){
return c == '[' || c == ']' || c == ',';
}
int main(){
const char filename[] = "data.txt";
ifstream fin(filename);
vector<int> v;
string buff;
while(getline(fin, buff)){
replace_if(buff.begin(), buff.end(), isSeparator, ' ');
istringstream sin(buff);
for(int n;sin >> n;){
v.push_back(n);
}
}
copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\n"));
//for(int i=0;i<v.size();++i) cout << v[i] << endl;
}
Related
I am reading a CSV file into vector of string vectors. I have written code below.
#include<iostream>
#include<fstream>
#include<string>
#include <vector>
#include <fstream>
#include <cmath>
#include <sstream>
using namespace std;
int main()
{
ifstream mesh;
mesh.open("mesh_reference.csv");
vector<vector<string> > point_coordinates;
string line, word;
while (getline(mesh,line))
{
stringstream ss(line);
vector<string> row;
while (getline(ss, word, ','))
{
row.push_back(word);
}
point_coordinates.push_back(row);
}
for(int i=0; i<point_coordinates.size(); i++)
{
for(int j=0; j<3; j++)
cout<<point_coordinates[i][j]<<" ";
cout<<endl;
}
return 0;
}
When I print out the vector of vectors, I see that I am loosing the first character of Element at 0 position in the vector row. Basically, point_coordinates[0][0] is displaying 0.0001 while the string is supposed to be -0.0001. I am not able to understand the reason for the same. Kindly help.
A typical output line is
.0131 -0.019430324 0.051801
Whereas the CSV data is
0.0131,-0.019430324,0.051801
SAMPLE CSV DATA FROM FILE
NODES__X,NODES__Y,NODES__Z
0.0131,-0.019430324,0.051801
0.0131,-0.019430324,0.06699588
0.0131,-0.018630324,0.06699588
0.0131,-0.018630324,0.051801
0.0131,-0.017630324,0.050801
0.0131,-0.017630324,0.050001
0.0149,-0.017630324,0.050001
0.0149,-0.019430324,0.051801
Although the problem is already solved, I would like to show you a solution using some modern C++ algorithms and eliminating minor issues.
Do not use using namespace std;. You should not do this
Ne need for a separate file.open. The std::ifstream constructor will open the file for you. And the destructor will close it
Check if the file could be opened. The ifstreams ! operator is overloaded. So you can do a boolean check
Do not use int in for loops where you compare against .size(). Use ````size_t instead
Always initialize all variables, even if there is an assignement in the next line
For tokenizing you should use std::sregex_token_iterator. It has exactly been designed for this purpose
In modern C++ you are encouraged to use algorithms
Please see an improved version of your code below:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <regex>
const std::regex comma(",");
int main()
{
// Open source file.
std::ifstream mesh("r:\\mesh_reference.csv");
// Here we will store the result
std::vector<std::vector<std::string>> point_coordinates;
// We want to read all lines of the file
std::string line{};
while (mesh && getline(mesh, line)) {
// Tokenize the line and store result in vector. Use range constructor of std::vector
std::vector<std::string> row{ std::sregex_token_iterator(line.begin(),line.end(),comma,-1), std::sregex_token_iterator() };
point_coordinates.push_back(row);
}
// Print result. Go through all lines and then copy line elements to std::cout
std::for_each(point_coordinates.begin(), point_coordinates.end(), [](std::vector<std::string> & vs) {
std::copy(vs.begin(), vs.end(), std::ostream_iterator<std::string>(std::cout, " ")); std::cout << "\n"; });
return 0;
}
Please consider, if you may want to use such an approach in the future
I am working on creating a program that is supposed to read a text file (ex. dog, buddy,,125,,,cat,,,etc...) line by line and parse it based on commas. This is what I have so far but when I run it, nothing happens. I am not entirely sure what i'm doing wrong and I am fairly new to the higher level concepts.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
std::ifstream file_("file.txt"); //open file
std::string line_; //declare line_ as a string
std::stringstream ss(line_); //using line as stringstream
vector<string> result; //declaring vector result
while (file_.is_open() && ss.good())
{ //while the file is open and stringstream is good
std::string substr; //declares substr as a string
getline( ss, substr, ',' ); //getting the stringstream line_ and substr and parsing
result.push_back(substr);
}
return 0;
}
Did you forget to add a line like std::getline(file_, line_);? file_ was not read from at all and line_ was put into ss right after it was declared when it was empty.
I'm not sure why you checked if file_ is open in your loop condition since it will always be open unless you close it.
As far as I know, using good() as a loop condition is not a good idea. The flags will only be set the first time an attempt is made to read past the end of the file (it won't be set if you read to exactly the end of the file when hitting the delimiter), so if there was a comma at the end of the file the loop will run one extra time. Instead, you should somehow put the flag check after the extraction and before you use the result of the extraction. A simple way is to just use the getline() call as your loop condition since the function returns the stream itself, which when cast into a bool is equivalent to !ss.fail(). That way, the loop will not execute if the end of the file is reached without extracting any characters.
By the way, comments like //declaring vector result is pretty much useless since it gives no useful information that you can't easily see from the code.
My code:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
int main()
{
std::ifstream file("input.txt");
std::string line, word;
std::vector<std::vector<string>> result; //result[i][j] = the jth word in the input of the ith line
while(std::getline(file, line))
{
std::stringstream ss(line);
result.emplace_back();
while(std::getline(ss, word, ','))
{
result.back().push_back(word);
}
}
//printing results
for(auto &i : result)
{
for(auto &j : i)
{
std::cout << j << ' ';
}
std::cout << '\n';
}
}
As a learner in c++, I decided to play with complex numbers, using the standard library. Now I need to read and write an array of complex from/to text files. This works simply for writing, without supplemental tricks :
void dump(const char *filename){
ofstream result;
result.open (filename);
for(int k=0;k<15;k++){
result<< outputs[k] <<endl;
}
result.close();
}
The data are parenthesized and written line by line looking like : (real,im)...
Now, I guess reading (and loading an array of complex) should be as trivial as reading. However, despite my research, I have not found the right way to do that.
My first attempt was naive :
void readfile(const char *filename){
string line;
ifstream myfile (filename);
if (myfile.is_open())
{
int k=0;
while ( getline (myfile,line) ){
k++;
cout << line << endl;
inputs[k]= (complex<float>) line; //naive !
}
myfile.close();
}
else cout << "Unable to open file";
}
Is there a way to do that simply (without a string parser ) ?
Assuming you have an operator<< for your_complex_type (as has been mentioned, std::complex provides one), you can use an istream_iterator:
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream input( "numbers.txt" );
std::vector<your_complex_type> buffer{
std::istream_iterator<your_complex_type>(input),
std::istream_iterator<your_complex_type>() };
}
This will read all numbers in the file and store them in an std::vector<your_complex_type>.
Edit about your comment
If you know the number of elements you will read up-front, you can optimize this as follows:
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream input( "numbers.txt" );
std::vector<your_complex_type> buffer;
buffer.reserve(expected_number_of_entries);
std::copy(std::istream_iterator<your_complex_type>(input),
std::istream_iterator<your_complex_type>(),
std::back_inserter(buffer));
}
std::vector::reserve will make the vector reserve enough memory to store the specified number of elements. This will remove unnecessary reallocations.
You can also use similar code to write your numbers to a file:
std::vector<your_complex_type> numbers; // assume this is filled
std::ofstream output{ "numbers.txt" };
std::copy(std::begin(numbers), std::end(numbers),
std::ostream_iterator<your_complex_type>(output, '\n') );
C++ version:
std::complex<int> c;
std::ifstream fin("filename");
fin>>c;
C version:
int a,b;
FILE *fin=fopen("filename","r");
fscanf(fin,"(%d,%d)\n",&a,&b);
C++ read multiple lines with multiple complex values on each line
#include <stdio.h>
#include <fstream>
#include <complex>
#include <iostream>
#include <sstream>
int main ()
{
std::complex<int> c;
std::ifstream fin("test.in");
std::string line;
std::vector<std::complex<int> > vec;
vec.reserve(10000000);
while(std::getline(fin,line))
{
std::stringstream stream(line);
while(stream>>c)
{
vec.push_back(c);
}
}
return 0;
}
So this seems like such a simple problem but i am struggling with it. I have a big file that i want to just go through and put every char in the file into a char array. My problem is i dont know how to deal with the newlines and any whitespace. is there a way to just go through the file and grab all of the chars, skip over newlines, and put them in an array?
Yes, i have searched around on google but with no luck.
Here is how it would be trivially done in C. I am sure it can be done using the C++ interface as well, but the C library is part of C++ too.
#include <stdio.h>
#include <ctype.h>
FILE *f = fopen ("filename", "r");
if (!f)
error ...
char array [BIGENOUGH];
int index = 0;
while (!feof (f))
{
char c = fgetc (f);
if (!isspace (c))
array [index++] = c;
}
fclose (f):
The preferred method would be to use the standard library string. Example of removing whitespace from a string here. How to read from a file line-by-line here.
Example code:
fstream file;
file.open("test.txt",ios::in);
while ( !file.eof() ) {
string str;
file >> str;
remove_if(str.begin(), str.end(), isspace);
str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
//save str here
}
file.close();
remove_if sample implementation:
template<typename T, typename P>
T remove_if(T beg, T end, P pred)
{
T dest = beg;
for (T itr = beg;itr != end; ++itr)
if (!pred(*itr))
*(dest++) = *itr;
return dest;
}
This code is untested.
The key to doing what you want in C++ is to take advantage of the formatted input operations. You want to ignore whitespace; the formatted input methods do exactly that.
Here is one way, using the canonical C++ input loop:
#include <fstream>
#include <iostream>
int main () {
std::ifstream inFile("input.txt");
char c;
std::string result;
while(inFile >> c)
result.push_back(c);
std::cout << result;
}
I prefer standard algorithms to hand-crafted loops. Here is one way to do it in C++, using std::copy. Note that this way and the first way are nearly identical.
#include <vector>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
int main () {
std::ifstream inFile("input.txt");
std::string result;
std::copy(std::istream_iterator<char>(inFile),
std::istream_iterator<char>(),
std::back_inserter(result));
std::cout << result;
}
Another way, this time with std::accumulate. std::accumulate uses operator+ instead of push_back, so we can read the file in a string at a time.
#include <vector>
#include <fstream>
#include <numeric>
#include <iostream>
#include <iterator>
#include <algorithm>
int main () {
std::ifstream inFile("input.txt");
std::string result =
std::accumulate(
std::istream_iterator<std::string>(inFile),
std::istream_iterator<std::string>(),
std::string());
std::cout << result;
}
fgets allows you to read until a newline (notice that the newline still exists in the destination buffer, so make sure to overwrite it with '\0').
Read the file line by line, and each time concatenate your output with the previous output.
What's the most compact way to compute the number of lines of a file?
I need this information to create/initialize a matrix data structure.
Later I have to go through the file again and store the information inside a matrix.
Update: Based on Dave Gamble's. But why this doesn't compile?
Note that the file could be very large. So I try to avoid using container
to save memory.
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main ( int arg_count, char *arg_vec[] ) {
if (arg_count !=2 ) {
cerr << "expected one argument" << endl;
return EXIT_FAILURE;
}
string line;
ifstream myfile (arg_vec[1]);
FILE *f=fopen(myfile,"rb");
int c=0,b;
while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;
fseek(f,0,SEEK_SET);
return 0;
}
I think this might do it...
std::ifstream file(f);
int n = std::count(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), '\n') + 1;
If the reason you need to "go back again" is because you cannot continue without the size, try re-ordering your setup.
That is, read through the file, storing each line in a std::vector<string> or something. Then you have the size, along with the lines in the file:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
std::fstream file("main.cpp");
std::vector<std::string> fileData;
// read in each line
std::string dummy;
while (getline(file, dummy))
{
fileData.push_back(dummy);
}
// and size is available, along with the file
// being in memory (faster than hard drive)
size_t fileLines = fileData.size();
std::cout << "Number of lines: " << fileLines << std::endl;
}
Here is a solution without the container:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
std::fstream file("main.cpp");
size_t fileLines = 0;
// read in each line
std::string dummy;
while (getline(file, dummy))
{
++fileLines;
}
std::cout << "Number of lines: " << fileLines << std::endl;
}
Though I doubt that's the most efficient way. The benefit of this method was the ability to store the lines in memory as you went.
FILE *f=fopen(filename,"rb");
int c=0,b;while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;fseek(f,0,SEEK_SET);
Answer in c.
That kind of compact?
#include <stdlib.h>
int main(void) { system("wc -l plainfile.txt"); }
Count the number of instances of '\n'. This works for *nix (\n) and DOS/Windows (\r\n) line endings, but not for old-skool Mac (System 9 or maybe before that), which used just \r. I've never seen a case come up with just \r as line endings, so I wouldn't worry about it unless you know it's going to be an issue.
Edit: If your input is not ASCII, then you could run into encoding problems as well. What's your input look like?