question

BING SYUAN L avatar image
0 Likes"
BING SYUAN L asked BING SYUAN L commented

Write infor to a txt file with a newline everytime

write-infor-to-txt-file.fsmHello

I would like to know how to write a string data to a txt file that do not overlape the previous data and write to a newline everytime.(The result I need as following pic).

My model as attachment. Thank you so much.

FlexSim 18.2.3
fileclosefileopenfpt
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Arun Kr avatar image
1 Like"
Arun Kr answered

@BING SYUAN L,

Adding on to Joshua's answer, you can first write the data into a global table whenever the token enters the custom code activity in the process flow. And then you can print the whole global table into the text. The code will look like this.

Table GlobalTable = Table("GlobalTable1");// Table Reference
GlobalTable.addRow();
int NumRows = GlobalTable.numRows;
GlobalTable[NumRows][1]= "Time";
GlobalTable[NumRows][2]= Model.time;
GlobalTable[NumRows][3]= "Hello World";

fileopen("C:/Users/Arun/Desktop/Data.txt");
for (int i = 1; i <= GlobalTable.numRows; i++){
	fpt(GlobalTable[i][1]);
	fpf(GlobalTable[i][2]); 
	fpt(GlobalTable[i][3]);
	fpr();
}
fileclose();

Also, set the global table to delete all rows on reset.

Pls, see the attached model.support.fsmdata.txt

Regards,

Arun KR


support.fsm (24.9 KiB)
data.txt (9.7 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

BING SYUAN L avatar image BING SYUAN L commented ·

@Arun KR

Wow, thank you so much.

It's what I need, I will try it.

0 Likes 0 ·
Joshua S avatar image
0 Likes"
Joshua S answered BING SYUAN L commented

There is a list of commands you can use to perform different file edits. You can find them under Help>Commands, and in the section called "Output". Here is a simple example for what you want:

fileopen("C:/Users/joshua.stevens/Documents/testtext.txt");
for (int i = 1; i <= 3; i++){
	fpt("Time:");
	fpd(i * 10); 
	fpt(" Hello World");
	fpr();
}
fileclose();
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

BING SYUAN L avatar image BING SYUAN L commented ·

HI @Joshua S

Thanks for your help. But it is not what I want. My ideal as below.

It will print hello word In time 0, and after a 10 second delay time in processflow, when it run to this custom code in processflow, it will print hello wrod again in time 10 and so on...everytime it print in a newline and do not overlape the word last time it printed. I would like to record the what time it run to this custom code in the model everytime.

0 Likes 0 ·