[英]Use input redirection from a text file using BASH to use as data in a C program
我需要收集写在文本文件中的数据并使用 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: \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);
}
任何对此的见解或来源都将不胜感激。 谢谢
假设 C 代码被编译为可执行文件a.out
,请您尝试 bash 代码:
#!/bin/bash
./a.out $(wc -l < input.txt) < input.txt > output,txt
$(wc -l < input.txt)
计算输入文件的行数并作为第一个参数传递给a.out
。input.txt
被重定向到a.out
的标准输入, output 被重定向到output.txt
,这是作为一个新文件创建的。C 代码无需修改即可使用,但可以进行改进,例如删除提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.