How do read in the file and store it?

So I am trying to read in a file and store it into my data struct, but every time I run it it either reads in garbage data and my struct is filled with 0s. Any suggestions?

I have functions to check if the data is valid, because my struct cannot store data that it has already stored (e.g same port or vmn).

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


typedef struct DataType{
    double timeOffset;
    int vmn;
    int port;
}Data;

void insertDataType(Data *Data, double timeOffset, int vmn, int port){
    Data->timeOffset = timeOffset;
    Data->vmn = vmn;
    Data->port = port;
}

double returnTimeOffset(Data D){
    assert(D.timeOffset != 0.0);
    return D.timeOffset;
}

int returnVMN(Data D){
    assert(D.vmn != 0);
    return D.vmn;
}

int returnPort(Data D){
    assert(D.port != 0);
    return D.port;
}

bool vmnValid(Data *Data, int n, int vmn){
    int i;
    for(i = 0; i <= n; i++){
        if(Data[i].vmn != 0){
            if(Data[i].vmn == vmn){
                printf("Invalid vmn %d: vmn already inserted \n", vmn);
                return false;
            }
        }
    }
    return true;
}

bool timeValid(Data *Data, int n, double timeOffset){
    int i;
    for(i = 0; i <= n; i++){
        if(Data[i].timeOffset != 0.0){
            if(Data[i].timeOffset == timeOffset){
                printf("Invalid timeOffset %2lf: timeOffset already used \n", timeOffset);
                return false;
            }
        }
    }
    return true;
}

bool portValid(Data *Data, int n, int port){
    int i;
    for(i = 0; i <= n; i++){
        if(Data[i].port != 0){
            if(Data[i].port == port){
                printf("Invalid port %d: port already in use\n", port);
                return false;
            }
        }
    }
    return true;
}


int main(int argc, const char * argv[]){
    int n = 0;
    int i = 0;
    char c;
    FILE *file;

    // Open file
    file = fopen("connect1.in", "r");
    if (file == NULL) {
        fprintf(stderr, "Invalid input file \n");
        exit(1);
    }

    // Get number of lines (n)
    while((c = fgetc(file))!= EOF){
        if(c == '\n'){
            n++;
        }
    }

    printf("n = %d \n", n);

    // Create a strut DataType of size n
    Data *storage;
    storage = calloc(n, sizeof(struct DataType));

    // Read and insert the data
    double timeOffset;
    int vmn;
    int port;

    printf("\n");
    while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port != EOF)){
        printf("%lf %d %d \n", timeOffset, vmn, port);
        if(timeValid(storage, n, timeOffset)){
            if(vmnValid(storage, n, vmn)){
                if(portValid(storage, n, port)){
                    insertDataType(&storage[vmn], timeOffset, vmn, port);
                }
            }
        }
    }
    printf("\n");

    printf("\n");
    printf("Storage:\n");
    for(i = 0; i <= n; i++){
        printf("%3d:   %2lf  %d  %d \n", i, storage[i].timeOffset, storage[i].vmn, storage[i].port);
    }
}

Solutions

Change

// while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port != EOF)){
while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port) != EOF){
// or better 
while (fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port) == 3) {

@suspectus is correct, add rewind(file);

// change
// char c;
int c;

Minor considerations:

Data *storage;
// storage = calloc(n, sizeof(struct DataType));
// I like the style
storage = calloc(n, sizeof(*storage));

// In a number of places, function do not change *Data, so use `const`
// Useful to now, at a glance, that *Data is unchanged
//   and forces the compiler to warn otherwise.
// bool vmnValid(Data *Data, int n, int vmn){
bool vmnValid(const Data *Data, int n, int vmn) {

After counting the number of lines the file pointer must be reset to the start of the file again.

Use the rewind() call which resets the file position back to file start after the line counting loop:

rewind(file);
Tags: C / File / Scanf

Similar questions

Program to read data from keyboard, write it to a file and again read the same data from the file and copy that to another file and display?
This is what I've so far. I want the program to read data from the keyboard write it to a file called INPUT, again read the same data from the INPUT file, and copy to that another file and also display it on the screen. I don't know where did I go wrong. Please help me. Thanks in advance.
How to read data from file and clean and store to a file
Good day all. I have a file that contains data which some part is shown below. It contains the records at every 3 seconds taking for a period of 30 days. I want to perform the following on the data. 2.For each hour, I want calculate the sum of the data in the second column only and the number of records that gave rise to this sum. 3.And then print ...
C
Want a C program to write into a file, read it and store even and odd numbers in a separate file
I tried to make the following code. The problem I face is that the contents of the files are in hex or binary. I want it to be readable with text editor not a hex editor. The other problem I face is the scanf() doesn't accept 3 digit numbers. The output is given below. Enter no.of digits: 5 Enter the numbers: 123 34 456 67 789 The content of number...
C
To find factorial of 500 and store it in a variable...and perform calculations...How to store such a huge number?
how do i store a huge number in a variable (i) and wont need to change much of the program ? Is there a available datatype to store factorial of 100 for example ?
Atomic load and store functions produce same assembly code as non-atomic load and store
Why is the assembly output of store_idx_x86() the same as store_idx() and load_idx_x86() the same as load_idx()? It was my understanding that __atomic_load_n() would flush the core's invalidation queue, and __atomic_store_n() would flush the core's store buffer. Note -- I complied with: gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16) Update: I understa...
i am trying to read a file and store the contents of file into three different array
I am trying to read a file and store the contents of file into three different array using below code my input file is: and my output is: I don't know why in first line of output along with p1, p2p3p4 is coming and same for 2 and 3 line.
C

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.