If you're going to execute system() calls, use a batch file. I've hardly ever seen a case where the main functionality of a so-called C/C++ program used system() and couldn't be made into a far more terse batch.
As to your issues: they are all based on not knowing enough about C/C++. Which probably explains why you would choose it for something like this.
Anyway, I tried to make a C/C++ app but apparently stream i/o won't let me input or output a char*, and I couldn't be bothered to figure out why.
So here's a C# program. haha.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace floppinc
{
class Program
{
//equivalent to C's System() call. ugh.
private static int System(String commandline)
{
String[] splitarg = commandline.Split(new char[]{' '},2);
Process getprocess = Process.Start(splitarg[0],splitarg[1]);
getprocess.WaitForExit();
return getprocess.ExitCode;
}
/// <summary>
/// returns integer equivalent of contents of file +1, or 1, if the file doesn't exist.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
static int SequenceValue(String filename)
{
int intvalue =0;
//open the file.
if (File.Exists(filename))
{
FileStream fsr = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fsr);
//read the first line.
String lineread = sr.ReadLine();
//parse it to an integer.
intvalue = lineread==null?1:int.Parse(lineread) + 1;
fsr.Close();
}
else
{
intvalue = 1;
}
String strvalue = intvalue.ToString();
//reopen for truncate.
FileStream fsout = new FileStream(filename, FileMode.Create);
StreamWriter sw = new StreamWriter(fsout);
sw.Write(strvalue);
sw.Close();
return intvalue;
}
private static String ChoiceInput(String Prompt, String[] valid)
{
string currinput=null;
bool isvalid=false;
while (!isvalid)
{
Console.WriteLine(Prompt + "[" + String.Join("/", valid) + "]");
currinput = Console.ReadLine();
isvalid = valid.Any((w) => w.Equals(currinput, StringComparison.OrdinalIgnoreCase));
if (!isvalid)
{
Console.WriteLine("Invalid option-" + currinput + ".");
}
}
return currinput;
}
private static readonly String sequencefile="D:\\sequence.dat";
private static readonly String commandformat = "XCOPY A:\\*.* C:\\Floppies\\{0}\\*.* /S /Y";
static void Main(string[] args)
{
bool exitloop=false;
while (!exitloop)
{
int sequenceval = SequenceValue(sequencefile);
String execcmd = String.Format(commandformat, sequenceval);
System(execcmd);
exitloop = (ChoiceInput("Copy Another?", new string[] { "Y", "N" }).Equals("N",StringComparison.OrdinalIgnoreCase));
}
}
}
}
I rather like the ChoiceInput() function, myself.
This would be a very short batch file, though, I just don't like messing with batch files.
EDIT: fixed line endings. That's what happens when you copy text files from windows to Linux...