I need to gather data written in a text file and use a bash shell which will allow each line of the text file to be used in my c program as separate data points. For a little more context, my program is taking in a set of coordinates(x,y) for as many lines there are in my input.txt file. After that, it will find the nearest and furthest points in reference to the one it is on.
For example input.txt has the following lines:
input1 3.2 9.3
input2 5.7 13.6
input3 18.4 12.2
I have not found out how to do this on bash. I have written the following program to do something very similar but not dynamically using bash redirection.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct coordinates{
//The label of the coordinates
char coordinateName[32];
//The x and y coordinates
float xCoord;
float yCoord;
} coordinate;
//Function to calculate distance from another point
float distance(char p1[32], char p2[32], float x1, float x2, float y1, float y2){
float c;
c = sqrt( pow((x1-x2),2) pow((y1-y2),2) );
printf("\nDistance between %s and %s is: \t\t\t\t%.2f", p1, p2, c);
return c;
}
int main () {
// Get the number of inputs being taken in from the user via command line
char ENTRIESstring[1];
int ENTRIES;
scanf("%s", ENTRIESstring);
ENTRIES = atoi(ENTRIESstring);
// Declare a struct object
struct coordinates myCoordinates[ENTRIES];
for(int i = 0; i<ENTRIES; i ){
// Enter the coordinate name
//printf("Enter a Coordinate name: ");
scanf("%s", &*myCoordinates[i].coordinateName);
// Ask for x coordinate
//printf("Enter a Coordinate value for x: ");
scanf("%f", &myCoordinates[i].xCoord);
// Ask for y coordinate
//printf("Enter a Coordinate value for y: ");
scanf("%f", &myCoordinates[i].yCoord);
}
//define closest and furthest points
float closestPoints = INFINITY, furthestPoints = 0.0;
int closestPoint1, closestPoint2;
int furthestPoint1, furthestPoint2;
//define calculation variable to check against closest and furthest point
float calculation;
for(int i = 0; i <= ENTRIES-1; i ){
for (int j = 0; j <= ENTRIES-1; j ) {
char *p1,*p2;
float x1,x2,y1,y2;
p1 = myCoordinates[i].coordinateName;
x1 = myCoordinates[i].xCoord;
y1 = myCoordinates[i].yCoord;
p2 = myCoordinates[j].coordinateName;
x2 = myCoordinates[j].xCoord;
y2 = myCoordinates[j].yCoord;
//if coord1 is equal to coord2 skip
if(i==j){
continue;
}
else{
printf("\n%s - (x:%.2f,y:%.2f) and %s - (x:%.2f,y:%.2f)", p1,x1,y1,p2,x2,y2);
calculation = distance(p1, p2, x1, x2, y1, y2);
if (calculation < closestPoints){
closestPoint1 = i;
closestPoint2 = j;
closestPoints = calculation;
}
else if (calculation > furthestPoints){
furthestPoint1 = i;
furthestPoint2 = j;
furthestPoints = calculation;
}
printf("\n-----------------------------------------------------------------------");
}
}
}
printf("\nClosest points: point %s and point %s - distance: \t%.2f", myCoordinates[closestPoint1].coordinateName, myCoordinates[closestPoint2].coordinateName, closestPoints);
printf("\nFurthest points: point %s and point %s - distance: \t%.2f\n", myCoordinates[furthestPoint1].coordinateName, myCoordinates[furthestPoint2].coordinateName, furthestPoints);
return(0);
}
Any insight or sources to read on this would be greatly appreciated. Thanks
CodePudding user response:
Assuming the C code is compiled to an executable a.out
, would you
please try the bash code:
#!/bin/bash
./a.out $(wc -l < input.txt) < input.txt > output,txt
$(wc -l < input.txt)
counts the lines of the input file and is passed toa.out
as the 1st argument.- The
input.txt
is redirecred to the standard input ofa.out
and the output is redirected tooutput.txt
, which is created as a new file.
The C code will work without modifications but may be refined e.g. removing the prompts.