This question already has answers here:
Reading input files in FORTRAN
(3 answers)
Closed 8 years ago.
I want to read some data from a text file. The name of the file is 'freq.txt' and it is in the same directory where I compiled the code. The text file looks like:
100 cm-1
200 cm-1
300 cm-1
The number of lines may vary. I just want to read the number for further processing. Can anyone help me to do this?
program dataread
integer, parameter :: last=3
real :: dat(last)
open(1,file='freq')
do i=1,last
read(1,*) dat(i)
write(*,*) dat(i)
end do
close(1)
end program
Related
I want to read in multiple files in Fortran in a loop by using the filename formatting system. Problem is the filenames have numbers that don't follow each other directly. Examples of the filenames are 4e3_2048_380_40_3e9.ksz_cl.txt, 4e3_2048_200_80_2e8.ksz_cl.txt. The 3rd, 4th and 5th numbers in the filenames form a 3x3 grid. The first number goes from 140-260, the second goes from 40-80 and the third number goes from 2e8-2e9.
I've searched for answers in threads like reading multiple files in fortran but it doesn't seem to answer my question. My code below currently print out 4e3_2048_01.ksz_cl.txt.
program readfiles
implicit none
integer :: i, N
Logical, Save :: first_time = .True.
CHARACTER(len=25) :: FN
N=3 !--arbitrary number of files
if(first_time) then
DO I=1,N
WRITE(FN,10)I
WRITE(6,*)FN
OPEN(1,FILE=FN, status='replace')
CLOSE(1)
END DO
10 FORMAT('4e3_2048_',I2.2,'.ksz_cl.txt')
endif
end program readfiles
This question already has answers here:
How to write to middle of a file in C++?
(3 answers)
Closed 6 years ago.
I'm trying to take data from multiple files and append them into one file using fstream, however whenever I try to output to an existing file using
std::ofstream Out("mushroom.csv", std::ofstream::app);
it outputs to the end of the file, I want it to append to the same line, for example if this is the previous file:
1,2,3,4,5,6,7
8,9,10,11,12,13
I want it to become:
1,2,3,4,5,6,7,a,b,c
8,9,10,11,12,13,c,d,e
You can't. Files don't really have lines, they just store a bunch of characters/binary data. When you have
1,2,3,4,5
6,7,8,9,0
It only looks that was because there is an invisible character in there that tells it to write the second line to the second line. The actual data in the file is
1,2,3,4,5\n6,7,8,9,0
So you can see then end of the file is after the 0 and to get after the 5 you would need to seek into the middle of the file.
The way you can get around this is to read each line of the file into some container and then add your data to the end of each line. Then you would write that whole thing back o the file replacing the original contents.
This question already has answers here:
What is the easiest way to parse an INI File in C++? [closed]
(13 answers)
Closed 8 years ago.
I am a c++ beginner and I have a (probably simple) question. So far i have defined several variables:
double Start = 0;
double End = 1;
int Steps = 100;
I want to change these values to a value that I have stated in a text file "paramaters.txt":
x_start = 0
x_end = 10
num_steps = 100
So my c++ needs to read the file and change the double End from 1 to 10. Reading the file can be done with this function:
std::ifstream file("parameters.txt")
I want to define a variable of type std::string, called label. Then i want to read the ’label’ from the file. Using a group of ’if (label == ”value”)’ statements to determine if I'm dealing with the start, end of the number of steps. Within the if-statement, the value of 10 would stand for the end for example.
I hope that someone can help me.
Regards,
It seems you want to read a file for some values. You can do that by reading file line by line and then parsing each line.
For e.g in your case you would separate the line into two words with demiliter as "=".
But often the best way to read file for some values is to use some libraries. Like you can use boost::program options.
This question already has an answer here:
c++ how to build a 2D matrix of strings from a .dat file? 5 columns x rows
(1 answer)
Closed 8 years ago.
Say that I have a .txt file with 5000 words one above the other. and I want to convert that list contained in the txt file into this form:
{"word1, "word2", "word3" ....."word5000"}
So that way I can use it as an array for C++.
Is there a way to do that? Any method is welcome , as long as it is an automated process. Thanks for reading!
Use a vector instead of an array. Using it, the task looks something like this:
std::ifstream in("words.txt");
std::vector<std::string> words{ std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>() };
Now the word that was on the first line of the file is in words[0], the second in words[1], and so on.
Note: if a line contains more than one word, this will read them as separate words. If you want the entire contents of a line treated as a single word, see the answers to a previous question specifically about how to do that.
This question already has answers here:
read data from text file
(5 answers)
Closed 8 years ago.
I have a text file with the following layout:
step=fixed step start=100 step=1
32
112
step=fixed step start =211 step=1
11
34
and so on
I need to extract the numbers 100 and 211 respectively i.e. the start values as integers in my code and carry out some operations.
getchar and use space as delimiter. Or use scanf and formatted string. (but it still needs some spaces between words and numbers.)
Example:
scanf("%s %s %s %s %d",variables..);
scanf("%d",var);
scanf("%d",var1);
since you know format its quite easy. If it is from file use fscanf.