Netherlands
Asked — Edited

Ez-Script Modular File Write Question

hi all, i was just wondering a thing.

(example) you have the following code:


read file if file exists : create a variable for each row with data empty file modify variables for each data row if needed write file else: write file with default values


i know this is possible wih ez-script, but is it also possible with a given amount of rows, without modifying the code? if so, how? :s

note: this might be usefull for when adding things to robot projects which save data to files, and in the end, it will save time coding if it is modular code.:)


ARC Pro

Upgrade to ARC Pro

Unleash your creativity with the power of easy robot programming using Synthiam ARC Pro

PRO
Synthiam
#1  

That's not how a file has ever been used before. Operating systems allow you to rewrite or append to a file - not modify contents at specific locations very easily. What is it you are wanting to achieve? I'll see what I can do to help

#2  

well, i am basically asking if there there can me a modular version of this

United Kingdom
#3  

Is it me or does that not make sense?.. (could be me, running on no sleep again)

Quote:

but is it also possible with a given amount of rows, without modifying the code?
That's the bit I struggle to understand...

As DJ pointed out, you can rewrite a file or you can append to the file.

If neither of those options are suitable for whatever it is you are attempting, let us know what you are trying to achieve there may be a different method available.

#4  

hmm... i know how to archieve it in php, so i'll give you the example of what i mean in the php language: (please don't mind any bad coding, as its just an example i wrote in a few minutes)


<?php
//modular text function
//you can include this function and use it with any file, without having to change this code
//aka, with a modular input;)
//code by cosplaying_bunny
function modular_text_write($file,$row,$data_array,$mode) {
	$mode = (int)$mode;
	$row = (int)$row;
	if ($mode == 0) { //create file
		$filehandle = fopen($file, 'w' ) or die("can't open file, check permissions." );
		fclose($filehandle);
		return;
	} elseif  ($mode == 1) { //read file
		$filehandle = fopen($file, 'r' ) or die("can't open file, check permissions." );
		fclose($filehandle);
		return;
	} elseif  ($mode == 2) { //read given line
		$filehandle = fopen($file, 'r' ) or die("can't open file, check permissions." );
		$i=0;
		while (!feof($filehandle)) {
			$line = fgets($filehandle)
			if ($i == $row){
				return $line;
			}
			$i++;
		}
		fclose($filehandle);
	} elseif ($mode == 3) { //overwrite file
		$filehandle = fopen($file, 'w' ) or die("can't open file, check permissions." );
			foreach ($data_array as $row) {
				fwrite($file, $row."/n" );
			}
		fclose($filehandle);
		return;
	} elseif ($mode == 4) { //add lines to file
		$filehandle = fopen($file, 'a' ) or die("can't open file, check permissions." );
			foreach ($data_array as $row) {
				fwrite($file, $row."/n" );
			}
		fclose($filehandle);
		return;
	} else {
		return false;
	}
	
}
?>



#5  

You would probably have to use the SDK. I'm only really familiar with C# in C# I know you can read a file using the string reader function/method. Not sure on what the code would look like off hand and its kind of complicated but in short: Check for file, If file exists start reading it with the string reader. That will basically turn the whole file into a searchable string. Then check the string for what you want to replace. Get it's starting position in the string. Now that you know where the info is start writing at that position. I can't remember but, I know there's a way to start writing at a specific position.

I know this is a crummy explanation but maybe someone can fill it in better. I know it can be done in C# because I had to do something similar this summer for an IT Specialist course. Granted, it was only a 10 line text file and I'm fuzzy on the details but my logic is sound.:) But I'm pretty sure you'd need the SDK to do this.

PRO
Synthiam
#6  

That php example does not allow you to modify specific lines of a file. That php example does what the EZ-Script file commands do, and less. The php example...

  • Creates a File

  • Read a File (means open a file for read)

  • Read a specific line from a file

  • Overwrite a file (delete the contents and start from scratch)

  • Append to the file

Which of those would you like help with? Have you looked at the File Operations Example Project that is included with ARC?

#7  

you're looking at the wrong place of the script, @dj:p (yeah, i made the code a bit too long maybe ^^; ) you only needed to look at this:

function modular_text_write($file,$row,$data_array,$mode) 

by which i mean: execute the script below with different inputs, like a custom function;) there is the scriptstart in the ControlCommand function, but it doesn't allow custom parameters (like $file, $row, $data_array and so) as in my custom php function.;)

i hope i'm clear enough now, as i don't really know how to explain it precisely..

edit: nevermind, i just saw this in the script documentation: "Variables are global throughout the entire ARC environment" this solves my whole problem. so it isn't needed at all, sorry for bothering you all with this relatively dumb question blush

and thank you all for your support!:D

PRO
Synthiam
#8  

Glad you figured it out:D Awesome!

#9  

The only truly dumb question is the one not asked :)