I have a c program that accepts as argument a string. The string input as argument is a single line of a file, so for example one of my files contains the following:
0410000340000230
1111111111111111
1800400700032050
So far I've writen a bash script to automate the work:
#!/bin/bash
read -p "make clean"
make clean
read -p "make"
make
file=$1
while read line; do
echo $line
done < $file
So far, so good. Right now, I want to use the output from the echo $line command as argument to my program. I've tried:
echo $line | ./program
echo $line > ./program
echo $line < ./program
./program < echo $line
Sometimes it will just give me an error that there are no arguments for my program and at other times it will arrive at a segmentation fault.
Can someone tell me what I'm doing wrong?
The C code of the main program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "functions.h"
int main(int argc, char * argv[])
{
if (argc != 2)
{
printf("Usage: ./Sudoku <Sudoku en une seule ligne. Represantation des cases vides avec 0>\n");
return 0;
}
FILE *f, *g;
int n, n_clauses, ** Sudoku;
n = (int) sqrt((double) strlen(argv[1]));
if (ceil(sqrt((double) strlen(argv[1]))) == sqrt((double) strlen(argv[1])))
{
Sudoku = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++)
{
Sudoku[i] = (int *)malloc(n * sizeof(int));
}
remplir_sudoku(argv[1], n, Sudoku);
}
else
{
n = (int) sqrt((double) find_sudoku_length(argv[1]));
printf("\n %d\n", n);
Sudoku = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++)
{
Sudoku[i] = (int *)malloc(n * sizeof(int));
}
remplir_sudoku_v2(argv[1], n, Sudoku);
}
afficher_sudoku(Sudoku, n);
n_clauses = nb_clauses(n);
n_clauses = n_clauses + nb_remplis(Sudoku, n);
f = fopen("CNF", "w");
fprintf(f, "c CNF\n");
fprintf(f, "p cnf %d %d\n", n*n*n, n_clauses);
transformer_en_cnf(f, Sudoku, n);
fclose(f);
return 0;
}