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);
}

Hi Benjamin,
I have modified your code bit to access struct member from main function.
To achieve this i passed struct as member to function and declared one global variable to track line count which will be usefull to print the information based on the line count

#include <stdio.h>
#include <stdlib.h>

#define BUFFER 100000
#define LINE_BUFFER 200

int glineCount = 0;
typedef struct 
{
    float x, y, u, v;
} PIVPOINT;

// Function Declaration 
void open_vec_file(const char*, PIVPOINT* );

int main()
{
    char fn[] = "PP_B00001.txt";
    int i = 0;

    PIVPOINT *pivp = (PIVPOINT *)malloc(sizeof(BUFFER)); //Allocating memory before using it

    open_vec_file(fn, pivp);  //Passing struct as extra parameter


    while( i++ <glineCount )
    {
        //printing the structinformation
        printf("point no %d: %f %f  %f  %f\n",i, pivp[i].x, pivp[i].y, pivp[i].u, pivp[i].v);
    }

    return 0;
}

void open_vec_file(const char *filename, PIVPOINT *pivp)
{

    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);
            ++glineCount; //To fiind the line count
        }
    }
    fclose(fp);
}

Here is the final modified one.

#include <stdio.h>
#include <stdlib.h>

#define BUFFER 100000
#define LINE_BUFFER 200

int glineCount = 0;
typedef struct 
{
    float x, y, u, v;
} PIVPOINT;

// Function Declaration 
void open_vec_file(const char*, PIVPOINT* );

int main()
{
    char fn[] = "PP_B00001.txt";
    int i = 0;

    PIVPOINT *pivp = (PIVPOINT *)malloc(sizeof(BUFFER));

    open_vec_file(fn, pivp);  


    while( i <glineCount )
    {
        printf("point no %d: %f %f  %f  %f\n",i, pivp[i].x, pivp[i].y, pivp[i].u, pivp[i].v);
        i++;
    }

    return 0;
}

void open_vec_file(const char *filename, PIVPOINT *pivp)
{

    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);
            ++glineCount;
            ++i;
        }
    }
    fclose(fp);
}

Hi, thank you very much for your help! I was having problems with the code you gave so I changed from using dynamically allocated memory to greating an array of structures of size BUFFER and creating a pointer to that. Your code worked on the first couple of hundred points but it then crashed so I'm assuming it is a problem with the memory allocation.
For completeness here's my code now:

#include <stdio.h>
#include <stdlib.h>
#define BUFFER 100000
#define LINE_BUFFER 500

int glineCount = 0;
typedef struct 
{
    float x, y, u, v;
} PIVPOINT;

// Function Declaration 
void open_vec_file(const char*, PIVPOINT* );
int main()
{
    char fn[] = "PP_B00001.txt";
    int i = 0;
    PIVPOINT myPIV[BUFFER];

    //PIVPOINT *pivp = (PIVPOINT *)malloc(sizeof(BUFFER)); //Allocating memory before using it
    PIVPOINT *pivp;
    pivp = &myPIV[0];

    open_vec_file(fn, pivp);  //Passing struct as extra parameter

    while( i++ <glineCount )
    {
        //printing the structinformation
        printf("point no %d: %f %f  %f  %f\n",i, pivp[i].x, pivp[i].y, pivp[i].u, pivp[i].v);
    }
    return 0;
}
void open_vec_file(const char *filename, PIVPOINT *pivp)
{
    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);
            ++glineCount; //To fiind the line count
            ++i;
        }
    }
    fclose(fp);
}

Many thanks again for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.