How to write some command on a batch file through C++ program? - c++

I need some help regarding my C++ program. A batch file named abc.bat is located somewhere in my hardisk. I know that in C++ I can use this line of code to execute that abc.bat file:
system ("file path here\\abc.bat");
I want to send some commands to that batch file so that after executing that abc.bat file my C++ program should write commands to its console and execute them. How can I do that?

You can do this by opening a pipe. In brief:
FILE *bat = popen("path\\abc.bat", "w");
fprintf(bat, "first command\n");
fprintf(bat, "second command\n");
pclose(bat);
The text you write to bat will end up on the standard input of the batch file.

Related

Is it possible to write and read from a text file with two programs simultaneously

If I have a program in c or c++ that writes to a particular text file and a program that reads from that same text file Is it possible for me to use the two programs simultaneously so that as the first program writes new data to the text file the other program can read it and detect the changes?
Any help would be appreciated.
Writing to a file:
if(fp)
{
// fp -> handle to the file
fputs("Satya Pawan Kartik", fp);
fclose(fp);
}
Reading from the file:
for(;;)
{
// fp -> handle to the file
while(fgets(line, sizeof line, fp))
{
printf("%s\n", line);
}
}
Let's say that the program writing to the text file is called write and the program reading the file is called read.
read obviously runs forever. Executing write displays the changes made by it to the text file by read. If required write can be modified to run forever and display the line written by it through a for loop counter. The same changes will be evidently visible in read.
So yes it is possible to write and read with 2 programs simultaneously.

Exec() read from file

I am working on creating a basic shell. I'm stuck on trying to get exec() to read in from an input file. Here's what I have. I'm unsure what arguments I should be feeding execvp(). Here, stringList[0] will be something along the lines of "ls" or "cat". If stringList[0] is ls the file would contain something along the lines of ls -a -l
int fd = open(iFile, O_RDONLY);
dup2(fd, 0);
close(fd);
execvp(stringList[0], ...);
cout << "Exec error!\n";
exit(1);
It sounds like you want to read a command from a file and then execute that command. If that's your objective, you should actually be executing the shell.
Your current approach of open then dup2 doesn't cause exec to read from a file, because exec never reads from standard input. It only reads from the executable (to load the program image). What your current approach does is redirect input, so that if exec is successful, the new program will have iFile as its standard input file.
You can just do this:
execl(shell, basename(shell), iFile, (char*)0);
Example: if iFile is the string "myCommand.sh", and shell is /bin/bash, then basename(shell) gives bash, and this is similar to running this on the command line:
$ bash myCommand.sh
For shell you probably want to use the current user's default shell. You can obtain this information portably using getpwuid or getpwuid_r.

C++ ifstream tries to open file while being written

I am polling a directory constantly for files and every time I see a file that meets some certain criteria for reading, I open the file and parse it.
string newLine;
ifstream fileReader;
fileReader.open(filename.c_str());
while(!fileReader.eof())
{
getline(fileReader, newLine);
// do some stuff with the line...
}
filereader.close();
The above code is in a loop that runs every 1 second checking a directory for new files. My issue is that as I am transferring files into the folder for processing, my loop finds the file and passes the name of the file to ifstream who then opens it and tries to parse an incomplete file. How do I make ifstream wait until the file is done being written before it tries to parse the file?
EDIT:
Wanted to better word the issue here since a replier seems to have misunderstood my issue. I have 2 directories
mydirectory/
mydirectoryParsed/
THe way my code works is that my program checks for files in mydirectory/ and when it finds them, parses them and uses the information in the files. No writing to the files are done. Once I am done parsing the file, the file is moved to mydirectoryparsed/
The issue is that when I transfer files over the network into mydirectory/ the ifstream sees these files midtransfer and starts reading them before they finish writing to the directory. How do I make ifstream wait until the file is completely written before parsing it?
Don't transfer the files directly into the directory that your program is watching; instead, transfer them into a different directory on the same drive, and then when the transfer is done, move them into the watched directory. That way, the complete file appears in the watched directory in a single atomic operation.
Alternatively, you could use a naming convention in the watched directory — append a suffix like ".partial" to files that are being transferred, and then rename the file to remove the suffix when the transfer is done. Have your program ignore files whose names end with the suffix.
You're not supposed to open the file every time you write in it. Open it once!
Some pseudo-code for this would be :
1- Open file
2- Get the data you want to write, treat that data
3- Call the write to file function
4- Loop until you have nothing left to write
5- Close de file

How to read the disk usage (du) in a C variable

I would like to do the following in a "c" program.
I need to get the disk usage of the following directory and should be able to read it in a variable.
du -sb /home/mann | awk '{print$1}'
I would like to do the above in C program and copy the output in a variable. I need to do this for this directory alone not for the "/" or "/home".
Pipe the output of your command to a file on the disk. Run your command using system
Read the file using standard C functions
Update your variable
Another option is to use popen/pclose to launch your command. This will return a file descriptor from which you can read.
Yet another option is to hunt your system for any library function that provides the information you desire

Bash input/output in C++

I'm writing program in C++ (for XAMPP communication) and I want to execute command which I have in strings (I know that this is simply system("command")) but I want to get the output from bash to C++ to string. I've founded several threads about this, but no which solved Bash -> C++.
You can call the FILE *popen(const char *command, const char *mode) function. Then, you can read the file it returns to get the output of your call.
It's like using a pipe to redirect the output of the command you used to a file in the hard drive and then read the file, but you don't get to create a file in the hard drive.
The documentation of the popen() is here.
You need to call the popen function, and read the output from the FILE it returns.
You can try Standard Output Redirection to redirect the standard output to a file stream
and then use it to read to a string.
Dup()