Importing text file to RAPID

hello
i am trying to write and read a txt file into my rapid program, can somebody help me in that it shows no errors but i dont know where it is saved or how it is read by rapid
this is my code:

ROC write_to_file()
VAR iodev file;
!Open “HOME:/file.doc”, infile\Read;
!reg1 := ReadNum(infile);
VAR num number:= 8;
Open “Desktop:/SAJJ.TXT”, file \Read;
Write file, “The number is :”\Num:=number;
Close file;
ENDPROC

PROC read_from_file()
VAR iodev file;
VAR num number;
VAR string text;
Open “Desktop:/SAJJ.TXT”, file \Read;
TPWrite ReadStr(file);
Rewind file;
text := ReadStr(file\Delim:=“:”);
number := ReadNum(file);
Close file;
TPWrite text \Num:=number;
ENDPROC

Not sure about “Desktop:/” as a location… Maybe try "C:\FILENAME" to save it to your computer’s C drive running a virtual controller and switch to “HOME:” on a real controller.

i don’t have Home , because am doing it now on simulation only does it work ?

this line has the error ‘’ C:\FILENAME\SAJJ.TXT", file \Read; "
Unexpected unknown token.

it gives an error for ‘’ : ‘’ and for the dot in ‘’ SAJJ.TXT’’ and for the comma between the file name and the word file
i put the errors in red maybe it is easier
C:\FILENAME\SAJJ.TXT", file \Read;

and these are the error :

Syntax error(136): Unexpected ‘:’ 10/9/2013 9:22:58 AM General
Unexpected unknown token(150): Unexpected unknown token. 10/9/2013 9:22:58 AM General
Syntax error(137): Expected ‘,’ but found ‘.’ 10/9/2013 9:22:58 AM General
syntax errors. 10/9/2013 9:22:58 AM RAPID

Your code suggests you have the text file in a folder called FILENAME is that true? you are also using \read when you try to write

Open "C:" \file:= “SAJJ.TXT” , file \Write;

Do you have your text file located in a folder called ‘FILENAME’ on your C drive? because that is what your code suggests. also I think you want to do a \write instead of a \read. \read means you’re opening your file for reading

try
Open " C:" \file:= “SAJJ.TXT’” , file \Write;

i modified the command
‘Open “C:\SAJJ.TXT”, file \Write;’
i got the following error :

Not allowed command 10/10/2013 9:42:48 AM Event Log
and when i double click on the error explanation it says :

20051: Not allowed command

Description
The command is only allowed when the client is in control of the resource (program/motion).
Consequences
The system remains in the same status, and the requested action will not be performed.
Actions

  1. Check if the client is in control, by checking “Write Access” in RobotStudio.
  2. Check if the client who ought to be in control really is.

where can i get this write access ?

update : the following program gives no errors but does not write anything to the txt file :

PROC write_to_file()
VAR iodev file;
!Open “HOME:/file.doc”, infile\Read;
!reg1 := ReadNum(infile);
VAR num number:= 8;
Open “C:/file” \file:= “SAJJ.TXT’” , file \Write;
Write file, “The number is :”\Num:=number;
Close file;
ENDPROC

VAR string filelocation:=“C:\something2write.txt”;
VAR string something2write:=“blabla123”;
VAR iodev file;

PROC pWriteSomething()
Open filelocation, file \Append;
Write file, something2write \NoNewLine;
Close file;
ENDPROC

Hi , thank you so much for your reply, and regarding the code, i would like to tell you what am doing,
i write this code on rapid only targets with a procedure for thier path then the procedure you provided in the previous comment , i attached the file below, then i press the ‘’ apply ‘’ button in RAPID menu , i got the same errors about ‘’ : ‘’ and ‘’'’ in this line
VAR string filelocation:= “C**:**something2write.txt”; but when i change it to

VAR string filelocation:= “C:/something2write.txt”;

i got no errors but nothing is shown in the txt file .
please see attached a snapshot for the errors i got in rapid, and for the code
write code_soupUSA modification.txt (974 Bytes)

Hummm. Try creating a folder called “temp” on the C: drive and then change “C:/something2write.txt” to “C:/temp/something2write.txt” and see if it works.

Also, you do have a HOME file eventhough you are running a VC.

The easiest way to find it is to:

  1. click on the [Controller] tab in RobotStudio
  2. right-click on the Current Station name in the left Controller window
  3. choose Open System Folder

Change to: VAR string filelocation:= “HOME:something2write.txt”;
and it should show in your HOME folder instead of your C: drive.

Just tried the code below and it worked:

!VAR string filelocation:=“C:/temp/something2write.txt”;
VAR string filelocation:=“HOME:something2write.txt”;
VAR string something2write:=“blabla123”;
VAR iodev file;

PROC pWriteSomething()
Open filelocation, file \Append;
Write file, something2write \NoNewLine;
Close file;
ENDPROC

Hello, i found the Home thanks alot, and thanks for your patience but i created the txt file in the Home, but still not writing, shall i press something more than apply, i tried also to press synchronize to VC and synchronize to station , but still empty :frowning:

[Apply] saves your changes but doesn’t run the program. Do you know if you’ve been able to run any other code on your virtual controller?

usually when i make changes in my rapid program, in the beginning i apply changes , then i press [synchronize to station]

but if i have targets and path, i make [ synchronize to VC] then i can use them in the rapid program, that is what i know :frowning:

Demo of creating a simple station to run some RAPID:

Demo of writing to a text file using the code above: