A Scripting Language for Web, Linux and Windows

A Scripting Language for Web, Linux and Windows

Example: Working with Input and Files

The follwing system functions and file functions can be used to work with the standard input and files.

<?v1

// Read single character from input
getch ();
print (
c);

// Read single character from input as keyboard/ascii number
getch (true);
print (
c);

// Read line from input with text message
result input ("Type in something: ");
print (
"You typed in: "result);

// If console is off, keyboard input will not be shown
console_echo (0); 
result input ("Type in some secret: ");
print (
"\r\nYou typed in: "result);

?>
<?v1

// Read file data from standard input
// Example: v1 script.v1 < file.dat
fh fopen ("v1://stdin");
print (
"Input size: "filesize (fh));

pos 0;
while (!
feof (fh)) {
    
line fgets (fh);
    print (
"Pos "pos"\t: "line);
    
pos=ftell(fh);
}

?>
<?v1

// Read file line by line
if (fh fopen ("myfile.txt")) {
  
line "";
  while (
freadln (fhline)) {
    
tokens explode (";"line);
    
print_r (tokens);  
  }
  
fclose (fh);
}

?>
<?v1

// Read a file in blocks (default block size is 64 KB)
if (fh fopen ("myfile.bin")) {
  
block "";
  while (
bytesRead=fread (fhblock)) {
    
printf ("Block of %u bytes read"bytesRead);

    
// Do something with the block
    // ...
  
}
  
fclose (fh);
}

?>
<?v1

// Create a file and write binary data
if (fh fopen ("myfile.txt""w+")) {
  
binary "\x56\x31\x20\x53\x63\x72\x69\x70\x74\0"// Binary representation of V1 Script with zero byte
  
binary.=binformat (424); // Add 42 as 64 Bit binary number
  
binary.=binformat (423); // Add 42 as 32 Bit binary number
  
binary.=binformat (42); // Add 42 as binary number with system byte alignment (4 on 32 Bit, 8 on 64 Bit versions of V1)
  
fwrite (fhbinary);
  
fclose (fh);
}

?>
<?v1

// Read a file from specific position
if (fh fopen ("myfile.txt""r")) {
  
fseek (fh3); // Set file pointer
  
"";
  
fread (fhc1);  // Read 1 Byte
  
print (c); 
  
fclose (fh);
}

?>

back to Home