Fortran: user defined integer in file name of "open" statement [duplicate] - fortran

This question already has answers here:
Writing multiple output files in Fortran
(2 answers)
Closed 8 years ago.
I want to open a file in fortran with file name "controlinputs.12.dat" and then write into that file. The digit "12" is user defined variable whose value is stored in another variable "k". I have tried following and failed.
k=12
open(10,filename='controlinputs.',k,'.dat')
Tried storing the name in character and then using character to open file.
k=12
fname='controlinputs.',k,'.dat'
open(10,filaname=fname)

This was very simple. I didn't knew how to concatenate characters. I was reading the integer 12 from a file so I saved it in character type then simply used following.
character::k*5
open(10,filename='controlinputs.'//trim(k)//'.dat')

Related

simple fortran program to print back what it reads [duplicate]

This question already has answers here:
Using a deferred-length character string to read user input
(4 answers)
Reading a string with spaces in Fortran
(2 answers)
Reading a character string of unknown length
(4 answers)
How to read text files with possibly very long lines in Fortran
(2 answers)
Closed last month.
It's a very simple task, but there are two problems with my current implementation.
I want it to read a whole line. It currently only reads the first few characters if they are separated by space. I think this can be fixed with a custom format, but I don't know what to use.
Fortran needs to know how much to allocate and with my current implementation it can only read 10 characters. I want it to read without a limit. The only way that I know that can fix this right now is asking the user, how many characters they want to be read, but other programming languages can do this without it and I don't want to ask the user for additional things.
program main
implicit none
character(:), allocatable :: name
allocate(character(10) :: name)
read *, name
print *, name
deallocate(name)
end program main

How to change the name of the file taken as input by a function [duplicate]

This question already has answers here:
How do I concatenate multiple C++ strings on one line?
(24 answers)
Creating file names automatically C++
(5 answers)
Closed last year.
I have this function in C++:
void file_to_vect(double * a, char * name_file);
that takes in input a vector and writes it on the file whose name is specified by name_file. The problem is that I want to use this function on a file whose name changes. For example
file_to_vect(a,"file_18.txt")
Where the number after "file_" is not fixed. I've tried to do that by:
int n=18;
file_to_vect(a,"file_"+char(n)+".txt");
But it doesn't work. Any suggestion on how to fix this?

Reading from file at eof [duplicate]

This question already has an answer here:
Will the attempt to read an improper value into a variable change its value?
(1 answer)
Closed 7 years ago.
When reading from a file using the following command, and get iostat value to be eof, is the data from s valid or should I discard it?
Read (u, "(a)", iostat=st) s
Upon an end of file condition for a READ statement, all variables in the input list become undefined. See F2008 9.11.3 item (3).

How to validate an integer properly in C++? [duplicate]

This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
How to convert a command-line argument to int?
(8 answers)
Closed 9 years ago.
I want to validate the input by user to make sure it's an integer (fully). I've tried a few methods, like !cin, but none of them work properly..
Most of methods fail to validate input like this:
32tgf
When there is a number first and then letters, it doesn't fail, but it takes it as valid entry..
Note: It's a project for college and it's specified that the variable should be of type int.
Read into a string, then use e.g. std::stoi or std::strtol to both convert to an integer and validate the input.
Or read into a string, put that string in a std::istringstream which you use to extract the integer. Then check if there's anything more in the istringstream.
I'd recommend the first method though.

DATA declaration in Fortran [duplicate]

This question already has an answer here:
what does DATA TKX/2HKX/ mean in fortran?
(1 answer)
Closed 1 year ago.
Does anyone know the meaning of 4HEND in the following line which comes from an old Fortran code?
DATA XHEND / 4HEND /
4HEND is a Hollerith code to represent the character string "END ". In very old FORTRAN, the variable XHEND might even be a 4-byte integer or real used to hold character data. If implicit typing was in effect in this program, XHEND would have been a real. Another recent question with Hollerith codes: Writing both characters and digits in an array
It replaces assignment
XHEND='END '