Oracle 读写文件美高梅赌堵59599: bfilename,模拟acm测试系统

2020-03-28 作者:联系我们   |   浏览(156)

project    --    options    --    directories/conditionals里面:  
   
output   directory:  
unit   output   directory:  
   
拜见设置是还是不是和文件所在的目录雷同。。。

1.最核心的DBMS_OUTPUT.PUT_LINE()方法

Oracle 读写文件 bfilename [实例]
Create directory让大家能够在Oracle数据库中灵活的对文本进行读写操作,非常大的增高了Oracle的易用性和可扩张性。
其语法为:
CREATE [OR REPLACE] DIRECTORY directory AS 'pathname';

模拟acm测验系统

检查OUTPUT   FILE   PATH   的门路设置  
检查OUTPUT   FILE   PATH是不是有写权限  
检查OUTPUT   FILE   PATH中的*.*是不是有写珍贵

甭管在什么地方,只倘使BEGIN和END之间,就足以采纳DBMS_OUTPUT.PUT_LINE(output);

该案例具体制造如下:
create or replace directory exp_dir as '/tmp';  

  • 1. 前言
  • 2. 代码

不过那会有二个标题,就是选取该函数一遍最三只可以够显得2伍十个字符,不然缓冲区会溢出。

目录创立以往,就能够把读写权限付与特定客户,具体语法如下:
GRANT READ[,WRITE] ON DIRECTORY directory TO username;

模拟acm测验系统

