overwrite a single line with fortran - fortran

I am wondering how to edit a single line in an existing file without overwriting the rest of that file with fortran? I tried
access = 'append'
or
status = 'replace'
or
status = 'default'
but all of them gives me a a file which is changed on that specific line but the rest of the file is empty.
Thanks!

Assuming that you mean an existing sequential file, I don't think that you could do this in Fortran. Perhaps if the lines were of fixed length and you opened it as a direct access file, you could modify a line in the middle without changing lines before or after. If the lines are of variable length (indicated by the Fortran EOL for that OS), then I don't think you can modify an existing file. The easiest solution is probably to copy the file to a new file, copying most lines and modifying the one.

Related

Macro within a loop and include

In Stata, I'm trying to use the command include to run several regressions in one do file. The overall goal is to help in forecasting Natural Gas production.
I have a do file for each product and basin that I'm interested in. Within each do file I am running several versions of the regression based on data available at specific times (e.g. the first set of regressions is for product type 4, in basin 2, with information available in June 2020).
The regression command within the do file looks something like this:
include "gas\temp\NG var.doh"
foreach time in dec14 dec15 dec16 dec17 previous current {
arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}
I have the perm4pvars defined in the file NG var.doh like this:
local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time'
When I run my do file the time from my doh file doesn't show up. So I get an error message: "PNGHH_ is an ambiguous abbreviation"
How can I get the time to show up in my regress command?
I did try doing this in my .doh file
foreach time in dec14 dec15 dec16 dec17 previous current {
local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time'
}
I got it to run, only for time=current
The relationship between the included .do file and the main .do file is not symmetric.
The point of include is that the included do file is read into the main do file. But the included do file knows nothing about any files it is included in. So definitions in the included do file may not usefully refer to definitions in the main .do file, or to any others, unless they in turn are included, or so I presume.
That explains why your reference to local macro time in the included file doesn't do what you want. It's not illegal in any do file or Stata program to refer to a local macro that doesn't exist (meaning, is not visible from the file) but as here the consequences may not be what you want.
Imo your issue is more basic than what the above answer suggests, in that you want to use locals from a loop outside of it. Or, in the case where you got it to run for time=current, misunderstanding what the loop does. Might be good to get a good understanding of the functioning of loops first, also since you seem to be using multiple other loops that are not detailed in your question and where I can only assume those are specified correctly.
Assuming the gas\temp\NG var.doh file only holds the line defining the local, i.e. local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time', a way to get it working the way you want (or at least how I think you want it, since you do not detail the specifications you want to achieve with your loop) is to move the include inside the loop, changing your code to:
foreach time in dec14 dec15 dec16 dec17 previous current {
include "gas\temp\NG var.doh"
arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}
This way, the line in the included .do file can use the local time from the loop. In the case there is more in the .doh file you would have to change your code a bit more to get it to work, however I can't help you with that unless you give me more information about the structure of the code and what you want to achieve with it.

File handling C++:Which is the ideal stream to be used while working with database files?

