I am writing to a text file using c and using a batch file to open the text file and then run the corresponding exe file at the same time. My c code is writing a dividing underscore line and the current date and then the user would write whatever text. The issue is that when I run the batch file which opens the text file, the cursor is at the beginning of the file and I want it to start at the very end so I wouldn't have to move it myself to write text.
This is my c code:
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;
string underscoreDiv (int lengthText){
string underscores;
for(int i{}; i<lengthText; i )
{
underscores = "_";
}
return underscores "\n";
}
struct Date
{
int year;
int month;
int day;
Date()
{
time_t t = time(0);
tm* now = localtime(&t);
year = now->tm_year 1900;
month = now->tm_mon 1;
day = now->tm_mday;
}
};
int main(){
fstream fileApp("C:\\Users\\trist\\OneDrive\\Documents\\Notes_application\\Notes_app.txt", ios::in | ios::app);
if(fileApp.is_open()){
string line;
string underscore;
Date date;
int count[2]={0};
underscore=underscoreDiv (80);
fileApp.clear();
fileApp<<"\n";
fileApp<<underscore<<"\n";
fileApp<<date.month<<"/"<<date.day<<"/"<<date.year<<" : ";
fileApp.clear();
fileApp.close();
}
else{
cout<<"Text file not found";
}
system("C:\\Users\\trist\\OneDrive\\Documents\\Notes_application\\Notes_app.txt");
return 0;
}
Here is my batch file
start /b C:\Users\trist\OneDrive\Desktop\projects\notes_project_app\app_proj.exe
start C:\Users\trist\OneDrive\Documents\Notes_application\Notes_app.txt
'I tried playing with the c code but I'm finding it hasn't been able to actually move the cursor position when the file is opened through windows. I'm having trouble trying to move the cursor to the end through the batch file. I tried doing a send key page down but I couldn't get that to work either.'
There may be some unnecessary function in the code as I'm trying to other things meanwhile.
CodePudding user response:
Mmmm... Your question is pretty confusing... After read it several times, I think I can state these points:
Neither Windows nor a Batch file "open" a text file. A text file is open by a text editor program (like Windows Notepad). If I correctly understand your question, you have a C program that create a text file and then, after the file was created, open the text file with the standard text editor, not at "the same time". For this reason, your batch file is wrong: if both your C program and text editor run at the same time (both started via start
command), then exist the possibility that Notepad open the file before the C program have completed and closed it (not matter if the start
command of the C program is placed before the start
command of Notepad).
On the other hand, you just want to open the text file (with the standard text editor) and place the cursor at the very end of the file, right? In such a case no matter who and how created the text file! So all your explanation about your C code (and the C code itself) have no relation with your problem and just confuse and over-complicate the problem...
However, in this case the C code show another error: it open the text file via a system
command! The batch file also open the file, so the system
command in the C code is wrong and must be deleted.
I think this Batch file will solve your problem:
@if (@CodeSection == @Batch) @then
@echo off
rem Run the C program, do *NOT* use "start" here
C:\Users\trist\OneDrive\Desktop\projects\notes_project_app\app_proj.exe
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start the text editor program ("open" the text file)
start C:\Users\trist\OneDrive\Documents\Notes_application\Notes_app.txt
rem Wait a little
timeout /T 1
rem Send a Ctrl-End key to move cursor to end of the file
%SendKeys% "^{END}"
goto :EOF
@end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
For further details about the method to send a key from the Batch file, read this answer.