此外,函数DBMS_OUTPUT.ENABLE(二〇〇三0卡塔尔国这种函数,仅仅是安装总体进度的全体出口缓冲区大小,而非DBMS_OUTPUT.PUT_LINE(卡塔尔的缓冲区大小。

例如:
grant read, write on directory exp_dir to eygle;  

1. 前言

多年来和恋人在做一些acm的教练,尽管算法功力相比较虚弱,但对acm的测验系统发生

了好几感兴趣,于是就尝试接受shell脚本做了同一袖珍的模拟程序。

 

运转条件至关心重视假使linux和mac。小编的景况是mac。近期只扶助c语言。

 

重大作用有:

  1. 第三遍运转时,会初步化设置你的类型根目录,并问询是不是将脚本参与到PATH
  2. 基于客商输入的标题命名,生成标题目录,包罗源文件,测验数据文件,期待数据文件
  3. 能够生成难标题录的时候,选用是还是不是在决定台输入测量检验和愿意数据
  4. 依靠测量试验数据文件运营源代码,并扭转结果输出文件,和梦想数据文件进行比对,输出比对结果
  5. 提供运维时刻的测量检验,微秒品级,但是要求设置python
  6. 开行使用gdb命令调节和测量检验程序
  7. 一对大致的错误处理

优点:

因为acm标题要求规范的输入和输出,那样变成测量试验的时候倒霉开展大数据量的测量试验。
诚如的做法是在源代码中投入读测验文件的代码,再在提交前再改过成标准输入那几个代码,
也许使用重定向的宏,提交前再注释那几个宏。说来讲去认为有一点麻烦。
美好的场所是本土的代码直接一点儿也不动的交付上去。那么些本子正是为了实现这一个目标而实行贯彻的。

注意:

因为在mac景况中,c语言的编写翻译器暗许选项clang,若无安装,则选择gcc。

在测验时间的功效里,是调用python来完结的,mac下并未有找到系统自带的好用的时光命令。

本次的台本编辑撰写,也足以是用作是一次shell命令学习,挺风趣,但是工具不根本,算法的加强

才是珍视啊~~

想尝试的同桌,复制代码,保存到名叫acm_manage.sh的公文中,在赋上可进行权限,运转./acm_manage.sh

就能够依据提醒新闻立即体验。

 

应接留言切磋。

 

(4.8更新)
有贰个致命错误:

事前是利用

while read -r line

do
   echo $line | $EXECUTABLE_FILE_PATH >> $RESULT_FILE_PATH
done < $TEST_DATA_FILE_PATH

来逐行读取文件,并运路程序,那样表面上能够运维通过有些主题材料,但是一个

重在的标题是,它不能够处理需求连接读取多行内容,再运营程序的难题。因为

它是每读取一行,就再一次运营一遍main函数。显明是错的。。。。

 

改成一种轻便的方法,三回性读取全体剧情,并运行main函数:

cat $TEST_DATA_FILE_PATH | $EXECUTABLE_FILE_PATH >> $RESULT_FILE_PATH

 
(4.12更新)

新增添gdb自动从测量试验文件输入数据效率

原来是这般:
debug_code()
{
    echo "Debug is start."
    $DEBUG_TOOL $EXECUTABLE_FILE_PATH
    echo "Debug is done."
}

改成:
debug_code()
{
    echo "Debug is start."
    echo "start < $TEST_DATA_FILE_PATH" > $DEBUG_COMMAND_FILE_PATH
    $DEBUG_TOOL -x $DEBUG_COMMAND_FILE_PATH $EXECUTABLE_FILE_PATH
    rm $DEBUG_COMMAND_FILE_PATH
    echo "Debug is done."
}

完毕情势是,使用-x命令,从文件中读取gdb shell的下令,在此地运用start带参数的格局来形成

gdb的重定向输入。所以小编就先创设贰个这么的一声令下文件,调节和测量试验完成的时候再删除。在此之前运用gdb -ex

尝试间接输入运行命令,好像不成事。方今不通晓还或者有未有此外的情势。

(4.13更新)
去掉编写翻译-O2优化增选,便于调节和测量试验。改成:
OPTIMIZE_OPTION=
在调节和测量检验时,需求重新编写翻译代码,从前从没写。。。
select_compile_tool
compile_code
走入到终极while循环部分的,debug分支中去。

对于超越253个字符的变量,使用DBMS_OUTPUT.PUT_LINE(卡塔尔(قطر‎方法是未有效的。传说在Oracle10中,消除了这些界定,能够出口大肆大小的字符串。

这时候客商eygle就具有了对该目录的读写权限。

2. 代码

#!/bin/bash

#################################################################################################
# Test Shell Script for acm program.
#
# Usage is:
# ./acm_manage.sh i questionName
# ./acm_manage.sh r questionName
# ./acm_manage.sh d questionName
# ./acm_manage.sh h
# ./acm_manage.sh help
#
# Main Function is:
# 1. init acm directory and create source, test, expect files for you.
# 2. run your code with your test data file, and check its correctness with your expect file.
# 3. debug your code with your test data file.
#
# Platform:
# Linux, Mac OSX, Unix
#
# Support Program Language:
# C
#
# Author:
# Hanks
#
# Version:
# 1.0
#################################################################################################

########################
# varibles define start
########################

# script name
SCRIPT_NAME="acm_manage.sh"

# diretory for question
QUESTION_DIR=
# question name
QUESTION_NAME=

# file name prefix
TEST_FILE_PREFIX="test_"
EXPECT_FILE_PREFIX="expect_"
RESULT_FILE_PREFIX="result_"

# file type
SOURCE_FILE_TYPE=".c"
DATA_FILE_TYPE=".txt"

# path for test data file
TEST_DATA_FILE_PATH=
# path for executable file
EXECUTABLE_FILE_PATH=
# path for result file
RESULT_FILE_PATH=
# path for source code file
SOURCE_CODE_PATH=
# path for expect data file
EXPECT_FILE_PATH=
# default executable file name
DEFAILT_EXECUTABLE_FILE_NAME="a.out"
# gdb command temp file
DEFAULT_DEBUG_COMMAND_FILE_NAME="gdb_cmd_temp"
DEBUG_COMMAND_FILE_PATH=

# cost second time
RUN_START_TIME=
RUN_END_TIME=
TIME_COST=

# accept time is 3 millisecond
ACCEPT_TIME=3000

# make format of each line of content from echo be corrent
IFS="
"

# init compiler command, default is clang, if no, use gcc
COMPILE_COMMAND=
DEBUG_OPTION="-g"
OPTIMIZE_OPTION=
DEBUG_TOOL="gdb"

# acm workspace existed flag
IS_WORKSPACE_NOT_EXIST=false
IS_ACM_ROOT_NOT_EXIST=false

#config variable
SCRIPT_DIRECTORY=$(cd "$(dirname "$0")"; pwd)
ACM_ROOT_NAME=
ACM_ROOT=
CONFIG_FILE_NAME="$SCRIPT_DIRECTORY/acm_manage.ini"
SET_PATH=

######################
# varibles define end
######################

#######################
# function define start
#######################

# add acm_manage.sh to your PATH environment varibale,
# so you can use this command defaultly.
set_script_to_path()
{
    # ask user whether to add to path or not
    loop=true
    while $loop; do
    read -n1 -p "Add this script to your PATH to run anywhere. [y/n]?" answer
        case $answer in
            Y | y)
                echo
                echo "Fine, continue."
                SET_PATH=true
                loop=false
                ;;
            N | n)
                echo
                echo "Ok, I got it. Donot set to path."
                SET_PATH=fasle
                loop=false 
                return
                ;;
            *)
                echo "Error choice, please answer with y or n."
                ;;
        esac
        echo
    done
    PROFILE_PATH=~/.profile
    BASH_PROFILE_PATH=~/.bash_profile
    OS_TYPE=`uname`

    ADD_PATH_COMMAND="export PATH=$PATH:$SCRIPT_DIRECTORY"
    TARGET_PROFILE_PATH=

    if [ $OS_TYPE = "Darwin" ]; then
        echo "You are a Mac system user."
        TARGET_PROFILE_PATH=$PROFILE_PATH
    elif [ $OS_TYPE = "Linux" ]; then
        echo "You are a Linux system user."
        TARGET_PROFILE_PATH=$BASH_PROFILE_PATH
    elif [ $OS_TYPE = "FreeBSD" ]; then
        echo "You are a FreeBSD system user."
        TARGET_PROFILE_PATH=$BASH_PROFILE_PATH
    fi
    echo "Start to add this script to your path."
    echo "#Add acm_manage.sh command to your PATH" >> $TARGET_PROFILE_PATH
    echo $ADD_PATH_COMMAND >> $TARGET_PROFILE_PATH
    echo "Add path is done. You can check $TARGET_PROFILE_PATH."
    echo "So you can run this script anywhere. Enjoy."
}

# print help info for user
print_help()
{
    echo
    echo "Usage: acm_manage.sh [i|r|d|h|help] questionName"
    echo "Options: These are optional argument"
    echo " i init acm directory, create directory, source, test file and expect file automatically for you."
    echo " h|help show help info."
    echo " r run your code with your test data. And diff output and expect file to check correctness "
    echo " d start debug tool (like gdb, lldb) to debug your code."
    echo
    echo "When you first run this shell, you should "
    echo "enter the directory the script is in "
    echo "and run the script. Have fun."
    echo
}

read_conf_info_from_file()
{
    ACM_ROOT=`**cat $CONFIG_FILE_NAME | grep ACM_ROOT | awk -F"=" '{print $2}'`
    SET_PATH=
`**cat $CONFIG_FILE_NAME | grep SET_PATH | awk -F"=" '{print $2}'`
    echo "ACM_ROOT is $ACM_ROOT"
    echo "SET_PATH is $SET_PATH"
}

create_conf_file()
{
    # create configure init file
    echo "ACM_ROOT=$ACM_ROOT" >> $CONFIG_FILE_NAME
    echo "SET_PATH=$SET_PATH" >> $CONFIG_FILE_NAME
    echo
    echo "Create config file $CONFIG_FILE_NAME in the current directory."
    echo
}

set_acm_root()
{
    echo "This is your first time and last time to see this message, just config some info. ^_^"
    echo "Please input your acm root directory name, it will be created in your current directory:"
    read ACM_ROOT_NAME
    ACM_ROOT=$SCRIPT_DIRECTORY/$ACM_ROOT_NAME/
    if [ ! -d $ACM_ROOT ]; then
        mkdir $ACM_ROOT
        echo "Create $ACM_ROOT directory for you."
    fi
}

init_acm_root_directory()
{
    if [ ! -f $CONFIG_FILE_NAME ]; then
        # config file is not existed, create it
        set_acm_root
        set_script_to_path
        create_conf_file
        echo "Now you can see usage to rock acm."
        print_help
        echo
    else
        # read config info from file and init ACM_ROOT
        read_conf_info_from_file
    fi
}

# init files path
init_path()
{
    echo
    # build paths for files
    QUESTION_DIR=$ACM_ROOT$QUESTION_NAME
    #echo "questoin directory is $QUESTION_DIR"
    SOURCE_CODE_PATH=$QUESTION_DIR/$QUESTION_NAME$SOURCE_FILE_TYPE
    echo "Source file is $SOURCE_CODE_PATH"
    TEST_DATA_FILE_PATH=$QUESTION_DIR/$TEST_FILE_PREFIX$QUESTION_NAME$DATA_FILE_TYPE
    #echo "test data is $TEST_DATA_FILE_PATH"
    EXPECT_FILE_PATH=$QUESTION_DIR/$EXPECT_FILE_PREFIX$QUESTION_NAME$DATA_FILE_TYPE
    #echo "expect is $EXPECT_FILE_PATH"
    EXECUTABLE_FILE_PATH=$QUESTION_DIR/$DEFAILT_EXECUTABLE_FILE_NAME
    #echo "executable is $EXECUTABLE_FILE_PATH"
    RESULT_FILE_PATH=$QUESTION_DIR/$RESULT_FILE_PREFIX$QUESTION_NAME$DATA_FILE_TYPE
    #echo "result is $RESULT_FILE_PATH"
    DEBUG_COMMAND_FILE_PATH=$QUESTION_DIR/$DEFAULT_DEBUG_COMMAND_FILE_NAME
}

# init source code template, default is c language
init_source_code()
{
    echo "#include <stdio.h>" >> $SOURCE_CODE_PATH
    echo >> $SOURCE_CODE_PATH
    echo "int main(int argc, char *args[]) {" >> $SOURCE_CODE_PATH
    echo >> $SOURCE_CODE_PATH
    echo " return 0;" >> $SOURCE_CODE_PATH
    echo "}" >> $SOURCE_CODE_PATH
}

# init test and expect data file from user input
init_test_and_expect_data()
{
    loop=true
    while $loop; do
        read -n1 -p "Do you want to input test and expect data now. [y/n]?" answer
        case $answer in
            Y | y)
                echo "Input start:"
                echo "Please pay attention to the command to end your input."
                echo "or else you will need modify these file by yourself again."
                echo
                echo "Please input your test input data and Press Enter and then ctrl+D to end input:"
                cat > $TEST_DATA_FILE_PATH
                echo "Please input your expect output data and Press Enter and then ctrl+D to end input:"
                cat > $EXPECT_FILE_PATH
                echo "Input end."
                loop=false
                ;;
            N | n)
                touch $TEST_DATA_FILE_PATH
                touch $EXPECT_FILE_PATH
                echo
                echo "Create empty test and expect file. You need add test data by yourself."
                loop=false
                ;;
            *)
                echo "Wrong input, please answer just by y or n."
                ;;
        esac
     done

}

