[英]How to read values from a .txt file into a program in C
我無法將txt文件中的值讀取到程序中。 我給定的值是坐標(x,y),后跟2個空格。 假定最大點數為100,那么如何讀取輸入,以便我的程序每行僅讀取給定數量的值? 到目前為止,我的程序旨在計算兩點之間的距離,並使用總和找到周長。
3 12867 1.0 2.0 1.0 5.0 4.0 5.0
5 15643 1.0 2.0 4.0 5.0 7.8 3.5 5.0 0.4 1.0 0.4
到目前為止,我能想到的是:
scanf("%f %f %f %f %f %f", &x1, &y1, &x2, &y2, &x3, &y3);
到目前為止,這也是我的程序。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_PTS 100
#define MAX_POLYS 100
#define END_INPUT 0
struct Point {
double x, y;
};
double getDistance(struct Point a, struct Point b) {
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main(int argc, char *argv[]) {
int npoints, poly_id;
struct Point a, b;
if(scanf("%d %d", &npoints, &poly_id)) {
scanf("%lf %lf", &a.x, &a.y);
scanf("%lf %lf", &b.x, &b.y);
} else { printf("\nUnable to read input.\n");
exit(EXIT_FAILURE);
}
printf("\nStage 1\n=======\n");
printf("First polygon is %d\n", poly_id);
printf(" x_val y_val\n %1.1f %1.1f\n %1.1f %1.1f\n",
a.x, a.y, b.x, b.y);
printf("perimeter = %2.2lf m\n", getDistance(a, b));
return 0;
}
輸出為:
First polygon is 12867
x_val y_val
1.0 2.0
1.0 5.0
perimeter = 3.00 m
編輯:我必須補充,必須使用重定向讀取txt文件,例如./program <txt文件
scanf
和family返回一個值,該值告訴您成功掃描了多少個值:
返回值
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
因此,只要知道最大點數,您仍然可以使用scanf
。 這是一個示例程序 :
#include <stdio.h>
void read(char *line) {
float x1 = 0, x2 = 0;
int n = sscanf(line, "%f %f", &x1, &x2);
printf("Read %d, %f %f\n", n, x1, x2);
}
int main(void) {
char *line1 = "1.0";
char *line2 = "1.0 2.0";
read(line1);
read(line2);
return 0;
}
輸出:
Read 1, 1.000000 0.000000
Read 2, 1.000000 2.000000
但是,從事物的聲音來看100是很多數字,因此您可以嘗試使用strtok
標記化並循環讀取值。
關於你的方法
要從文件讀取數據,您需要fscanf()
函數。 在此處閱讀手冊頁 。
注意:如果要使用fscanf()
,強烈建議您檢查fscanf()
的返回值以確保正確輸入。
關於值的存儲,可以使用以下任一方法
malloc()/calloc()/realloc()
。 (更靈活) 替代方法:
更好的方法是
從我在您的點示例中看到的結果來看,點之間有特定的分隔,即制表符和換行符。 我的建議如下:
一旦檢測到數字字符,請激活一個新狀態(一種方法),在該狀態下開始檢測點,直到檢測到制表符(“ \\ t”)或換行(“ \\ n”)。 由於您使用固定格式,因此可以使用
scanf("%f %f", &x1, &x2);
當您檢測到數字字符(我建議您使用點的數組(因為有固定的點數))(點是一個以x,y為成員的結構)時,您必須向后返回一個字符才能因為您的點的第一個數字字符用於觸發檢測狀態,所以可以檢測點的完整坐標。
一旦檢測到選項卡或換行,就返回循環並繼續“吃掉”選項卡和換行,直到下一個數字字符為止。
在您的示例中,解析如下所示:
希望這可以幫助。 :)
請嘗試以下代碼段:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
int count=0;
int i,j;
int cordinate[5][5];
fp = fopen('a.txt',"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fp) ) != EOF ){
printf("%c",ch);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++){
if(ch==' '){
count++;
cordinate[0][0]=
}
else{
cordinate[i][j]=ch;
}
}
}
}
printf('print content of file');
for(i=0;i<5;i++)
{
for(j=0;j<5;j++){
printf("cordinates:%d"+cordinate[i][j]);
}
}
}
fclose(fp);
return 0;
}
在編程中,當他們想一次又一次地做一些work
時,就會使用循環。
認為要計算多邊形的周長,您需要計算其邊緣的長度。 如果給定兩個相鄰點,則函數getDistance
可以找到邊的長度。 因此,您一直在尋找邊的長度並添加它們。 現在想想什么是重復性work
。 它是帶有新點的getDistance
。 因此,請在下面的代碼中查看for
循環。
我已經在代碼中寫了一些注釋來說明這一點。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_PTS 100
#define MAX_POLYS 100
#define END_INPUT 0
struct Point {
double x, y;
};
double getDistance(struct Point a, struct Point b) {
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main(int argc, char *argv[]) {
int npoints, poly_id;
struct Point a, b;
if(scanf("%d %d", &npoints, &poly_id)) {
int iteration = 0;
scanf("%lf %lf", &a.x, &a.y);
struct Point initialPoint = a;
double parimeter = 0; // i start with 0 value of parimeter.
for (iteration = 1; iteration < npoints; ++iteration) {
scanf("%lf %lf", &b.x, &b.y); // take input for new-point.
parimeter += getDistance(a, b); // add the parimeter.
// for next iteration, new-point would be first-point in getDistance
a = b;
}
// now complete the polygon with last-edge joining the last-point
// with initial-point.
parimeter += getDistance(a, initialPoint);
// print the information.
printf("Polygon is %d\n", poly_id);
printf("perimeter = %2.2lf m\n", parimeter);
} else { printf("\nUnable to read input.\n");
exit(EXIT_FAILURE);
}
return 0;
}
請參見實際使用的代碼: 工作代碼 。
注意我給了
輸入:
3 12867 1.0 2.0 1.0 5.0 4.0 5.0
輸出為:
Polygon is 12867
perimeter = 10.24 m
現在,如果您想對更多的多邊形進行這項work
,希望您知道該怎么做!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.