0

I have a program that i am trying to write that will open a file named hw3.data, this file will contain a string, a float, an int, and a string on each line. Example: someword 5.4 200000 someword I do not know the size of the file or the size of the strings within the file. I need to dynamically allocate at array of struct to store the info. I am fairly new to C and have looked over other various questions and articles however none of them really helped me grasp how to solve this. I figured the best way to go about solving this was to first statically declare a structure. Read the file for the length and then take the information from the static struct and then dynamically allocate. Here is my code thus far:

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

//Static Array of Struct
struct record{
 char name[100];
 float hp;
 int size;
 char color[30];
};



//getData from file 
int getData(FILE*, struct record[], int currSize){
   fp = fopen("hw3.data","r");
   if (fp !=NULL){
    printf("file Open");
    while(3==fscanf(fp, "[^,],%f,%d,[^,]", records[i].name &records[i].hp,  &records[i].size, records[i].color)){
        currSizef++;
    }
} else {
    printf("failed");
    }
 return 0;
}

//First sorting function to sort by FLOAT value

//Second sorting function to sort by INT value


int main()
{
 int currSizef=0;
 struct record *records;
 FILE* fp = NULL;

 int choice;
//menu 
  do
   {
    printf("\t\tMenu\n");
    printf("Options:\n");
    printf("Input a number to select option:\n");
    printf("1-Sort floats high to low\n 2-Sort floats low to high\n");
    printf("3-Sort intergers high to low\n 4-Sort intergers low to high\n");
    printf("5-Exit\n");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1: /*Print high to low floats*/
        break;
   case 2: /*print low to high floats*/
    break;
   case 3: /*print high to low ints*/
        break;
   case 4: /*print low to high ints*/
        break;

    }

  }while(choice !=5);
 return 0;
}
2
  • 1
    If you want to dynamically allocate an array, try using malloc(your_size*sizeof(your_struct)) Commented Feb 15, 2015 at 21:41
  • What, specifically, is your question? Commented Feb 15, 2015 at 21:47

1 Answer 1

1

You can try loading the data to a temporary record and use realloc to dynamically extend the data size.

NOTE: if NULL is passed to realloc then realloc will behave like the malloc function for the specified size.

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

//Static Array of Struct
typedef struct record{
    char name[100];
    float hp;
    int size;
    char color[30];
}RECORD;

void record_copy(RECORD *dest, RECORD* src) {
    strcpy(dest->name,src->name);
    dest->hp = src->hp;
    dest->size = src->size;
    strcpy(dest->color,src->color);
}

//LoadData from file
RECORD* LoadData(FILE* fp,int *size){
    int i = 0;
    RECORD temp;
    RECORD *data= NULL,*tempptr = NULL;
    while( fscanf(fp, "%[^,],%f,%d,%[^,],", temp.name,&temp.hp,&temp.size,temp.color)==4 ) {
        if( (tempptr = (RECORD*) realloc( data, sizeof(RECORD) * (i+1) )) )
            data = tempptr;
        else
            return (NULL);
        record_copy(&data[i],&temp);
        i++;
    }
    *size = i;
    return (data);
}

void record_sort(RECORD *data, int size) {
    int i,j;
    RECORD temp;
    for (i = 0 ; i < ( size - 1 ); ++i)
        for (j = 0 ; j < (size - i - 1); ++j )
            if( data[j].hp > data[j+1].hp ) {
                record_copy(&temp,&data[i]);
                record_copy(&data[i],&data[i+1]);
                record_copy(&data[i+1],&temp);
            }
}

int main() {
    FILE *fp = fopen("<fileName>","r");
    RECORD *data = NULL;
    int i;
    int size;
    if( fp != NULL ) {
        data = LoadData(fp,&size);
    }
    record_sort(data,size);
    for(i = 0;i<size;i++) {
        printf("%s:%f:%d:%s\n",data[i].name,data[i].hp,data[i].size,data[i].color);
    }
    free(data);
    return 0;
}

with respect to sorting you can use any sorting algorithm to sort your record. I have given an example for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.