使用BASH从文本文件中使用输入重定向作为C程序中的数据



我需要收集写在文本文件中的数据,并使用bash shell,它将允许文本文件的每一行在我的c程序中用作单独的数据点。为了获得更多的上下文,我的程序将为input.txt文件中的行数获取一组坐标(x,y(。在那之后,它将根据它所在的点找到最近和最远的点。

例如input.txt有以下几行:

input1 3.2 9.3
input2 5.7 13.6
input3 18.4 12.2

我还没有在bash上找到如何做到这一点。我编写了以下程序来做一些非常类似的事情,但不是动态使用bash重定向。

#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: tttt%.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%.2fn", myCoordinates[furthestPoint1].coordinateName, myCoordinates[furthestPoint2].coordinateName, furthestPoints);
return(0);
}

如果您对此有任何见解或消息来源,我们将不胜感激。感谢

假设C代码被编译为可执行的a.out请尝试bash代码:

#!/bin/bash
./a.out $(wc -l < input.txt) < input.txt > output,txt
  • $(wc -l < input.txt)计算输入文件的行数作为第一个参数传递给CCD_ 3
  • input.txt被重定向到a.out的标准输入并且输出被重定向到作为新文件创建的output.txt

C代码将在不进行修改的情况下工作,但可能会进行细化例如去除提示。

最新更新