#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define PI 3.14159265359
int i,j;
double const X0=5,Y0=5,FI=0.7854;//FI in rads
double fi=FI*180/PI;//fi in degress
struct exp_point{
double x,y;
double x1,y1;
int line;
};
int main(){
double d,x,y,x1,y1;
i=0,j=0;
FILE *fp;
if ((fp=fopen("points2D_uni.txt","r"))==NULL)
{ printf("error reading file....\n");
exit(1);
}
else{
while(!feof(fp)){
fscanf(fp,"%lf %lf",&x,&y);
printf("x=%lf y=%lf\n",x,y);
x1=(x-X0)*cos(fi)+(y-Y0)*sin(fi);
y1=(y-Y0)*cos(fi)+(x-X0)*sin(fi);
d=pow(pow(x1-X0,2)+pow(y1-Y0,2),0.5);
if(d>10.0)
i++;
else j++;
}
fclose(fp);
printf("i=%d j=%d\n",i,j);
struct exp_point *lim;
struct exp_point *arr;
lim=(struct exp_point *)calloc(i,sizeof(exp_point));
arr=(struct exp_point *)calloc(j,sizeof(exp_point));
printf("%d",i+j);
}
return 0;
}
I need to get some data from a txt file containing points in a coordinates system but when i print them all are 0 and the file scan never ends. I have tried both unicode file and with tabs. In the file with tabs it gets the first number without the demical points and after that all the other data are displayed as 0.0000
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265359
// Definition of the struct exp_point
struct exp_point {
double x, y;
double x1, y1;
int line;
};
int main() {
double X0 = 5, Y0 = 5, FI = 0.7854; // FI in radians
double fi = FI; // Using FI directly since it is already in radians
int i = 0, j = 0;
double d, x, y, x1, y1;
FILE *fp;
// Provide the absolute path to the file
const char *file_path = "/ABSOLUTE/PATH/file.txt"; // Replace with your actual file path
// Attempt to open the file
if ((fp = fopen(file_path, "r")) == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
// Reading the file until the end
while (fscanf(fp, "%lf %lf", &x, &y) == 2) {
printf("Read values: x = %lf, y = %lf\n", x, y);
// Applying transformation
x1 = (x - X0) * cos(fi) + (y - Y0) * sin(fi);
y1 = (y - Y0) * cos(fi) - (x - X0) * sin(fi);
// Calculating distance using hypot
d = hypot(x1 - X0, y1 - Y0);
if (d > 10.0)
i++;
else
j++;
}
// Close the file
fclose(fp);
printf("i = %d, j = %d\n", i, j);
// Allocate memory for exp_point structures
struct exp_point *lim = (struct exp_point *)calloc(i, sizeof(struct exp_point));
struct exp_point *arr = (struct exp_point *)calloc(j, sizeof(struct exp_point));
if (lim == NULL || arr == NULL) {
perror("Error allocating memory");
exit(EXIT_FAILURE);
}
printf("Total points processed: %d\n", i + j);
// Free allocated memory
free(lim);
free(arr);
return 0;
}
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
int main() {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
// The rest of your code...
}