# create acm directory and files for user
init_acm_workspace()
{
    echo "Init acm workspace for you."
    # if directory is not existed, create a new one
    # or else, do nothing to protect the existed source files
    if $IS_WORKSPACE_NOT_EXIST; then
        # create acm diretory
        mkdir $QUESTION_DIR

        # init source and test data
        init_source_code
        init_test_and_expect_data
        echo "Init is done, let's rock."
    else
        echo "Sorry, workspace is already exsited, you can start rock."
    fi

    # if directory existed, do nothing
    return
}

# check workspace existed
workspace_exist_check()
{
    if $IS_WORKSPACE_NOT_EXIST; then
        echo "Workspace "$QUESTION_NAME" is not existed. Please use command "$SCRIPT_NAME i $QUESTION_NAME" to init workspace."
        exit 1
    fi
}

compile_code()
{
    echo "Compile start."
    $COMPILE_COMMAND $DEBUG_OPTION $SOURCE_CODE_PATH $OPTIMIZE_OPTION -o $EXECUTABLE_FILE_PATH
    echo "Compile end."
}

run_code()
{
    # delete result file firstly if existed, to avoid
    # result confict with old one
    echo
    echo "Clear result file."
    rm $RESULT_FILE_PATH > /dev/null

    # create a new empty result file
    touch $RESULT_FILE_PATH

    # run code with test data, and redirect output to result file
    echo
    echo "Start to run code:"
    # record start time, %N means nanoseconds
    # second is 1
    # millisecond is 0.001
    # macrosecond is 0.000001
    # nanosecond is 0.000000001
    #RUN_START_TIME=`date +%s%N`
    # get total millisecond from epoth
    RUN_START_TIME=$(python -c 'import time; print int(round(time.time()*1000))')
    #while read -r line
    #do
    #    echo $line | $EXECUTABLE_FILE_PATH >> $RESULT_FILE_PATH
    #done < $TEST_DATA_FILE_PATH
    cat $TEST_DATA_FILE_PATH | $EXECUTABLE_FILE_PATH >> $RESULT_FILE_PATH
    # record end time
    RUN_END_TIME=$(python -c 'import time; print int(round(time.time()*1000))')
    echo "Run is done."
}

