使用PHP

时间:2015-07-05 09:48:34

标签: php

我正在我的大学里开展一个项目,那就是为我的大学开发一个类似hackerrank的在线自动编程网站。现在,为了编译代码,我使用了sphere引擎提供的API。 API将输出作为字符串返回,然后将其存储到文件中。然后,我使用先前定义的输出文件来比较该文件,并相应地检查代码是对还是错。现在的问题是例如。如果输出如下:

Hello World

Hello World

Hello World

该字符串在Hello WorldHello WorldHwllo World中写入文件。我正在使用PHP将文本写入文件。

有没有办法将字符串写入文件? 任何其他替代方法也将受到赞赏。

谢谢。

修改

例如,如果代码:

#include<iotsream>
using namespace std;
int main()
{
 int i;
 for(i=0;i<3;i++)
 {
 cout<<"Hello World\n";
 }
 return 0;
}

此代码的输出应为

Hello World

Hello World

Hello World

当我显示存储在PHP数组$ data ['output']中的字符串时,它正常显示,但是当我将此字符串写入文件时,它将被存储在一行中。我希望将字符串存储在不同的行中。

由于这个原因,这个文件如果不等于包含不同行中字符串Hello World的模板输出文件。

EDIT 这是该文件的PHP代码,它将获取源代码和输入并将其发送到编译器并相应地接收它的输出。

<?php
error_reporting(0);
include_once '../connection-script.php';
session_start();


$user = 'xxxxx';
$pass = 'xxxxx';
$code = '';
$input = '';
$run = true;
$private = false;

$subStatus = array(
    0 => 'Success',
    1 => 'Compiled',
    3 => 'Running',
    11 => 'Compilation Error',
    12 => 'Runtime Error',
    13 => 'Timelimit exceeded',
    15 => 'Success',
    17 => 'memory limit exceeded',
    19 => 'illegal system call',
    20 => 'internal error'
);

$error = array(
    'status' => 'error',
    'output' => 'Something went wrong :('
);

//echo json_encode( array( 'hi', 1 ) ); exit;
//print_r( $_POST ); exit;

if ( isset( $_POST['process'] ) && $_POST['process'] == 1 ) {
    $lang = isset( $_POST['lang'] ) ? intval( $_POST['lang'] ) : 1;
    $input = trim( $_POST['input'] );
    $code = trim( $_POST['source'] );
    $answerfile=$_POST['answerfile'];
    $outputfile=$_POST['outputfile'];

    $client = new SoapClient( "http://ideone.com/api/1/service.wsdl" );

    //create new submission
    $result = $client->createSubmission( $user, $pass, $code, $lang, $input, $run, $private );

    //if submission is OK, get the status
    if ( $result['error'] == 'OK' ) {
        $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
        if ( $status['error'] == 'OK' ) {

            //check if the status is 0, otherwise getSubmissionStatus again
            while ( $status['status'] != 0 ) {
                sleep( 3 ); //sleep 3 seconds
                $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
            }

            //finally get the submission results
            $details = $client->getSubmissionDetails( $user, $pass, $result['link'], true, true, true, true, true );
            if ( $details['error'] == 'OK' ) {
                //print_r( $details );
                if ( $details['status'] < 0 ) {
                    $status = 'waiting for compilation';
                } else {
                    $status = $subStatus[$details['status']];
                }

                $data = array(
                    'status' => 'success',
                    'meta' => "Status: $status | Memory: {$details['memory']} | Returned value: {$details['status']} | Time: {$details['time']}s",
                    'output' => htmlspecialchars( $details['output'] ),
                    'raw' => $details
                );


                if( $details['cmpinfo'] ) {
                    $data['cmpinfo'] = $details['cmpinfo'];
                }

                $myfile=fopen("output.txt","w");
                fwrite($myfile, $data['output']); 
                fclose($myfile);

                $qid=$_POST['questionid'];
                $did=$_POST['domainid'];

                if(sha1_file("output.txt") == sha1_file($outputfile))
                {

                $file=fopen($answerfile,"w");
                fwrite($file,$code);
                fclose($file);

                $mail=$_SESSION['email'];
                $query="SELECT * from student where semail='$mail'";
                $result1=mysql_query($query);
                $row1=mysql_fetch_array($result1);
                $sid=$row1['studentid'];


                $sql="SELECT * from practiceques where did='$did' and quesid='$qid'";
                $result=mysql_query($sql);
                $row=mysql_fetch_array($result);
                $marks=$row['marks'];

                $curdate=date("Y-m-d H:i:s");

                $answer=mysql_query("INSERT INTO points VALUES ('$sid','$qid','$curdate',1,'$marks','$did')");

                echo json_encode( $data );

                }
                else 
                {
                    $mail=$_SESSION['email'];
                    $query="SELECT * from student where semail='$mail'";
                    $result1=mysql_query($query);
                    $row1=mysql_fetch_array($result1);
                    $sid=$row1['studentid'];



                    $sql="SELECT * from practiceques where did='$did' and quesid='$qid'";
                    $result=mysql_query($sql);
                    $row=mysql_fetch_array($result);
                    $marks=$row['marks'];

                    $curdate=date("Y-m-d H:i:s");

                    $answer=mysql_query("INSERT INTO points VALUES ('$sid','$qid','$curdate',0,0,'$did')");
                    echo json_encode($data);
                }
            } 
            else {
                //we got some error :(
                //print_r( $details );
                echo json_encode( $error );
            }
        } 
        else {
            //we got some error :(
            //print_r( $status );
            echo json_encode( $error );
        }
    } else {
        //we got some error :(
        //print_r( $result );
        echo json_encode( $error );
    }
}
?>

1 个答案:

答案 0 :(得分:0)

你不能在php文档中用C语言编写。 无论如何,这是写入文件的PHP代码。

<?php
$fp = fopen('file.name', 'a+');
fwrite($fp, 'Hello World'.PHP_EOL);
fclose($fp);
?>