I have a file with data like this
10000 9.425 1.00216 -0.149976
20000 19.425 0.973893 -0.135456
30000 29.425 1.01707 -0.115423
40000 39.425 1.0181 -0.12074
.
.
.
to get the data what I am doing is to read the whole line and then separate the line by the spaces to get the data I need. The problem is that the file has 3000 lines so I tried to get the line in a for loop
std::vector<std::string> a;
std::ifstream datas("Data/thermo_stability.dat");
std::string str;
char d=' ';
for(int i=0; i<n; i++)
{
std::getline(datas, str);
tokenize(str,d,a);
x[i]=std::atof((a[1]).c_str());
y[i]=std::atof((a[3]).c_str());
std::cout << x[i] << "\t" << y[i] << std::endl;
}
I noticed that something was wrong so I added that cout and found out that it was always getting the same line. How can I fix this problem? Why is not getting the next line after getline is called? When I do it outside the loop it goes to the next line.
EDIT
here is the tokenized function
void tokenize(std::string &str, char delim, std::vector<std::string> &out)
{
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}
Have some issues with that code:
I don't see where n is set so how do you know it is correct. The proper way to read a line is to call getline() and then test it worked (it can be done in a single line).
while(std::getline(datas, str)) {
// Successfully read a line from the file
}
You don't need to manually convert string to integers or floats. The stream library will do that automatically.
std::istringstream lineStream(std::move(str));
str.clear();
int value1; // please use better names:
double value2;
double value3;
double value4;
lineStream >> value1 >> value2 >> value3 >> value4;
Related
I am trying to read from a data file that contains a header which is 4 lines, and also has a list of numbers that I will be storing into a 2d int array
for example
header
header
header
header
int
int
int
int
......
I need to somehow skip these header lines which contain text and only use the int lines and store them into the aforementioned 2d array. When I open the file and search through it, it doesn't store any values at all because of the text at the very start. I've tried multiple if statements and other things to get around this, but has worked so far.
int main()
{
ifstream imageFile;
imageFile.open("myfile");
if (!imageFile.is_open())
{
exit (EXIT_FAILURE);
}
int test2[16][16];
int word;
imageFile >> word;
while (imageFile.good())
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
test2[i][j] = word;
imageFile >> word;
}
}
}
As said in the comments, you need to first read the headers - here I just store the headers in a trash variable which is a string that's being overwritten every time I store new header:
std::string trash;
for (int i =0; i < 4; i++)
std::getline(imageFile, trash);
This part goes after you check if file opened correctly and will be directly followed by your original code where you declare the 2D array and read the integers.
As it was also said in the comments you need std::getline that reads every header line as a whole and not a word at a time which was the first version of my answer (imageFile >> trash;).
You can do this just by regex and patterns (Modify code for 2d array this is just example for how you can extract numbers from file or string):
std::string ss;
ifstream myReadFile;
myReadFile.open("foo.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
ss.append(output);
ss.append("\n");
}
}
myReadFile.close();
std::regex rx(R"((?:^|\s)([+-]?[[:digit:]]+(?:\.[[:digit:]]+)?)(?=$|\s))"); // Declare the regex with a raw string literal
std::smatch m;
std::string str = ss;
while (regex_search(str, m, rx)) {
std::cout << "Number found: " << m[1] << std::endl; // Get Captured Group 1 text
str = m.suffix().str(); // Proceed to the next match
}
Output:
Number found: 612
Number found: 551
Number found: 14124
I am quite new in c++ and programming so sorry in advance in my question repeats. I have a text file of 3 lines:
7
00000000000000000000000*0000
0 0 0 R 0
What I need to do is read 2nd line and write it into an array as char. But I must not include 3rd line because it will go to a completely different matrix. My code so far :
ifstream input;
input.open("input1.txt");
input >> start;
char a=0;
string line;
while (getline(input, line))
{
a=0;
istringstream iss(line);
int length = line.size();
for (int i=0; i<length; i++)
{
iss >> a;
A[i] = a;
cout << A[i] << " " << i << endl;
}
}
input.close();
However, with this code it always starts new array for 3rd line. What am I doing wrong? What is the easiest way to fix it? Thank you.
-----------------------------Update--------------------------------------
I have modified the code but it still does not work properly. I am getting this kind of result : 5)-└ instead of correct one. My current code:
void Read(int &numLines, int &start, vector<char>&A, char B[][5])
{
ifstream input;
input.open("input.txt");
input >> start;
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
string line;
if(getline(input, line))
{
for(char temp: line)
{
A.push_back(temp);
}
}
input.close();
}
A here is a vector I want to write 2nd line to, char by char
Start is just an integer in which I am storing 1st line (7)
Thank you very much for advices
Mixing >> and std::getline is non-trivial. For example, after input >> start; the end of line marker is left in the stream in case it's still needed. In your case it isn't, and it is picked off by the subsequent call to getline, resulting in a read of an empty line.
This is what's complicating your read of line and forcing the while loop and test for empty lines.
Step through your program with your development environment's debugger and you'll see what I'm talking about. Get used to using the debugger. It's possibly the best programming productivity tool you'll ever encounter.
The easiest way to fix it is to place
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
after
input >> start;
to eat up the end of the line (and anything else that might be on that line. This needs the addition of #include<limits> to get std::numeric_limits<std::streamsize>::max.
Then you can remove the while loop and replace it with
if (getline(input, line))
No loop, not chance of consuming multiple lines from the file. And the logic for reading and processing the third line can follow.
Side note: instead of that for loop, consider
int i = 0;
while (iss >> a)
{
A[i] = a;
cout << A[i] << " " << i << endl;
i++;
}
This will march through iss until it hits the end of the line. You can also throw iss out entirely and just read the characters directly out of line.
int i = 0;
for(char temp: line)
{
A[i] = temp;
}
And A should probably be a vector if it isn't already to reduce the chances of buffer overruns.
for(char temp: line)
{
A.push_back(temp);
}
I would go with something like this:
std::string start;
std::string Astring;
ifstream input;
input.open("input.txt");
input >> start;
input >> Astring;
// If you really want a char array
char * A = new char[Astring.size()];
for (unsigned int i = 0; i < Astring.size(); i++) {
A[i] = Astring[i];
}
// Don't forget to delete after use
delete[] A;
Moreover, if you just need the char array as an input to something else later, you can call Astring.c_str() instead of that for loop, which returns a C-style char array.
I have a file that has a number in which is the number of names that follow. For example:
4
bob
jim
bar
ted
im trying to write a program to read these names.
void process_file(ifstream& in, ofstream& out)
{
string i,o;
int tmp1,sp;
char tmp2;
prompt_user(i,o);
in.open (i.c_str());
if (in.fail())
{
cout << "Error opening " << i << endl;
exit(1);
}
out.open(o.c_str());
in >> tmp1;
sp=tmp1;
do
{
in.get(tmp2);
} while (tmp2 != '\n');
in.close();
out.close();
cout<< sp;
}
So far I am able to read the first line and assign int to sp
I need sp to be a counter for how many names. How do I get this to read the names.
The only problem I have left is how to get the names while ignoring the first number.
Until then i cannot implement my loop.
while (in >> tmp1)
sp=tmp1;
This successfuly reads the first int from the and then tries to continue. Since the second line is not an int, extraction fails, so it stops looping. So far so good.
However, the stream is now in fail state, and all subsequent extractions will fail unless you clear the error flags.
Say in.clear() right after the first while loop.
I don't really see why you wrote a loop to extract a single integer, though. You could just write
if (!(in >> sp)) { /* error, no int */ }
To read the names, read in strings. A loop is fine this time:
std::vector<std::string> names;
std::string temp;
while (in >> temp) names.push_back(temp);
You'd might want to add a counter somewhere to make sure that the number of names matches the number you've read from the file.
int lines;
string line;
inputfile.open("names.txt");
lines << inputfile;
for(i=0; i< lines; ++i){
if (std::getline(inputfile, line) != 0){
cout << line << std::endl;
}
}
First of all, assuming that the first loop:
while (in >> tmp1)
sp=tmp1;
Is meant to read the number in the beginning, this code should do:
in >> tmp1;
According to manual operator>>:
The istream object (*this).
The extracted value or sequence is not returned, but directly stored
in the variable passed as argument.
So don't use it in condition, rather use:
in >> tmp1;
if( tmp1 < 1){
exit(5);
}
Second, NEVER rely on assumption that the file is correctly formatted:
do {
in.get(tmp2);
cout << tmp2 << endl;
} while ( (tmp2 != '\n') && !in.eof());
Although whole algorithm seems a bit clumsy to me, this should prevent infinite loop.
Here's a simple example of how to read a specified number of words from a text file in the way you want.
#include <string>
#include <iostream>
#include <fstream>
void process_file() {
// Get file name.
std::string fileName;
std::cin >> fileName;
// Open file for read access.
std::ifstream input(fileName);
// Check if file exists.
if (!input) {
return EXIT_FAILURE;
}
// Get number of names.
int count = 0;
input >> count;
// Get names and print to cout.
std::string token;
for (int i = 0; i < count; ++i) {
input >> token;
std::cout << token;
}
}
I am trying to read this .csv file and here is an example of the data:
1.33286E+12 0 -20.790001 -4.49 -0.762739 -3.364226 -8.962189
1.33286E+12 0 -21.059999 -4.46 -0.721878 -3.255263 -8.989429
The problem is with the first column row 1 and 2. In the excel file it says the numbers in the cells are displayed as 1.33286E+12 and when you click on the cell it says they are 1332856031313 and 1332856031328 but the program is reading them as 1.33286E+12 but I need the whole number 1332856031313 and 1332856031328.
The code:
inputfile.open(word1.c_str());
while (getline (inputfile, line)) //while line reads good
{
istringstream linestream(line); //allows manipulation of string
string str;
while (getline (linestream, item, ',')) //extract character and store in item until ','
{
char * cstr, *p;
cstr = new char [item.size()+1];
strcpy(cstr, item.c_str());
p = strtok(cstr, " ");
while (p!=NULL) //while not at the end loop
{ // double e = atof(p); // coverts p to double
value++;
if( value == 1)
{ double e = atof(p); // coverts p to double
if(m ==1)
cout << time[0]<<"\n";
ostringstream str1;
str1 << e;
str = str1.str();
string str2;
str2.append(str.begin(), str.end());
const char * convert = str2.c_str();
e = atof(convert);
time[m] = e*0.001;
m++;
//if(m >=192542)
//cout << time[m-1]<<"\n";
}
p = strtok(NULL, " ");
}
delete[] cstr; //delete cstr to free up space.
}
count ++;
value = 0;
}
inputfile.close();
If the number 1332856031313 is being serialised as 1.33286E+12, there is no way to get it back in the deserialisation process. Information in the form of those 6 extra significant digits is gone forever. You need to make sure that when the CSV file is generated, it is saved at full precision. I don't know how you might do this with Excel.
Also, your use of atof and const char* isn't very C++-esque. Consider using code like
double a, b, c, d;
linestream >> a >> b >> c >> d;
instead.
Rook has beaten me to it, but I will make one suggestion. Use a loop for decoding, testing on the stringstream status. OK, two suggestions: break your code into functions. Putting it all in one big lump makes it harder to understand.
void DecodeLine(const std::string &sLine,
std::vector<double> &vResults)
{
std::istringstream istr(sLine);
double d = 0;
istr >> d;
while (!istr.fail())
{
vResults.push_back(d);
istr >> d;
}
}
I am creating a program (In C++) that takes an ASCII file and reads a few values from each line until it reaches the end of the file. I am using ifstream to read the file, and I have never had problems with it stopping when I use the ifstream.eof() method. This time, however, even though it found the eof character in my test case, when I analyzed my other files, it is infinite looping because it never finds the eof character. Is this a coding issue, or an issue with my files?
string line = "";
unsigned long pos = 0;
ifstream curfile(input.c_str());
getline(curfile, line);
int linenumber = 0;
cout<<"About to try to read the file"<<endl;
if (!curfile.good())
cout<<"Bad file read"<<endl;
while (!curfile.eof())
{
cout<<"Getting line "<<linenumber<<endl;
linenumber++;
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
current.push_back(atof(line.substr(0, pos).c_str()));
for (int i = 0; i<4; i++)
{
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
}
pos = line.find_first_of(' ');
dx.push_back(atof(line.substr(0, pos).c_str()));
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
dy.push_back(atof(line.substr(0, pos).c_str()));
getline(curfile, line);
}
EDIT: When I first run the loop, currentfile.good() returns false...what am I doing that causes it to return that?
First thing is first, you shouldn't check like that. eof() doesn't return true until after a failed read. But you can do better (and easier)!
check the stream state with the implicit conversion to void* which can be used in a bool context. Since most of the read operations on streams return a reference to the stream, you can write some very consice code like this:
std::string line;
while(std::getline(currentfile, line)) {
// process line
}
Basically what it is doing is saying "while I could successfully extract a line from currentfile, do the following", which is what you really meant to say anyway ;-);
Like I said, this applies to most stream operations, so you can do things like this:
int x;
std::string y;
if(std::cin >> x >> y) {
// successfully read an integer and a string from cin!
}
EDIT: The way I would rewrite your code is like this:
string line;
unsigned long pos = 0;
int linenumber = 0;
ifstream curfile(input.c_str());
std::cout << "About to try to read the file" << std::endl;
while (std::getline(curfile, line)) {
std::cout << "Getting line " << linenumber << std::endl;
linenumber++;
// do the rest of the work with line
}
Do not do it like that.
EOF is not the only thing you'll encounter while reading. There's a bunch of errors you might get, and so the best is to simply test the stream itself:
while(currentfile)
{
// read somehow
}
If you're reading lines, then, the simplest way is:
std::string line;
while(std::getline(currentfile, line))
{
// use line
}
Your first call to getline is triggering one of the fail-bits on the ifstream object. That is why if you do a check for a fail-bit using ios::good(), you never enter your read loop. I would check to see what the value of line is ... it's probably empty, meaning you're having another issue reading your file, like maybe permissions problems, etc.
The problem is here:
if (!curfile.good())
cout<<"Bad file read"<<endl; // OK you print bad.
while (!curfile.eof()) // But the loop is still entered.
// Another reason to **NEVER** to use
// while (file.eof()) // as bad does not mean eof
// though eof is bad
Try this:
void readFile(std::istream& str)
{
std::string line;
while(std::getline(str, line))
{
std::stringstream lineStream(line);
std::string ignoreWord;
int number[3];
lineStream >> ignoreWord // reads one space seporated word
>> number[0] // reads a number
>> ignoreWord >> ignoreWord >> ignoreWords // reads three words
>> number[1] // reads a number
>> number[2]; // reads a number
current.push_back(number[0]);
dx.push_back(number[1]);
dy.push_back(number[2]);
}
}