Formatting in file using Fortran - fortran

do i=1,10
write(21,19) (dai(i,j),j=1,10)
end do
19 format(10f12.10)
This is part of my code where I input in file, here 21 is unit of my file. I want to print 10x10 matrix in file such that only 10 digits are there after decimal. The output of this is formatted matrix but with elements having no space between them. Also if I remove format line from code the file output is usual 10x10 matrix(unformatted). What is wrong here?

If you want spaces in the formatted output, you have to include them in the format. Either explicitly using the appropriate descriptors or by increasing the fields for your numbers.
E.g.,
19 format(10(f12.10,1x))
or
19 format(10f25.10)

Related

How to read a certain line from a file and read from that point below, in c++?

I have a txt file, with some numbers. Let's say I have these lines.
124
559
774
12
145
698
So there are 6 lines, let's say, I would like to read the file from 12 and below, is there a function in c++ that returns the cursor of the file? I don't know prior what are the values, I was just trying to explain what I would like to do.
So for example if I would like to read the values 12, 145, 698, is it possible, and ignoring the prior values, not deleting.
You can use getline : http://www.cplusplus.com/reference/string/string/getline/
And call it in a loop to skip the first N lines.

Fortran code to read formatted data file record wise

I got a Fortran code for K-means clustering from online.
As I am new to Fortran, I do not have an idea about the required input file format for the code as below.
How to prepare an input file according to this Fortran code?
infile1='D1_TR_all_cent63.dat'
OPEN(1,FILE=infile1,form='formatted',access='direct',
+ recl=429)
istep=nvectors/nclusters
DO i=1,nclusters
READ(1,23,rec=istep*(i-1)+1)(vec(j),j=1,42)
DO k=1,nelements
centroid(i,k)=vec(k)
END DO
END DO
23 format(42(f10.3))
It's looking for a file where each line contains 42 fields where it expects to find fixed point numbers. That's the 42(f10.3) in the format
statement. Each of those fields is 10 characters wide; there are no commas or other separator characters between them, although spaces are allowed.
For example:
123456.789 123.123 -123.123 12345.678 0.000
(and onward until there are 42 of those).
Most FORTRAN implementations will be a bit merciful on reading: You can leave off some of the after-decimal point digits, or have a space after the number (but you have to stay in the 10-character fields). But not all do, so it might be better to prepare your input with 6 digits (including sign, if needed, and allowing spaces instead) before the decimal point and three after.

How to customize the encoding of a text file in c++?

I am working on huffman file compression project. Till now I know it works something like:
file.txt(original) -> file.huf(encoded, compressed) -> file.txt(decoded)
What I have to do is to open the txt file, generate the huffman code, but how can I replace those code in the binary code file of the original file. For example, if file.txt stores abc then its ASCII encoded file will store 01100001 01100010 01100011 and huffman-coded file i.e file.huf should store 10 11 0 and then this file should be decoded using the encoding map generated.
My question is to how can I do this in C++ at the file implementation level, how can I alter the binary file?
I have figured it out, in simple words, make the code for an alphabet and then for another and combine, iterate this till the code is equal to or more than 8 characters containing 1 and 0, then this sequence of 8 digits is encoded to some character and stored in a file.
This way file can be compressed.

Fortran New Output File

I'm new to Fortran, and trying to repurpose a bit of code that is part of a larger program I've inherited. When the program is run, it prompts for output file names. I would prefer to specify these output file names directly in the code, so I can run the program in parallel/all at once (the run time of the program is about 15 hours, and needs to be run for 66 different files, so I would prefer to run them all at once as opposed to one after another because that would take quite a long time).
The bit of code I'm working with looks like this:
24 print *,'Enter output filename'
25 read(*,*) fout
26 print *,'Enter parameter estimate output filename'
27 read(*,*) foutb
28 print *,'Enter seed for random number generator'
29 read(*,*) idum
30 if(idum.gt.0) idum = -idum
Everything I've tried gives me:
At line 25 of file v1.f95:
Fortran runtime error: End of file
I've checked that I have proper line endings.
Any suggestions?
If I understand the question correctly, you want to pass a number of output filenames/other variables to your program at runtime. This could be handled by using a simple namelist input file, and passing this single file to the program. The code would look like, for example:
character(20) :: file1, file2, file3
integer :: idum
namelist /input/ file1, file2, file3, idum
read(unit = iunit, nml = input)
where iunit is the I/O unit connected to the previously opened input file. The contents of this file would be like
&input ! Name of corresponding namelist group
file1 = 'file1.out',
file2 = 'file2.out',
file3 = 'file3.out',
idum = 1
/
Namelist files are very easy to handle. They don't care about the order of entries, record length or comments. You can omit any variables in the namelist file (their value will not be modified), or include multiple namelists (even of the same name) in a single file.
perhaps the simplest possible solution for you, assuming you have only a
few simple read(*,*)'s you can leave the code alone and simply pipe
the required strings to standard input
echo "file1\nfile2\n3" | executable
(tested under csh.. you might need to tweak a bit for a different shell)
The error message BTW makes me think you were already reading from a stdin pipe
rather than a terminal and some prior read exhausted the input.
I think your error comes from the fact that fout cannot be read without a character string as the format specifier. Try this on line 25 (and in other read's)
read(*,'(a)') fout
You could improve on that if you copy the length from the definition of fout, like so (guessing the size)
CHARACTER fout(25)
.
.
.
read(*,'(a25)') fout
You are reading from the standard input (this is the asterisk in the first argument). I assume your input file is ASCII text. Like you have it now, you could specify the names of the output files on the first two lines of the file, then a third line for idum, then the rest of the input (if you have any).
Say your executable is my_program.exe and your input file (with the three lines mentioned above) is my_input_file.txt, you could redirect the input file to standard input like this
%> my_program.exe < my_input_file.txt

Fortran randomely writing data in file

How to write a text or dat file in FORTRAN like a 2D array of integers and each time to enter a value, if in any row there is no value just insert in the start but if some values exists insert to the end of values. This insertion of values can be random, i.e. may be line number 100 first then 80 then 101 then 2. The number of entries in each line is also different.
I also need to use this file at the end but I think that will be easy as need line by line information.
Edit (what he ment possibly) :: How to write a text file in Fortran, like a 2D array of integers, each time adding one value? If there is an empty row with no values, insert one at the beginning of a row, but if there are already some values in that row, append the new value to the end of the row.
Have no idea what he was getting at with those random values and line numbers.
If you want to make decisions based on the input, read the line into a string. Then examine the contents of the string and decide which case of input. If you have numbers that you want to read, use an "internal read" to read them from the string. This question has a code example: Reading comment lines correctly in an input file using Fortran 90