Hello,

I need to write a script for a program I am running and I have never written on. I don't even know what commands scripts use, or what they even look like. Does anyone know any tutorials or example I could modify to fit my needs? Thanks,

-Al

Dani AI

Generated

Short answer: write a small "controller" (script or tiny program) that does three things in a loop: create the RNG output, atomically place/rename it where the Fortran run expects it, then invoke the Fortran executable and wait for it to finish. That single wrapper is simpler and more reliable than manually kicking things off every hour. was right to ask about the environment — the exact wrapper can be a shell/batch/PowerShell script or a C++ program, depending on whether you want portability or familiarity.

A minimal C++-style controller (conceptual) pattern to avoid busy-waits and unsafe moves:

/* sketch: compile with C++17+ */
std::system("rng_generator > rng.tmp");               // create tmp file
std::filesystem::rename("rng.tmp", "input_for_sim");  // atomic move
int rc = std::system("fortran_sim > sim.log 2>&1");  // run and wait
if (rc != 0) { /* log, retry/backoff, or stop */ }
std::this_thread::sleep_for(std::chrono::seconds(1)); // avoid burning CPU

If you prefer OS tooling: on Linux use a wrapper script run under systemd (service + timer) or cron + nohup/screen; on Windows use Task Scheduler or a PowerShell script with Start-Process -Wait, or run the wrapper as a service (nssm) so it restarts after reboots. Those options are more robust for a multi-week unattended run than leaving an interactive session running.

Practical cautions and tips: always use absolute paths, write logs with timestamps, rotate logs, check return codes and disk space, test the whole loop for a day before leaving it for weeks, and add a safe stop condition or "stop file" so you can halt the controller cleanly. This builds on ’s loop idea and on ’s file-write point, but adds atomic moves, proper waits, basic error handling, and deployment options so the run can survive crashes and reboots.

Recommended Answers

All 12 Replies

"Script" is a very broad term that typically doesn't apply to C or C++. Can you be more specific?

I have a random number generator which outputs a file that my program uses. I want to be able to write something so that I don't have to manually execute the random number generator and then manually move the file to where the program need it. Pretty much have the random number generator and the program to run without having me execute them each time. I am only familliar with c++, I do not know any pearl. I hope this is more clear.

-Al

Write a separate C++ program that creates the file and places it where you want. Basic automation. :)

My problem is to have the program execute pretty much indefinitely while I'm away. Pretty much...
Random number--->move file--->run program--->repeat

Here:

while(1)
{
     //your code here
}

What will that do?

Here:

while(1)
{
     //your code here
}

The program I am running is a Fortran program. What is does is run a simulation. I have a code that outputs a file for the fortran program, and when it has that program I run the simulation. The simulation roughly an hour to run, what I am trying to do is have the program continue to run for about 2 months without me having to execute the program each time. I pretty much want something where I can start it one day, and check on it a week later or something to that extent.

Why are you asking about scripts and fortran in a C++ forum?

I know essentially no nothing about writing scripts, I was led to believe I could write a script in c++ (the only language I know). All I need to do is execute the fortran program, nothing more than that. From everyone's responses I take it you can't write a script in c++, I'm just trying to figure out what I need to do because I am in the dark. Thanks, sorry for frustrating anyone out there.

-Al

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
       int num = rand();
       ofstream cFile;
       cFile.open("RanNum.txt");

       loop:
       cFile << num << endl;
       goto loop;
}

A very simple way of doing it which can eventualy overload your computer (I think). However, I'm confused how you want to move the file.

umm what's with the unneeded goto is while(1) not good enough anymore?

umm what's with the unneeded goto is while(1) not good enough anymore?

I have to agree with prog-man (again). The goto should only be used to get out of deeply nested loops, otherwise it is considered bad style.

I agree as well, however I chose goto so I did not have to fix the spacing (I cannot use tab on this text box). Lame excuse and write-up I know. :eek:

Anyways, to make up for it:

#include <iostream>
#include <fstream>
#include <stdio.h>

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

using namespace std;

int main()
{
	string* NewPath = S"C:\\File.txt"; // Your path
	string* OldPath = String::Concat(NewPath, S"temp");

	ofstream cFile;
	cFile.open("File.txt");	// or should it be NewPath? Not quite sure...
	
	int num = rand();
	
	try
	{

		if (!File::Exists(NewPath))
			File::Move(OldPath, NewPath);
	}


	while(1) 	// For you prog-bman : )
		cFile << num << endl;
}

Hopefully I did not mess up anywhere in this. I used notepad this time : )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.