I asked an interesting (for me) question. Is it possible to read data in C ++ from files in columns, using ifstream? Example: there is a file for many (100k) lines of the following form:

(int float float float соответственно) 1 22.4000 24.1423 24.1352 2 22.2000 24.1441 24.1367 3 22.0000 24.1402 24.1345 

etc. In C, reading such a file is elementary:

 f = fopen("123.dat","r"); while(!feof(f)) { fscanf(f,"%d %f %f %f",&a,&b,&c,&d); //и обрабатываем a,b,c,d как нам вздумается } 

But with C ++ there was a question. There were ideas about the use of the separation character, but it is not always clear that there are \ t or spaces. I will be glad to read your comments. Answers in the style of "why not use the C-option and not enjoy life" or "use% name_lib%" library "are not desirable, because it is interesting for me to solve this problem using C ++ and most of sports interest. Thank you in advance for the response

  • If you want to achieve speed - I recommend you a cyclic buffer (2 or 3) * 4096 bytes and sscanf or atof , or maybe even parse the numbers even manually. - nick_n_a
  • I don’t understand what `while (cin >> x >> a >> b >> c) cout << x << '<<' << a << '<< << << <<' << c << '\ n'; `not suitable? - avp
  • @ Chorkov, @ avp isn’t it so simple?!, Even embarrassing: D thanks for the prompt response - dr_zak

1 answer 1

 std::ifstream f("123.dat"); int a; float b,c,d; while( f >> a >> b >> c >> d ) { //... } 
  • The question of filling, and if a, b, c, d are vectors? Will additional temporary variables be needed? - dr_zak
  • Yes, to fill the vectors will need temporary variables. (Or third-party libraries.) - Chorkov