print_run_time_cost()
{
    echo
    # cost in milliseconds
    TIME_COST=$((RUN_END_TIME-RUN_START_TIME))
    # cost in seconds
    SECOND_TIME_COST=`**echo "$TIME_COST / 1000 " | bc -l`     echo** "Run time cost is ${SECOND_TIME_COST:0:4} seconds."
}

print_output()
{
    echo
    echo "Output is:"
    cat $RESULT_FILE_PATH
}

judge_result()
{
    echo
    echo "Diff result is:"
    if diff "$RESULT_FILE_PATH" "$EXPECT_FILE_PATH"; then
        if [ $TIME_COST -le $ACCEPT_TIME ]; then
            echo "Accept. Congratulations"
        else
            echo "Time limit exceeded. Cost time $TIME_COST > Accept time $ACCEPT_TIME milliseconds"
        fi
    else
        echo "Wrong answer. Try again."
    fi
}

debug_code()
{
    echo "Debug is start."
    echo "start < $TEST_DATA_FILE_PATH" > $DEBUG_COMMAND_FILE_PATH
    #$DEBUG_TOOL $EXECUTABLE_FILE_PATH
    $DEBUG_TOOL -x $DEBUG_COMMAND_FILE_PATH $EXECUTABLE_FILE_PATH
    rm $DEBUG_COMMAND_FILE_PATH
    echo "Debug is done."

}

