Incrementing Integer Inside Lines of a Text File - list

I have large text files with about 6 lines/instances of 3_xcalc_59 in which 59 is some 2-digit integer.
I am looking to increment these values of the text file by 1 every time I run the program.
I know I can increment a value defined in the code, but how can I increment an integer inside a line of text?
I was thinking the first part of the process would involve reading these lines and assigning them to string variables or a list, but I am not sure how to even do that.
I can find the lines by writing if line.startswith("3_xcalc") , but I'm not sure how to assign them to a list.
Simply writing
for line in open(inputfile, "w"):
line.startswith("3_xcalc") = listoflinesstartingwith3xcalc
Tells me "can't assign to function call", so that doesn't work, but I'm not sure what else to try.
Thank you.

Related

Reading a line of a text file from a specific position in C++

I would like to read a text file in C++ in following manner:
Ignore the entire first line as it is simply meant as an introduction.
Only read the following lines from a specific position.
That starting position for reading is a fixed one and remains the same for every line; however, the numbers after that may be of variable length. I need to save all of these numbers from line 2 to line n into an Array.
At the moment I can read a regular 2D Array with getline.
How can I work around these things?
An example for a line I want to read could be:
Person1: 25 988.3 0.0023 7
To set the file to a position, use std::ifstream::seekg().
To set the file to the beginning of a line, you must read and count the line endings. Many text files have variable length text lines.
How can I work around these things?
You can't, unless you can ensure that all of the data lines after the first line are all the same length.
If you can't ensure that, then all you can do is read through all of the preceding lines.
An alternative I have employed in the past is to generate an 'index' of line start positions in a secondary file in binary format (so that I CAN jump directly to the right place in that file), and use that to jump to the right place in the text file. Of course that means that you need to regenerate that index file every time you replace/amend the data file.

Standard Input and Command line file in Fortran77

I've been given the challenge to port a Fortran 77 program into C#.
I've found out that read(5,*) read from the standard input, i.e. the keyboard.
Now I'm trying to understand how the following works:
1. When I run the program, I have to run it as cheeseCalc<blue.dat>output.txt
, which read a blue.dat file and produces a output.txt file. How does read work in this case?
In the same program, there is READ(5,* )IDUM and later it also has read(5,*)idum,idum,tinit. What is happening in this case?
The blue.dat file has the following lines:
HEAD make new cake
INPUT VARIABLES
MFED MASS-FEED 30 ;1001 1 100 PEOPLE TO FEED
TOVE TEMP-IN-OVEN 150.0 ;1001 20 100 TEMPERATURE OF OVEN, C
UPDATED: Just for context, the initial lines of code in the program are:
program cheeseCalc
CHARACTER*76 IDENT
CHARACTER*1 IDUM
READ(5,104)IDENT
104 FORMAT(4X,A)
READ(5,*)IDUM
c write start record
write(6,102)IDENT
102 format('**START',/,4X,A,/)
read(5,*)idum,idum,frate
110 format(f10.0)
frate2=frate/3.6
read(5,*)idum,idum,tempo
* Do calculation *
write(6,*)frate2,tempo
end
Any help will be appreciated!! Thanks!
The full detail of the general read statement is documented elsewhere, but there is an idiom here which is perhaps worth elaborating on.
The statement read(5,*) ... is list-directed input from the external unit number 5. Let's assume (it's not guaranteed, but it's likely and you seem happy with that for your setup) that this external unit is standard input.
The idiomatic part is the repeated use of a single variable in an input list such as
read(5,*) idum, idum, ...
This (and the fact that idum is an (awfully named) length-1 character variable) signifies that the user doesn't care about the input in the first two fields). The first string, delimited by blanks, is read then the first character is assigned to idum. Then idum is immediately set to the first character of the next string.
The purpose of this is to set the place in the record to the third field, which is read into the (real) variable frate (in the first case).
Equally
read(5,*) idum
is just skipping the second line (strictly, reading the first character, but that's not used anywhere before the next read into idum): the first blank-delimited field is read but the next read moves on to the next line rather than continuing with that one.

Writing on the same line in FORTRAN

In Fortran, each time one uses WRITE a new line is produced. In order to control the working of a program that is being executed, I would like to write on screen the current value of a variable, but always on the same line (erasing the previous value and starting at the beginning of the line). That is, something like
1 CONTINUE
"update the value of a"
WRITE(*,*) a
BACKSPACE "screen"
GOTO 1
Something like WRITE(*,*,ADVANCE='NO') (incorrect anyway) is not quite what I
need: this would write all the values of a one after another on a very long
line.
A trick that I was shown for what you want is as follows
do l=1,lmax
...update a...
write(*,'(1a1,<type>,$)') char(13), a
enddo
where <type> is your format specifier for a (i.e., i0 for integer).
The key is the char(13), which is the carriage return, and the $ in the format descriptor. I really don't know if there is a name for $, I just know that it works for displaying on the screen--for output to file you get an a on each line.

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

writing a single line of a file in C++ into two different arrays

suppose a text file has 11001100 11001101
and i open the text file and take the input from the file as pt[0]=11001100, pt[1]=11001101..
but if i take the input from file as in>>pt it wont put it in two different arrays which is obvious but it takes the whole line . Thus I have to take another for loop and traverse through the whole string and when I find a null character i start putting into the second array . But how can I do it without putting it into a variable and traversing .. I mean directly from the file itself ..
Read it one byte at a time (e.g. with fread()). Append each byte to pt[i], where i is incremented when a space is encountered.