Formatted file i/o C++

I am trying to read values form a file. This file contains information, which in this case are the values of a 3D array printed in consecutive row major order. I would like to do this in C++ because the file of interest contains input values for a code.

The format descriptor '(4(ES13.5,F7.4))' tells the code that the array is not on one line only, but is stored in 4 values on each row (precision 5 digits and buffer width 13), where each value is followed by uninteresting to us number red in the buffer DUMMY (precision 4 values buffer width 7).

Is it possible to do this in C++, I presume it is. However I was not able to find information about it. I tried browsing the other topics on the forum, unfortunately it seems that nobody have done such things. I would appreciate any help on the topic.

Is it possible to dothis in C++,
Yes, it is possible to read your file and to output a similar file in C/C++.

However I was not able to find information about it.
Reading each line into a structure, vector, or array is fairly straight forward since each item is separated by a white space character. You can use the extraction operator>> to extract each item and place it into the corresponding element. You may want to study up on basic C++ file input and output. http://www.cplusplus.com/doc/tutorial/files/

Writing the file is also fairly simple using the io manipulators. Your odd columns are in scientific notation and have a width of 13 and a precision of 5, the even columns are in a fixed format and have a width of 7 and a precision of 4.

So, to answer the question, yes it is possible, use ifstream to "Input from File". You will have to work out exactly how to deal with the whole scientific notation thing, but it is easy enough to make your own function that deals with that.

on second thought, the whole scientific notation thing will complicate how you will write your code though. I don't use fortran, so is it possible to rewrite the file from there so that the numbers are stored as regular notation? that will just save you some headache in transferring from char to floating point numbers, and might save your computer some processing time.

It all depends on how much work you're willing to put in and how much experience you have in C++.

Why do you need to transfer from char to floating point numbers. The extraction operator>> knows how to extract numbers that are stored in scientific notation format to doubles. 1.45915E-03 is a valid floating point number.

Well i know how to do that, thanks for the feedback however. I think i am not expressing myself clear enough.

I am not much aware how things are done in C++, but in FORTRAN one needs to have an empty buffer which might be a real number. It is used to store the values which are of no interest. In my case I am not interested in the numbers written in normal format, which are trailing the values written in scientific notation. In general i am not talking about cast form char to float. I am just asking how to get rid of the trailing numbers.

In general ignore the scientific notation, what i do not understand is how to loop the array and have only fixed number of values on each line, and how to get rid of the values i do not need, and which are sitting in between.

I looked at this in two different ways. In a language such as COBOL, the defining of a layout to match the line of data is very easy. In C++ too it is possible, using a data structure, to match the 13 and 7 character fields, repeated four times.

However, the resulting code ended up being longer and more complex than just using the ordinary C++ formatted input.