⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.jc

📁 The JILRunOnly project is a simple command-line application written in ANSI-C that is intended to de
💻 JC
字号:
/* File.jc: A script file for testing the File object. */
import stdlib;
import File;

// file mode constants
const long kRead = 0;
const long kWrite = 1;
const long kAppend = 2;
const long kBinary = 16;

function string main(const array& args)
{
	File file = new File();

	stdlib::Printf( "* Setting file specifier to: %s\n", args[0] );
	file.fileSpec = args[0];

	stdlib::Printf( "file.path returns: %s\n", file.path );
	stdlib::Printf( "file.name returns: %s\n", file.name );
	stdlib::Printf( "file.type returns: %s\n", file.type );
	stdlib::Printf( "file.fileSpec returns: %s\n", file.fileSpec );

	stdlib::Print( "* Setting mode to read, text\n");
	file.mode = kRead;

	stdlib::Printf( "file.Exists() returns: %d\n", file.Exists() );

	stdlib::Print("* Trying to open file and read one text line\n");
	string text = "";
	if( !file.Open() )
	{
		if( file.ReadTextLine( text ) )
		{
			stdlib::Print("file.ReadTextLine() returned with an error.\n");
		}
		else
		{
			stdlib::Printf( "file.ReadTextLine() returns: %s\n", text );
		}
		stdlib::Print("* Read a second text line\n");
		if( file.ReadTextLine( text ) )
		{
			stdlib::Print("file.ReadTextLine() returned with an error.\n");
		}
		else
		{
			stdlib::Printf( "file.ReadTextLine() returns: %s\n", text );
		}
		stdlib::Print("* Read the entire remaining text file into a string\n");
		if( file.ReadText( text ) )
		{
			stdlib::Print("file.ReadText() returned with an error.\n");
		}
		else
		{
			stdlib::Printf( "file.ReadText() returns: %d characters\n", text.length );
		}
		file.Close();
	}
	stdlib::Print("* Making a copy of the File object\n");
	File newFile = file;

	stdlib::Print("* Setting file type of newFile to '.txt'\n");
	newFile.type = "txt";

	stdlib::Printf( "newFile.path returns: %s\n", newFile.path );
	stdlib::Printf( "newFile.name returns: %s\n", newFile.name );
	stdlib::Printf( "newFile.type returns: %s\n", newFile.type );
	stdlib::Printf( "newFile.fileSpec returns: %s\n", newFile.fileSpec );
	stdlib::Printf( "newFile.Exists() returns: %d\n", newFile.Exists());

	stdlib::Print("* Setting mode of newFile to write, text\n");
	newFile.mode = kWrite;

	stdlib::Print("* Trying to open newFile and write a text line\n");
	if( !newFile.Open() )
	{
		if( newFile.WriteTextLine("This is what we read from "+args[0]+".\n") )
		{
			stdlib::Print("newFile.WriteTextLine() returned with an error.\n");
		}
		stdlib::Print("* Write another text line\n");
		if( newFile.WriteTextLine("Note that the first two text lines from that file are missing.\n\n") )
		{
			stdlib::Print("newFile.WriteTextLine() returned with an error.\n");
		}
		stdlib::Print("* Write the whole remainder of the text file we have previously read in\n");
		if( newFile.WriteText(text) )
		{
			stdlib::Print("newFile.WriteText() returned with an error.\n");
		}
		newFile.Close();
	}

	stdlib::Print( "* Creating binary file 'test.bin'. Writing a long, a float and a string.\n");
	File binFile = new File("test.bin", kWrite | kBinary);
	binFile.path = file.path;
	if( !binFile.Open() )
	{
		binFile.PutLong( 27 );
		binFile.PutFloat( 3.141 );
		binFile.PutString( "Hello world!" );
		binFile.Close();
	}

	stdlib::Print( "* Trying to load data from 'test.bin'\n");
	long aLong = 0;
	float aFloat = 0;
	string aString = "";
	binFile.mode = kRead | kBinary;
	if( !binFile.Open() )
	{
		binFile.GetLong( aLong );
		binFile.GetFloat( aFloat );
		binFile.GetString( aString );
		binFile.Close();
	}
	stdlib::Printf( "binFile.GetLong() returns %d\n", aLong );
	stdlib::Printf( "binFile.GetFloat() returns %g\n", aFloat );
	stdlib::Printf( "binFile.GetString() returns %s\n", aString );

	stdlib::Print( "* Trying to rename 'test.bin' to 'hello.bin'\n");
	if( binFile.Rename( "hello.bin" ) )
	{
		stdlib::Print("binFile.Rename() returned an error.\n");
	}
	else
	{
		stdlib::Print("binFile.Rename() succeeded.\n");
	}

	stdlib::Print("* Do you want to keep the files created? Y/N ");
	text = stdlib::GetString();

	if( text != "y" && text != "Y" )
	{
		stdlib::Print( "* Cleanup: Removing newFile and binFile\n");
		if( newFile.Remove() )
			stdlib::Print( "newFile.Remove() returned an error.\n");
		if( binFile.Remove() )
			stdlib::Print( "binFile.Remove() returned an error.\n");
	}

	return "Done.";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -