Read last line text file

Pages: 12
I'm still learning and did not understand how to declare it
Well, what type is it supposed to be?
Last edited on
I'm confused .. I have stated the type of variable, some missing include?
Presumably Wake is referring to http://msdn.microsoft.com/en-us/library/system.convert.todouble.aspx

Which is not C++.
exact ... My mistake, I thought something like that in other forums and tried ...

How can I convert the string to float?
If your compiler has C++11 support, you can use std::stof (or std::stod for double).

http://en.cppreference.com/w/cpp/string/basic_string/stof

If not, writing your own version isn't terribly hard:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>
 
float to_float(const std::string& str)
{
   std::istringstream is(str) ;
   float result ;
   
   is >> result ;
   return result ;
}
 
int main()
{
    std::string input ;
    std::cout << "Enter a number\n" ;
    std::cin >> input ;
 
    float value = to_float(input) ;
    std::cout << "value is " << value << '\n' ;
}


http://ideone.com/oXBOBM

[edit: It would've been better to start a new topic for questions unrelated to the original.]
Last edited on
hehe float to_float(const std::string& str) looks funny.
What makes it funny?

For simplicity, it lacks error handling. The std::stof throws exceptions when input is not acceptable. There are other ways too. There are two important things to do: (1) to learn to handle invalid input, and (2) to choose the general policy on what to do on error.
It kind of looks like "change a float to float" that is all. You can use a stringstream too.
It just makes your code cleaner doesn't it.
Superdude wrote:
You can use a stringstream too.

to_float does exactly that; there is no "too".

C++11 can declare auto to_float( const std::string& str ) -> float; that could look more intuitive (but is mainly for the compiler, which has no psychic abilities at all).
Last edited on
Good Day, I would like to take a doubt
In this code I get the last line of the file, as it would be to catch the penultimate line?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <limits>
#include <string>

std::istream& ignoreline(std::ifstream& in, std::ifstream::pos_type& pos)
{
    pos = in.tellg();
    return in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

std::string getLastLine(std::ifstream& in)
{
    std::ifstream::pos_type pos = in.tellg();

    std::ifstream::pos_type lastPos;
    while (in >> std::ws && ignoreline(in, lastPos))
        pos = lastPos;

    in.clear();
    in.seekg(pos);

    std::string line;
    std::getline(in, line);
    return line;
}

int main()
{
    std::ifstream file("temp.txt");

    if (file)
    {
        std::string line = getLastLine(file);
        std::cout << line << '\n';
    }
    else
        std::cout << "Unable to open file.\n";
}
Sorry, I forgot. I thought he was using the strof function. penultimate???
currently the script gets the last line, I get the previous line of the last
The line right before the last?
Text files are goofy things. I actually had an argument before with people about the number of lines in a text file.

I count newlines as separators, introducing a new line.
But some people (even some more modern systems) count them as line terminators. (Which is why your text files should always terminate with a newline.)

So you have to consider that the last line may be an empty line. If it is, discard it -- don't count it as one of your lines.

    "1\n2\n3\n" --> the 'last line' is "3", not "".

    "1\n2\n3" --> the last line is "3".

Hope this helps.
Wake wrote:
currently the script gets the last line, I get the previous line of the last

I showed you how to get the last line. I'm sure you can modify the code to make it work for the next-to-last if you really wanted to, although the next step would be to make it work for arbitrary lines in the file, and just reading in all the lines for that and retrieving them from a container would probably be preferred.


Duoas wrote:
So you have to consider that the last line may be an empty line.

The posted code ignores all empty lines (in >> std::ws prior to ignoreline.) So, I suppose it doesn't actually retrieve the last line in the file in some cases. Maybe getLastNonEmptyLine would be a better name for the function.
If anyone is interested, the tail utility's source is an, er... interesting read.
http://www.gnu.org/software/coreutils/
closed account (N36fSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>

#include <vector>

using namespace std;

int main()
{
	fstream file("example.txt");
	vector<string> contents;
	string buffer;
	
	if(file)
	{
		while(getline(file, buffer))
		{
			contents.push_back(buffer);
		}
		
		cout << contents[contents.size() - 1] << "\n";
	}
	
	std::cin.get();

	return 0;
}


Something like this. I didn't compile it at all, but I use this basic model in my program and I read everything in a file. There could be more error handling.

This isn't the most efficient way, but it gets the job done. You could empty the vector if you want it a bit more conservative of memory.

/*your guy's ways are kinda lengthy*/
Last edited on
Topic archived. No new replies allowed.
Pages: 12