ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
|
Downloads |
Q&A Using Flexsim and building models |
#1
|
||||
|
||||
Read strings separated with a semi-colon from an Ascii file
Community,
I had a task to do and solved it, but I like to discuss if my solution is a good one (code, speed, etc.) or if someone knows a better solution or have at least some ideas or hints how to do it in a more elegant way. I only want to use Flexscript. So no C++ code and therefore no compiling. All I want to do is to read from an Ascii file strings which are separated with semi-colons (";"). My file (Sample_AsciiFileRead_TD.txt) looks like this: Code:
12345;123;98765; 54321;321;56789; Name: TD_ReadStringsFromFile Parameters: (str PathFilename) Description: User command to read strings seperated by semi-colon from a file. Example: TD_ReadStringsFromFile(concat(cdir(),"\userproject s\\SampleModels\\Sample_AsciiFileRead_TD.txt")); Code: Code:
// USER COMMAND TO READ STRINGS SEPERATED BY SEMICOLON FROM A FILE // LOCAL VARIABLES string sPathFileName = parstr(1); // PATH AND FILENAME string sReadLine = ""; // READ LINE FROM FILE int iReadLineLength = 0; // STRING LENGTH OF READ LINE string sSeperator = ";"; // STRING SEPERATOR int iPositionSeperator = 0; // POSITION OF SEPERATOR string sFoundString = ""; // FOUND STRING int iNumberOfFoundStrings = 0; // NUMBER OF FOUND STRINGS int iOutputConsole = 0; // SWITCH FOR PRINT INTO OUTPUT CONSOLE FOR DEBUG // IF FILE EXISTS if(fileexists(sPathFileName)) { // OPEN FILE FOR READ fileopen(sPathFileName,"r"); // LOOP UNTIL END OF FILE IS REACHED while(!endoffile()) { // READ LINE FROM FILE sReadLine = filereadline(); // PRINT INTO OUTPUT CONSOLE if(iOutputConsole) {pr();pt(sReadLine);} // IF NOT END OF FILE if(!endoffile()) { // // LOOP UNTIL STRING IS EMPTY while(stringlen(sReadLine) > 0) { // INCREMENT NUMBER OF FOUND STRINGS iNumberOfFoundStrings = iNumberOfFoundStrings + 1; // STRING LENGTH iReadLineLength = stringlen(sReadLine); if(iOutputConsole) {pr();pt("iReadLineLength: ");pf(iReadLineLength);} // FOUND POSITION SEPERATOR iPositionSeperator = stringsearch(sReadLine,sSeperator,1); if(iOutputConsole) {pr();pt("iPositionSeperator: ");pf(iPositionSeperator);} // STORE FOUND STRING sFoundString = stringcopy(sReadLine,1,iPositionSeperator); {pr();pt("sFoundString ");pt(numtostring(iNumberOfFoundStrings,0,0));pt(" : ");pt(sFoundString);} // NEW START POSITION FOR NEXT POSITION SEPERATOR SEARCH iPositionSeperator = iPositionSeperator + 2; if(iOutputConsole) {pr();pt("iPositionSeperator: ");pf(iPositionSeperator);} // CUT READ LINE BY ALREADY FOUND STRING sReadLine = stringcopy(sReadLine,iPositionSeperator,sReadLine - iPositionSeperator); if(iOutputConsole) {pr();pt("sReadLine: ");pt(sReadLine);} } } } // CLOSE FILE fileclose(); //WRITE TO OUTPUTCONSOLE pr();pt("TD_ReadStringsFromFile executed !"); //RETURN ONE return 1 ; } else { // PRINT INTO OUTPUT CONSOLE pr();pt("File: ");pt(sPathFileName);pt(" does not exist!"); } So in general what I am doing is to go through the file until I reach endoffile(). I search for the colon, take first string, and then cut the read string and search for the next colon until the cut read string is empty. Maybe it would have been easier if there would be a command like endofline(). But I did not found it, so that’s why I took this way. Another good command might be to have something like readc() which only reads one character. Or which would be just wonderful would be a command like readstringuntil() which reads a string from a defined position until it founds the separator, e.g. readstringuntil(1,“;”). Reads from position 1 until if founds the “;” and returns the string. But maybe I overseen something and there is already a more elegant way to do it. Any suggestions, ideas, hints? Thanks in advance. tom the (A)tom P.S:: The code looks a little bit weird, because of the format. No idea how to change it. P.P.S.: I attach my model and the files, which are not finished yet.
__________________
tom the (A)tom: "We have solved our problems ... now we have to fight the solutions." |
The Following User Says Thank You to Tom David For This Useful Post: | ||
Cliff King (03-20-2008) |
#2
|
||||
|
||||
Tom,
I think, given the commands you have, it is about the only solution. I've done similar thinks by replacing ";" and "," by "," and "." to get german csv files, but I've done it in C++ in a DLL. So it is C++ but it is no compiling and you could call it with a usercommand. And within C++ there are a lot of extra commands to deal with File operations. But again, within Flexsim, I think you did a good job. Steven |
#3
|
|||
|
|||
Creating an output file
I would like to open a file for output, similar to the file reading example in this thread. I am hoping to create the output file with a unique filename, but I do not see any type of filecreate() command within Flexscript. I would also like to be able to append to an existing file when I write to it, but do not see any way to specify the choice of either "append" or "overwrite" when writing to a file from Flexscript.
Does anyone have any suggestions on how to do these two things (creating unique output filename and appending to existing file)? Thanks very much. |
#4
|
|||
|
|||
Hi,
To create a file use fileopen. Overwrite the file use: fileopen("c:/test.txt","w") Append to a file: fileopen("c:/test.txt","a") Lars-Olof |
The Following User Says Thank You to Lars-Olof Leven For This Useful Post: | ||
Rhonda MacIntyre (03-15-2010) |
#5
|
||||
|
||||
There is an example in the download section Sample_AsciiFileReadWrite_TD, which I hope you already found
http://www.flexsim.com/community/for...do=file&id=176 But back to your questions: If you want to append to a file, do not use “w” but “a”. Write fileopen(sPathFileName,"w"); Append fileopen(sPathFileName,"a"); As far as I know will the file created if it does not exist. I checked it and it is working.
__________________
tom the (A)tom: "We have solved our problems ... now we have to fight the solutions." |
The Following 2 Users Say Thank You to Tom David For This Useful Post: | ||
Rhonda MacIntyre (03-15-2010) |
Thread | Thread Starter | Forum | Replies | Last Post |
What about the file read commands? | AlanZhang | Gripes and Goodies | 0 | 02-08-2008 05:17 PM |