# shift command arguments for while loop process
skip_command()
{
    shift 2
}

# detect clang installed, or else use gcc
select_compile_tool()
{
    which clang > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        COMPILE_COMMAND="clang"
    else
        COMPILE_COMMAND="gcc"
    fi
    echo "Use $COMPILE_COMMAND to compile source code."
}

#######################
# function define end
#######################

##################
# Main part start
##################

# if no right number of argument, show help and exit
# if use h or help command, show help and exit
if [ "$1" = "h" ]
then
    print_help
    exit 0
elif [ "$1" = "help" ]
then
    print_help
    exit 0
elif [ $# -lt 2 ]
then
    print_help
    exit 1
fi

init_acm_root_directory

# get question name and init path for all files
QUESTION_NAME="$2"

init_path

# check if workspace is existed.
if [ ! -d $QUESTION_DIR ]; then
    IS_WORKSPACE_NOT_EXIST=true
fi

# option argument command implementation
while [ "$1" ]
    do
    echo
    if [ "$1" = "i" ]; then
        init_acm_workspace
    elif [ "$1" = "r" ]
    then
        workspace_exist_check
        echo "Run code with your test data and check correctness."
        select_compile_tool
        compile_code
        run_code
        print_output
        print_run_time_cost
        judge_result
    elif [ "$1" = "d" ]
    then
        workspace_exist_check
        select_compile_tool
        compile_code
        echo "Debug your code with debug tool, default is gdb"
        debug_code
    else
        echo "$SCRIPT_NAME does not recognize option $1."
        print_help
        exit 1
    fi
    # skip command for loop argument process now
    # maybe there are some extensions in the future
    shift 2
done

##################
# Main part end
##################

来源为知笔记(Wiz卡塔尔

 

 

 

 

 

 

 

 

 

declare

让我们看七个粗略的测量试验:
SQL> create or replace directory UTL_FILE_DIR as '/opt/oracle/utl_file';Directory created.
SQL> declare  
 2    fhandle utl_file.file_type;
 3  begin  
 4    fhandle := utl_file.fopen('UTL_FILE_DIR', 'example.txt', 'w');  
 5    utl_file.put_line(fhandle , 'eygle test write one');  
 6    utl_file.put_line(fhandle , 'eygle test write two');  
 7    utl_file.fclose(fhandle);  
 8  end;  
 9  /
 PL/SQL procedure successfully completed.
 SQL> !
 [oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt eygle test write oneeygle test write two[oracle@jumper 9.2.0]$  

output varchar2(200);

好像的大家得以由此utl_file来读取文件:
SQL> declare  
 2    fhandle   utl_file.file_type;  
 3    fp_buffer varchar2(4000);  
 4  begin  
 5    fhandle := utl_file.fopen ('UTL_FILE_DIR','example.txt', 'R');  
 6    
 7    utl_file.get_line (fhandle , fp_buffer );  
 8    dbms_output.put_line(fp_buffer );  
 9    utl_file.get_line (fhandle , fp_buffer );
 10    dbms_output.put_line(fp_buffer );
 11    utl_file.fclose(fhandle);
 12  end;
 13  /
 eygle test write one
 eygle test write two
 PL/SQL procedure successfully completed.  

begin

可以查询dba_directories查看全数directory.
SQL> select * from dba_directories;
      OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH

output:=...; //赋值

      ------------------------------ ------------------------------

      SYS                            UTL_FILE_DIR                   /opt/oracle/utl_fileSYS                            
                                      BDUMP_DIR                      /opt/oracle/admin/conner/bdumpSYS                                                         EXP_DIR                        /opt/oracle/utl_file  

能够选择drop directory删除那些路线.
SQL> drop directory exp_dir;
Directory dropped
SQL> select * from dba_directories;
OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH



SYS                            UTL_FILE_DIR                   /opt/oracle/utl_fileSYS                            
                               BDUMP_DIR                      /opt/oracle/admin/conner/bdump  

create or replace directory USER_DIR as 'E:PLSQL310';

DECLARE
   v_content VARCHAR2(1800);
   v_bfile BFILE;
   amount INT;
   offset INT :=1;
   
BEGIN
v_bfile := bfilename('USER_DI奥迪Q5','test.TXT'卡塔尔; -- 注意这里的 User_dir 对应下边已经创办好啊的目录
amount :=DBMS_LOB.getlength(v_bfile);
DBMS_LOB.OPEN(v_bfile);
DBMS_LOB.READ(v_bfile,amount,offset,v_content);
DBMS_LOB.close(v_bfile);
DBMS_OUTPUT.PUT_LINE(v_content);
END;

DBMS_OUTPUT.PUT_LINE(output);

end;

万一上述试行结果成功但未曾展现请实践 set serveroutput on; 然后就足以了;

本文由美高梅赌堵59599发布于联系我们,转载请注明出处:Oracle 读写文件美高梅赌堵59599: bfilename,模拟acm测试系统

关键词: