0

So I'm trying to teach myself , and Ive been doing online lab exercises to learn it. I wrote a program that goes into pretty good detail of arrays and structures. It uses random numbers to monitor for a spike of 100 psi, and then prints that as a 0 point, and prints the previous 10 seconds of the array and the next 10, as if I was collecting data. The next part of this exercise is to take the program's print statement, and have it write to an external file and have it print there. My thought process is to populate an array in the printout function that holds the values read from the file and then print from the array to screen. But I'm not sure how this would look, or really how to accomplish it. If anyone could point me in the right direction or give a good explanation it would be greatly appreciated!

My code so far:

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

#define MAX_CHANGE      100
#define ARRAY_SIZE      21

typedef struct data_slice
{
        int t;          // -> Time
        float tp;       // -> Valve pressure
    float tf;       // -> Sodium flow
    float tt;       // -> Sodium temp in Celsius
} data_slice;

// Function Declarations
void get_values(float * pressure, float * flow, float * temp);
void printIt(data_slice * data);
void initializeArray(data_slice * data);
bool spikeValueRecorded(data_slice * data, int outputIndex);


int main()
{
    srand((unsigned int)time(NULL));
    data_slice data[ARRAY_SIZE];
    int index = -1;

    while (1)
    {
        // Initialize the entire array
        initializeArray(data);

                    // If there's a spike.....
        if (spikeValueRecorded(data, index))
        {
            // Set the previous "time" in array to negatives
            int temp = index;
            for (int i = 0; i >= -10; --i)
            {
                data[temp].t = i;
                temp = temp - 1;
                if (temp < 0)
                    temp = temp + ARRAY_SIZE;
            }

                // Record for 10 more seconds
            for (int i = 0; i <= 10; ++i)
            {
                data[index].t = i;
                index = (index + 1) % ARRAY_SIZE; //         Increment the index of the circular array
                get_values(&data[index].tp, &data[index].tf,    &data[index].tt);       // "Record" the values
            }
            break;
        }
    }
    // Print the finished recording
    printIt(data);
}

// Return: void
// in - Values of the data_slice struct
//
// Description: The three values of the struct (data_slice) to be filled in
void get_values(float * pressure, float * flow, float * temp)
{
    *pressure   = (float)(rand() % (700 - 500 + 1) + 500);  //    Range: 500 - 700
    *flow       = (float)(rand() % (20 - 10 + 1) + 10);     //   Range: 10 - 20
    *temp       = (float)(rand() % (200 - 100 + 1) + 100);  //  Range: 100 - 200
}

// Return: void
// in - The array of data_slice
//
// Description: Prints the entire array being passed in
void printIt(data_slice * data)
{
    // Find the indice holding the time value of -10
     int indice = 0;
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        if (data[i].t == -10)
        {
            indice = i;
            break;
        }
    }

    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        printf("%i\t %f\t %f\t %f\n", data[indice].t, data[indice].tp,    data[indice].tf, data[indice].tt);
        indice = (indice + 1) % ARRAY_SIZE;
    }
}

// Return: void
// in - The array of data_slice
//
// Description: Initializes the entire array to random values and their   times to 0
void initializeArray(data_slice * data)
{
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        data[i].t = 0;
        get_values(&data[i].tp, &data[i].tf, &data[i].tt);
    }
}

// Return: boolean
// in - The array of data_slice
// out - Indice of the pressure spike
//
// Description: Returns true if a positive spike in pressure has been   recorded.
//      outputIndex will hold the 0-indice of the pressure spike, else -1
bool spikeValueRecorded(data_slice * data, int outputIndex)
{
    float oldValue = data[0].tp;
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        if (data[i].tp - oldValue < MAX_CHANGE)
        {
            outputIndex = i;
            return true;
        }
    }
    outputIndex = -1;
    return false;
}
  • 4
    [Get a couple of good beginners books to read](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), they should contain all information you need (and much more). – Some programmer dude Nov 16 '17 at 07:20
  • Correct me if understood wrong. you wanted to write array to file and then read from file and display on screen ? – ntshetty Nov 16 '17 at 07:27
  • More so how do I take my print function, put it in another file in the project, and then have it print from there? Here's the sample problem I'm following: The printout function should now print out to output-data.txt . Take out all code that prints to screen Write a second program (i.e., NOT a function in the first program!) that reads the data from output-data.txt and prints it out to screen. – Blake Rogers Nov 16 '17 at 14:07
  • to change a variable `index` in a calling function `main()`, the parameter passed to a sub function `spikeValueRecorded()` must be the address of the variable, not the contents of the variable. So the posted code has a major problem the needs corrected – user3629249 Nov 18 '17 at 17:45

2 Answers2

0

You can use the fprintf call exactly like the printf call, with one difference. The first argument will be a pointer to a file handle (FILE*) which you create using a call to fopen("full or relative path","w").

The string format is now the second argument and the variable args list starts at arg 3

Patrick Sturm
  • 393
  • 1
  • 13
0

This is way you can write to file by using libc API

void printIt(data_slice * data)
{
    // Find the indice holding the time value of -10
     int indice = 0;
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        if (data[i].t == -10)
        {
            indice = i;
            break;
        }
    }
     //write to file 
    FILE *fp = fopen("output.txt", "wb"); //Binary mode since the size of array is fixed.
    fwrite(data, sizeof(char), sizeof(data), fp);
    fclose(fp);

    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        printf("%i\t %f\t %f\t %f\n", data[indice].t, data[indice].tp,    data[indice].tf, data[indice].tt);
        indice = (indice + 1) % ARRAY_SIZE;
    }

}
ntshetty
  • 1,293
  • 9
  • 20
  • So let try to understand - I would take the FILE * fp and put it where my print statement is and then take my printIt data and put that in the output file? – Blake Rogers Nov 16 '17 at 14:09
  • @BlakeRogers, NO, the call to `fopen()` needs to be performed early in the execution of the code. Then use `fprintf()` (for text file) or `fwrite()` (for binary file) to output the data to the file. Note be sure to call `fclose()` before exiting the program. – user3629249 Nov 18 '17 at 17:48