I am working on a project in my college which is about developing an online autograder website like that of hackerrank for my college. Now, for compiling codes, I am using the API provided by sphere engines. The API is returning the output as a string which then I store it into a file. Then, I use a previously defined output file to compare this file and check accordingly whether the code is right or wrong. Now the problem is for eg. if output is like below:
Hello World
Hello World
Hello World
The string is written in the file as Hello WorldHello WorldHwllo World. I am using PHP to write text to file.
Is there any way to write the string as it is to a file? Any other alternative method will also be appreciated.
Thank You.
EDIT
for eg if the code:
#include<iotsream>
using namespace std;
int main()
{
int i;
for(i=0;i<3;i++)
{
cout<<"Hello World
";
}
return 0;
}
The output of this code should be
Hello World
Hello World
Hello World
And when i am displaying the string which is stored in a PHP array $data['output'], it is getting displayed normally but when i am writing this string to a file, It is getting stored in a single line. I want to store the string as it is i.e. in different lines.
Due to this, this file if not equal to the template output file which contains the string Hello World in different lines.
EDIT This is the PHP code for the file which will take the source code and input and send it to the compiler and receive it's output accordingly.
<?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 );
}
}
?>