reading/writing to text files

I come from python, where with text files, you read and write with strings and then convert them to the type needed after reading them in. However c++ seems to be able to read it in as the data type. Which i find quite odd.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

int main(){
	int data = 10, data_;
	double data2 = 3.14, data2_;
	std::string filename = "test.txt", filename_;
	
	std::ofstream fout(filename);
	fout << data << " " << data2 << " " << filename << std::endl;
	fout << data*10 << " " << data2 << " " << filename << std::endl;
	fout.close();
	
	std::ifstream fin(filename);
	while(fin >> data_ >> data2_ >> filename_){
		std::cout << data_*2 << ", " << data2_ << ", " << filename_ << std::endl;
	}
}


i am assuming that this method is faster than reading in the file as a string, or reading in the file as a string that is split into a container such as vector...and then converting them to the needed types at that point?

Also what happens when you read unknown formats of the file. If the structure of the file was int, double, string, you would be fine, but what if they were out of order, or some other type in their place? In that case would it be better to read it in as a string, and confirm its type first?

Is reading it in as a string then converting it to its known type slower at all?
However c++ seems to be able to read it in as the data type.
It's not C++. The stream class has functions/operators that does the convertion.

i am assuming that this method is faster than reading in the file as a string, or reading in the file as a string that is split into a container such as vector...and then converting them to the needed types at that point?
It's not necessarily faster. It depends on how you do it. I suggest that you think about improving speed only where it's needed

Also what happens when you read unknown formats of the file. If the structure of the file was int, double, string, you would be fine, but what if they were out of order, or some other type in their place?
In this case you get either wrong data or the stream fails. In such cases it might be a good idea to introduce a header that identifys the data and version and possible other informations (like character coding)

In that case would it be better to read it in as a string, and confirm its type first?
It would be better to know what you're reading beforehand (see above)

Is reading it in as a string then converting it to its known type slower at all?
As I said above. It depends on how you do it.
The stream isn't exactly known for being fast
If the structure of the file was int, double, string, you would be fine, but what if they were out of order?

To some extent this question is impossible to answer. For example, an integer value is also a valid double, and both int and double are valid strings.

Although error-checking and validation of input is a necessary part of a program, often the only appropriate action is to output a message and halt the program so the user can correct the situation.

There are situations where the nature of the input is unknown, for example a calculator where the input is some expression consisting of numbers, operators, variables, functions and so on. Here it may be best to treat the whole thing as a string and parse it character-by-character according to a set of rules defined beforehand.
Is reading it in as a string then converting it to its known type slower at all?


The obvious answer is yes, anytime you add another step, you add a small amount of processor time.

I think the real question is how much time and that is really hard to answer. For converting a single string to int for example, first you need to determine if 123 is truly an int, or if 123a is not a int. Time will depend on your method. Once that is determined, converting it is maybe a millisecond to two, even if I'm wrong, it is still a very short time.

So, a small file with a thousand strings would take lilttle time, and in human time, you wouldnt' even notice. A extremely huge file with millions or billions of strings would be best if you sorted the data first so that you could read the file and import each value with the correct type.

A example would be a list of prime numbers.

These would be a bad format because it would be difficult to read, and convert.
3,5,7,11,
3, 5, 7, 11,

Most prime number list I have seen and used look like this which is much easier to work with.
3 ; 5 ; 7 ; 11 ; 13 ; 17 ; 19 ; 23 ; 29 ; 31 ; 37 ; 41 ; 43 ; 47 ; 53 ; 59 ; 61 ; 67 ; 71 ; 73 ; 79 ; 83 ; 89 ; 97 ;

Even then with 500 million numbers it takes a while to work with, so you want to make everything as fast as possible.
Topic archived. No new replies allowed.