C - Seek to beginning of stdout - c

I am looking for a way to seek to a beginning of stdout. The reason I want to do that is because I am writing a program and i want to constantly display a status(idle, working, done) while writing other output on stdout.
I tried using fseek and lseek but with no success. I also tried using \r and \b in printf when printing but that also didn't work for me.
For example, the output i want to have would be something like this:
[Idle] Waiting for file...
And when the file is received i want it to change the output to:
[Working] Getting contents of the file...
And when that job is complete to change it to:
[Idle] Waiting for file...
[Done] Saved contents from a file...
And when the next file appears change the status to Working and afterwards change it back to Idle and print out a Done message.
Thanks in advance

Related

File empty after power cut while the fopen and fclose functions are present

Good morning all,
My goal is the following:
I want to fill a text file every 5ms (with a timer).
Below you will find the function code.
Code link
In normal operation, this function works correctly as long as I go into the "REGUL_STATE_EG" state and exit it correctly. While in the "REGUL_STATE_EG" state, my file is filling correctly with no problem and on output, I get a file that is filling correctly too.
Here is the problem :
As soon as I go into the "REGUL_STATE_EG" state and a power cut occurs at some point while I'm still in that state, the data that was added to this file before the power cut occurred get totally lost and I get an empty file and I don't know why.
In theory, the data written in the file before the power cut should be saved because each time, there is opening and closing of the file but in my case I recover an empty file. There is nothing in it.
Thank you in advance for your help.

Trouble with Scilab "print" instruction

Trying to debug a program in Scilab, I inserted a couple
of "print" instructions to track what is going on.
At first, I used the %io(2) output "file" which, according
to the Help, stands for the console. Nothing happened.
Then I used an actual filename:
print("C:\Leszek\Xprmnt\scl\Ovsjanko\K3ScilabLog.txt", "START! \n \n \n \n GOING \n")
which does print to that file, but when the dust has settled
and I want to inspect the file what I find inside is just the last
message (just before the program crashed), even though there should
have been others before it, including the "START" etc in the quote above.
Apparently, every print command reopens the file for writing as a clean slate,
overwriting whatever was in it before. Looking into Help and online docs
I didn't find any options or parameters that I could use to change this.
What I want is, obviously, the output from all my print commands since the
beginning of the program, either on the console or in a text file.
TIA.
Just use disp("some text") or mprintf("format",var1,...,varn), both will display on the console. If you need to write in a file use fd = mopen("file.txt") then mfprint(fd,"format",var1,...,varn).

What are possible reasons that cmd stop writing to a file with redirection?

This is on win7.
I got a batch script that executes a C++ program and take all of its output to the file with ">".
The program takes input from servers and display everything. We need all these information so we log all these outputs down to a file. But after a short while, we see that the program stops writing to the file and just stop there while the program continues running.
The file size is also at 0 byte (OS doesn't update until file is closed?) But we can see the content of the file with notepad++, but it does not seem to update any longer.
There are about 250,000 lines long and we see that our data simply got cut off in the end. For example, suppose you should have a table of data that lists out 123 567 436 975, we only see 123 567 43. The whole line isn't even finished in the end.
There are a lot of things to write down and there are lots of network transmission. Does the program simply give up outputting when there are too much data? Is there a way around this?
Try to disable buffering. setbuf(stdout, NULL);.
Anyway, in new versions of windows, when a file is being created and data is being written (the clasic >file scenario), the grow of the file is not always visible.
In this case, dir command shows a 0 bytes file, or stops to show increasing values.
Try to read the file with type file >nul and then dir file. This "should" refresh the file size information. But it is not needed. The file is growing, just not showing it.

Is there a way to clear a file context after you've written data in it?

I have this program where I open a file and write data points in it but the problem is I have to do that inside a loop. it goes:
file1=importdata('myfile.txt','%s')
for k=1:1:128
fid=fopen('myfile2.txt','w+') % I write input to that file and pass it to my exe file
fprintf(fid,'input1')
fprintf(fid,'input2')
fprintf(fid,'input3')
the 4th input (input4) is being taken from a diff file.txt and
input4=sscanf(file1{k},'%s')
Val=str2double(input4)
fprintf(fid,'%.3f',Val)
fclose(fid)
[status,result]=system('command<myfile2.txt')
M= sscanf(result,'%s')
more_result=[ Val M]
Fid2=fopen(myfile3.txt,'w+')
frpintf(Fid2,'%s', more_result)
end
This is a vague idea of the code.
Then I sscanf the results to get the a specific value (M) that I want.
I want to write Val and Z in another file but I only get the last value of each in the file because fopen(fid,'w+') keeps updating inside the loop. Using a+ plus doesn't help and it keeps appending and never updates after the program is done running.
RIght now I am using a+ then I manually delete the content of that file after i'm done running..writing outside the loop gives me error.
Is there a way I can clear the file after each run?
I think if you open just once with w+, you can write multiple times. The first time will be the beginning of the file and after that data will be appended.
Put opening and closing outside the loop. Before the loop you put
fid=fopen('myfile2.txt','w+')
Fid2=fopen('myfile3.txt','w+')
And you can put closing after the loop
fclose(fid)
fclose(Fid2)
If this really really does not work, then I suggest opening the file once before the loop with w' which will empty the contents. Then you can use a+ inside the loop to append data. You will have a clean file each time.

Prolog - Why is 'end-of-file' written to the output text file?

I'm working on a programming assignment and I was wondering if somebody could help me out with this issue. This assignment says to write a program in Prolog which takes the text from an input text file and write it to an output text file. In order to get the location of the text files, the user needs to be prompted to write the path of the text files.
I have figured out how to do it, but I have one small issue that is really annoying. Here is my code:
main:-
%Ask the user for the input text file and then open the file
write('Please enter the filename you would like to read from:'),
nl,
read(X),
open(X,read,In),
%Ask the user for the output text file and then open the file
write('Please enter the filename you would to write to:'),
nl,
read(Y),
open(Y,write,Out),
%Read in characters from the input text file and then put them
%on the output text file.
tell(Out),
repeat,
get_char(In,T),
write(T),
T == end_of_file, !,
close(In),
told,
close(Out).
Let's say the text file that is going to be read says "this is a test". My issue is if I use the program to save this text and write it to another text file, it will write "this is a testend_of_file" instead.
I realize that this is happening because the loop isn't being terminated at the right time, but I'm not sure how to go about fixing the loop so "end_of_file" doesn't get accidentally written to the text file as well. Any help would be much appreciated. I feel like I've tried everything.
You first do write(T), and after that your testing for T == end_of_file, so no surprise end_of_file will be written.
Try ( T == end_of_file -> ! ; write(T), fail ),
What Prolog system are you using, BTW?

Resources