execute program with php opening a terminal

0

What happens is that I am currently doing a PHP program where I run C programs I give inputs from a text file

and I save your outputs to analyze them, what happens is that I want the output to come out with all the format, including line breaks for example when executing this C program:

#include <stdio.h>
int main(){
int num1,num2,res;
printf("dame un numero ");
scanf("%d",&num1);
printf("dame otro numero ");
scanf("%d",&num2);
res=num1+num2;
printf(" el resultado de la suma es:%d",res);
return 0;
}

in the text file I have the entries that would be:

10
20

including the line break, and I run it with php with the shell exec command:

shell exec(programa<entrada.txt)

but this does not work for me because it gives me the following output:

dame un numero dame otro numero el resultado de la suma es:30

and what I need is for it to come out like this:

dame un numero
dame otro numero
el resultado de la suma es:30

I hope you can help me thank you very much ... I leave my php code that is very simple

note: as you can see before I compile everything

<!doctype html>
<html lang="es">
	<head>
		<title></title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1">
		<link rel="stylesheet" type="text/css" href="bootstrap/3.3.7/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="css/misestilos.css">
		<script src="jquery/3.2.1/jquery.min.js"></script>
		<script src="bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
	<body>
		<form name="frmEntrada" method="post" action="ejecutar.php">
			Codigo:<input type="text" name="txtEntrada">
			<input type="submit" class="btn btn-primary" name="btnEntrar" value="Enviar">	
		</form>


		<?php 
			if (isset($_POST["btnEntrar"])) {
				$texto=$_POST["txtEntrada"];
				$salida="gcc $texto.c -o $texto";
				$respuesta=array();
				$estado;
				$res=exec($salida,$respuesta,$estado);
				
				if ($estado==1) {
					echo "El programa no fue encontrado o error de compilacion";
				}
				else{
					//tomar contenido de salida
					$manejador2=fopen("S".$texto.".txt", 'r');
					//bloquear archivo
					flock($manejador2, 2);
					//pasar contenido de archivo a arreglo
					$contenido2=fread($manejador2, filesize("S".$texto.".txt"));
					fclose($manejador2);


					$salida=shell_exec($texto."< E".$texto.".txt");
					
					var_dump($salida);
					echo "<div class='row'>";
						echo "<div id='cajas' class='col-sm-6'>";
						echo "<H1>Salida del Programa</h1><br>";
						echo "<p>$salida</p>";
						echo "</div>";
						echo "<div id='cajas' class='col-sm-6'>";
						echo "<H1>Salida esperada</h1><br>";
						echo "<p>$contenido2</p>";
						echo "</div>";
					echo "</div>";	
					echo "<input type='button' class='btn btn-danger' value='No pasa'' onclick='' />";
					echo "<input type='button' class='btn btn-success' value='Pasa'' onclick='' />";
				}

			}

		 ?>
	</body>
</html>
    
asked by Yept 30.11.2017 в 09:42
source

1 answer

2

It's a syntax problem in C. The problem is that you have not added any line breaks. You must add the "\n" command so that a line break appears, such as:

printf("dame un numero \n");
    
answered by 30.11.2017 / 09:53
source