I need to find a way of reading in the last 6 lines of data from a file.
For example if I have 1 2 3 4 5 6 7 8 9 10
I need to read be able to get 10, 9, 8, 7, 6, 5.
This method outputs the last 6 numbers from the file however they are backwards and I need a way to reverse them and get rid of the whitespace it prints out.
Alot of the information I can find on this kind of thing are reading in all the information in a vector from the bottom and reversing it which is not what I need.
are your values arranged in a single line like you've shown?
why did you dislike using a vector and you could also apply reverse to it and make your work easier?
The vector method will still work , thing is right now i cant figure out another way your could achieve your results more efficiently without using containers.
///variables
ifstream read("file.txt");
std::vector<int> record;
int var;
if(!read)
{ std::cout<<"File not found\n"; return (1);}
while(read>>var)//no more whitespaces, your file must contain atleast 6 values
{
record.push_back(var);
}
std::reverse(record.begin(),record.end());
std::vector<int> results(record.begin(),record.begin()+6);///you could do without these
///just to make things clear.
for(constauto& reslt : results)
{
std::cout<<reslt<<std::endl;
}
Andy1992 could I get this code to work with a timestamp in the line and decimal numbers? That is the goal ultimately. Thank you for explaining this clearly.
timestamp in the line and decimal numbers?
if you've got some timestamps in your file you will need to ignore them if they aren't necessary BTW am not sure if you mean your file consists of timestamps as the values if so you can handle them with strings, if you want to handle value of floating point you could use a vector of type double instead.
ifstream read("file.txt");
std::vector<double> record;
int var;
if(!read)
{ std::cout<<"File not found\n"; return (1);}
while(read>>var)//no more whitespaces, your file must contain atleast 6 values
{
record.push_back(var);
}
std::vector<double> results(record.rbegin(),record.rbegin()+6);
/// from @keskiverto idea this will save our time
for(constauto& reslt : results)
{
std::cout<<reslt<<std::endl;
}
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
int main()
{
ifstream read("test.txt");
std::vector<string> record;
string var;
if (!read)
{
std::cout << "File not found\n"; return (1);
}
while (read >> var)//no more whitespaces, your file must contain atleast 6 values
{
record.push_back(var);
}
std::reverse(record.begin(), record.end());
std::vector<int> results(record.begin(), record.begin() + 6);///you could do without these
///just to make things clear.
for (constauto& reslt : results)
{
std::cout << reslt << std::endl;
}
cin.get();
}
However when I try to run the code it says iterator + offset out of range.
push_back is used to add another line of data to the vector
Exactly it just adds another value read from the file into the vector.
I'm not really sure what the if statement does.
Ooh I added these because your code threw an out-of-range exemption if the number of values of were less thanu six , here record.begin() + 6)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if (record.size ()>=6)///if our file had atleast six values it will be safe to use the iterators below
{
results.insert(result.begin (),record.rbegin(),record.rbegin()+6);
/// from the last value in the vector record we will insert six value upto to last-6th value.
}
else
{
///if our file held less than 6 records , an attempt to copy six values will just throw an out of range
/// exemption, instead we will copy all the values in reverse order but we will inform the user that
/// the values retrieved from the file were less than 6.
std::cout <<"Only :"<<record.size ()<<" values were found\n";
results.insert(results.begin(),record.rbegin (), record.rend ());
}