I wanted to know about the file handling stream that should be applied while working with database files.I want to create a database file i.e. a file which contains contents and these contents can be edited
Eg-Suppose the file contains data as following
Harshul 97 Jack 42 Sergey 69 Bill 96 Mark 92 Will 49
It is a database file which contains user's name along with the money in their account (which is stored after account's name).
Now suppose that I want to add a new account to my database for that I would have to first check that if an account already exists because if it exists then I will display error message else i will simply create a new account by appending data into the file.
Now I thought that I would need to edit the data so I should use fstream but while working with fstream I got the problem with the end of file marker which sets good bit to fail bit and stops file i-o operations I got a solution for that i.e. to clear the stream where ever necessary (whenever file pointer hits eof)
Eg-
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
But this was a little idiotic here so I thought that I should use the peek() function that tells us if the next bit is eof before it but got the result that it is not the right thing to do rather I should open the file again and again File Handling:What is the use of peek() function in c++?
Whereas I also got the suggestion to use ifstream and ofstream (with no trunc and ate modes) simultaneosly but I wanted to know if I would be able to edit data with it.Suppose I entered details of a new indivisual say "Finch 96" now the ofsteam variable will account for the new entry but for the ifstream object it had read the data of file into its buffer earlier and has no account for our new entry "Finch 96" untill we don't reopen the file in ifstream object
I have searched a lot about this matter but didn't got the result may be I was not able to express my problems properly and now I think that my objective is clear to all
A text file is nice if you want to be able to edit it by hand. If you call it a database, and only want to process it programmatically, you should considere a binary file. At the simplest level, you could have a direct file with fixed size records, that allows you to do in-place record edition. Or if you prefere not to re-invent oval wheels when round ones exist around, you could use a sqlite database which would deal with implementation details for you.
But if you really need a text file, you should read it once in a container of records, and save it all records at a time. A good practice is save to a temp file in same folder and rename it only when everything has successfully be written.
You probably should not create a text file in the first place. Did you consider using sqlite or some real database like PostgreSQL or MongoDb?
If you insist on editing programmatically a textual file, the only way is to process every line : either keep all of them in memory, or copy them (except the one you'll change) to some new file.... Which is not very efficient.
You might also be interested in textual serialization formats like JSON,it's quite easy to use and very powerful.

Creating users for game(reading and parsing CSV file)

So I am working on a planets vs zombies type mock up game for class, and am using Qt Creator GUI with C++. One of the things that we are required to do is, on start-up, the game window will attempt to read two files: "pvz_levels.csv" and "pvz_players.csv" from a pre-specified home directory.
The levels file is of the form "level:sequence:rows:start:interval:decrement" and "sequence" itself is a comma separated list of the form (1,1,1,2,3,1,3,1,3,3) which is the sequence in which zombies appear. If this file does not exist in the directory, the program exits with an error.
The players file is of the form "timestamp:player:level"; the time of last play, name of player, and last attempted level, respectively. If this file does not exist, the program silently skips this operation and starts as a new player. If it does exist, the file must be read and parsed, and then used in the program for calculations and such.
So, I am having much trouble with reading and parsing these files. Furthermore, we are required to save the user data in these files, and on the next start-up the user should have the option to continue their game by selecting their respective user from a drop-down list. They should also be able to delete any users.
I am proficient enough with c++ basics but this is my first GUI experience and my prof did not go over it in much detail, so I require quite a bit of help with this project.
Thank you to anyone who is able to help!
Look up the code to an available "csv parser" which stands for "comma separated variable". You need is almost identical, except you use a semi-colon instead of a comma. It seems that by changing one character you parsing is done for you.
You may be able to find a csv parser that accepts the character to be used as the parsing character (I've seen them before).
If you wish I can find a suitable csv parser for your use, but now that you know what you're looking for "csv parser c++ code" it should be a quick Google away.
Also, most csv parsers expect strings to be enclosed to double quotes (") but that is easily modifable.
Some hints:
Just open the File using QFile. Set up a QTextStream and use QTextStream::readLine() to read all Lines into QStringList. Now use QString::split() on the QString saved in the list to get the single values stored in this line. From there you can easily use QString::to* functions to cast the values into your desired type.
For saving just reverse the procedure.
Set up a line using: QString("%1,%2,%3").arg(timestamp).arg(player).arg(level) and put it into your QTextStream. If the stream is connected to a file, this will be written into the file.
I've implemented successfully the CSV reading like this:
std::unique_ptr<QFile> csv_worker(new QFile(resource_path));
QTextStream input_csv(csv_worker.get());
QList<QStringList> data
while(!input_csv.atEnd())
{
//removes the carriage return symbol and splits the elements
QStringList line = input_csv.readLine().remove(QRegExp("\r")).split(","); //replace here with :
data << line;
}
csv_worker->close();
It reads the complete file, each line generates a QStringList.
For the second element in the QStringList, let's say (1,1,1,2,3,1,3,1,3,3}, you have to additionally split that in a sub-QStringList, removing then brackets with something like this:
QStringList sequence = data[i][1].remove(QRegExp("{")).remove(QRegExp("}")).split(",");

Specifying Source file name using parameter variables in informatica 9?

I have a mapping like
SA-->SQ--->EXPR--->TGT
The source will be of the same structure and the tartget also.
There are a bunch of files(with the same structure) which will go through this mapping .
So i want to use a parameter file through which i will give the file names for every run manually.
How to use the param file in session for Source filename attribute
Please suggest..
you could use indirect source type, wherein your source file is basically a list of files, and in turn the session reads each of the files one by one.
the parameter file could reference a source file name (the list) as
$InputFile_myName=/a/b/c.list
In line with what Raghav says, indicate the name of a file that will hold a list of input files in the 'Source filename' property box for the SQ in question in the Mapping tab, making the file 'Source filetype' be 'Indirect', specified in the Session Properties. If you already know ahead of time the names of the input files, you can specify them in that file and deploy that file with the workflow to the location you indicate in the 'Source file directory' property box. However if you won't know the names of the input files until run-time but know the files' naming standard (e.g: "Input_files_name_ABC_" where "" represents variable text, such as a numeric value incremented per input file generated by some other process), then one way to deal with that is to use a Pre-Session Command specifiable in the 'Components' tab of the Session. Create one that will build a new file at the location and with the name specified for the Indirect input file referenced above by using the Unix shell (or if running on Windows, the cmd shell) to list the files conforming to the naming standard for them and redirect the listing output to that file.
Tricky thing is that there must be one or more files listed in that Indirect type of input file. If that file is empty, the workflow will fail (abend). An Indirect file type must have in it listed at least one file (even if that file is empty) and that file must exist. The workflow fails if the indirect file reader gets no files to read from or if a file listed in it is not present on the server to be read from. One way to get around this is to make sure an empty file is present at all times that conforms to the naming standard. This can be assured by creating a "touchfile" before executing the listing command to build the Indirect file type listing file. In Unix, you'd use the 'touch {path}/{filename}' command ({filename} could be, for example, "Input_files_name_ABC_TOUCHFILE"), or on Windows you'd redirect an empty string to a file likewise named via cmd shell process. Either way, that will help you avoid an abend. Cleaning up that file is easy to do: a Post-Session command can be used to delete the empty touchfile. Likewise, you can do the same for the Indirect type of file if desired.

C++ How do i delete a specify line from a textfile?

As stated from my title, how can i delete a specify line from a textfile.
My program has a HR user, which they can edit/remove users information.
I am able to write into a file, but to delete from a specific line, i am clueless.
Hopefully someone can give me an example of how to do it, thanks !
A example of my textfile
user;pass;1234;John;1111
user1;pass1;2345;May;2222
user2;pass2;3456;Mary;3333
user3;pass3;4567;Andy;4444
hr;hr;5678;Jonathan;5555
admin;admin;6789;Aili;6666
user10;pass10;7890;eggy;9999
and so i want to delete the contents of user3 which is at line 4 of my textfile,when the user inputs the username, which is user3.
Here is a pseudocode, I will let you work out the details:
1. read the entire file into a vector
2. delete that file
3. create and write back the data to the file skipping the line that isn't required.
Use std::getline() in a loop to read a line from the file.
Load the file content into memory,
delete the line there,
and write the content back to file.
You could somewhat optimize this process by not loading the portion of the file before the line being deleted (though you'd still need to scan it to find the "target" line), but you won't be able to do much better than that without a specialized data structure.
If this is really important performance-wise, consider using a database instead of the plain file.