Hi,
I am trying to write code to read a text file containing velocity vector data in the form x, y, u, v. An example of the data file I am opening is:
0 1.0 3.24 45.64
1.5 2.5 54.34 23.45
...
I have a function which can read the data file into an array of structures and display the values but I don't know how to access the array of structures in int main(). I am fairly new to C programming and giving it a go as Matlab is too slow for the files I'm trying to process (about 50,000 points per file).
Well, here's the code I have:
#include <stdio.h>
#include <stdlib.h>
#define BUFFER 100000
#define LINE_BUFFER 200
void open_vec_file(const char *filename);
typedef struct
{
float x, y, u, v;
} PIVPOINT;
int main()
{
char fn[] = "PP_B00001.txt";
open_vec_file(fn);
return 0;
}
void open_vec_file(const char *filename)
{
PIVPOINT pivp[BUFFER];
int i = 0;
FILE *fp = fopen(filename, "r");
char fileline[LINE_BUFFER];
// open the file, first checking if it exists
if (fp == NULL){
printf("ERROR: Could not open vector file %s\n", filename);
}
else
{
while ( fgets( fileline, LINE_BUFFER, fp) != NULL)
{
sscanf( fileline, "%f%f%f%f", &pivp[i].x, &pivp[i].y, &pivp[i].u, &pivp[i].v);
printf("point no %d: %f %f %f %f\n",i, pivp[i].x, pivp[i].y, pivp[i].u, pivp[i].v);
++i;
}
}
fclose(fp);
}