Page 1 of 1

File operations (fopen, fscanf, fprintf, etc)

Posted: Tue Aug 13, 2019 12:11 pm
by AntonKulikov
Hello!

Where can I find detailed information regarding operations with text files?
What is "wt", "rt", "r+" etc.
What parameters of the fopen, fscanf, fprintf commands can generally take values
How can I navigate the lines of a file, go to the beginning or end, insert an empty line, select and replace characters... etc.
I can not find this information anywhere.

Thanks for the answer

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Tue Aug 13, 2019 6:13 pm
by TomKerekes
Hi Anton,
Where can I find detailed information regarding operations with text files?
See KMotionDef.h for available KFLOP functions, variables, and definitions. Here is an excerpt:

Code: Select all

typedef int FILE;
FILE *fopen(const char*, const char*);		   // Open a text file for writing on the PC 2nd param = "rt" or "wt" or "at"(append write text mode) 
int fprintf(FILE *f, const char * format, ...);		   // Print formatted string to the PC's Disk File
int fclose(FILE *f);                           // Close the disk file on the PC

#define MAX_READ_DISK_LENGTH 1024 // max allowed length of disk file line length
char *fgets(char *str, int n, FILE *file); //read string from PC disk file, str=buffer, n=buffer length, f=FILE pointer, returns NULL on error
int fscanf(FILE *f, const char *format, ...); //read sting from PC Disk file, convert values, returns number of items converted
int feof(FILE *f);   // End of file status for disk reading

What is "wt", "rt", "r+" etc.
KFLOP support for accessing files on the PC is very basic. Only text mode (not binary) is supported. Only:

rt - read file text mode
wt - write file text mode
at - append to file write mode

What parameters of the fopen, fscanf, fprintf commands can generally take values
please see the KMotionDef.h file. For formatting codes Google these functions to find many on-line explanations. Such as:

https://en.wikipedia.org/wiki/Printf_format_string
https://en.wikipedia.org/wiki/Scanf_format_string


How can I navigate the lines of a file, go to the beginning or end, insert an empty line, select
and replace characters... etc.
You would need to do such things yourself by reading the file line-by-line and then writing a new file line-by-line.

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Thu Aug 15, 2019 10:16 am
by AntonKulikov
Wow! I did it! Thank you very much! :)

But could you tell me if it is possible to obtain the following information in any format (for subsequent recording to a file):
1) Current time
2) Program execution time
3) The name of the executable program file (<program1.tap> for example)

Thanks for the answers, Tom!

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Thu Aug 15, 2019 4:07 pm
by TomKerekes
Hi Anton,
But could you tell me if it is possible to obtain the following information in any format (for subsequent recording to a file):
1) Current time
Not currently. Time_sec() returns seconds from power up.

2) Program execution time
You can monitor JOB_ACTIVE to determine when a Job starts and how long it has been running.

3) The name of the executable program file (<program1.tap> for example)
Not currently

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Fri Aug 16, 2019 5:58 am
by AntonKulikov
It is very, very sad...

After all, we can see in the window title bar both the file name and the program execution time.

Thus, no report on the production process using KMotion is impossible to do ...

And the active state JOB_STATUS also appears when changing a hand tool, for example.
Okay, I can think of something with the runtime of the program, but with the rest ...

Thanks Tom!

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Fri Aug 16, 2019 5:23 pm
by TomKerekes
There is already a <>\KMotion\Data\RunLog.txt file written by KMotionCNC that has Time, Job Time, and Filename that looks like:

Mon, Aug 12, 2019, 04:09:07 Elapsed Job Time: 95.3 sec C:\KMotionSrc\GCode Programs\turbothisthread.ngc
Mon, Aug 12, 2019, 04:11:32 Elapsed Job Time: 18.9 sec C:\KMotionSrc\GCode Programs\turbothis thread no G95.ngc

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Mon Mar 02, 2020 12:33 pm
by AntonKulikov
But I do not see AM&PM time in this file. And it is very unpleasant ...

Re: File operations (fopen, fscanf, fprintf, etc)

Posted: Mon Mar 02, 2020 5:52 pm
by TomKerekes
Hi AntonKulinkov,
I do not see AM&PM time in this file.
Oops. You are correct. We can offer a patch to use 24 hr format. We'd prefer that as adding AM/PM may break Code of existing Users. Does that work for you?