C Program For this assignment, you need only write a single-file C program. Your
ID: 3607493 • Letter: C
Question
C Program
For this assignment, you need only write a single-file C program. Your program will: • Define a C structure ( contact struct ) suitable to hold the defining characteristics of a: Sample (char* name, int list_len, float* value_list) - call this format a Sample data record. • Write a separate function to : Read a file (created in a text editor – pico) of delimited Sample data records into an array of pointers to your sample structs. Note: you may use a statically sized array of size 1024, however, each element of the array must be a pointer to a sample struct (as defined above). Initially each element of the array must be set to NULL (to indicated that it is not used), and then later to a dynamically allocated sample struct if it is to be used. Note: after dynamically allocating memory for a sample struct, you have enough memory for each/every component of the structure. However, for each component of that structure that is a pointer, you will eventually need to allocated memory for the “thing” that it points to as well (think string via char* , array via float*). You will also have to remember this when freeing memory via a pointer to a sample struct. • Write a separate function to : Create a new file and write all the “used” elements of the array ( of sample struct s ) to this file. You must use binary writes for all of C's built-in numeric types. • Write a separate function to : Free all of the “used” elements of the array ( of sample struct s ) – that means all the memory that was allocated for each element ( the structure and its components ). • Write a separate function to : Read a file of your sample struct s, ( as it was created by your second function ) and display
CS337 System Programming Programming Assignment #6 Due Wednesday, November 1st, 11:59pm Topics: Files File Streams in C For this assignment, you need only write a single-file C prpgram. Your program will: Define a C structure (contact struct) suitable to hold the defining characteristics of a: Sample (char* name, int list len, float value _list) - call this format a Sample data record. Write a separate function to Read a file (created in a text editor -pico) of delimited Sample data records into an array of pointers to your sample structs. Note: you may use a statically sized array of size 1024 however, each element of the array must be a pointer to a sample struct (as defined above Initially each element of the array must be set to NULL (to indicated that it is not used), and then later to a dynamically allocated sample struct if it is to be used. Note: after dynamically allocating memory for a sample struct, you have enough memory for each every component of the structure. However, for each component of that structure that is a pointer, you will eventually need to allocated memory for the thing that it points to as well (think string via char* , array via float). You will also have to remember this when freeing memory via a pointer to a sample struct. . Write a separate function to Create a new file and write all the "used" elements of the array (of sample struct s) to this file. You must use binary writes for all of C's built-in numeric types. Write a separate function to Free all of the "used" elements of the array ( of sample struct s) -that means all the memory that was allocated for each element ( the structure and its components). Write a separate function to Read a file of your sample struct s, ( as it was created by your second function) and display .Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1024
typedef struct sample{
char *name;
int list_len;
float *value_list;
}sample;
void WriteToNewFile(struct sample recordList[], int i);
void FreeUsedMemory(struct sample recordList[], int i);
void DisplayWrittenFile();
void main()
{
struct sample *records;
char buff[1000];
char *temp_name;
int temp_len;
char *token;
char *temp_value_list;
sample *temp_record;
int i=0;
int j=0;
FILE *fp = fopen("file.txt", "w+");
while(fgets(buff, 1000, (FILE*)fp) != NULL) //fetch the record from the file until it reaches end of file
{
if(buff != NULL)
{
j=0;
//records[i] = (struct sample*) malloc(sizeof(sample)); //creating the struct element dynamically
temp_name = strtok(buff, ";");
temp_len = atoi(strtok(NULL, ";"));
temp_value_list = strtok(NULL, ";");
records[i].name = (char*)malloc(strlen(temp_name) * sizeof(char));
records[i].value_list = (float*)malloc(temp_len * sizeof(float));
token= strtok(temp_value_list,",");
while(token != NULL && j<temp_len)
{
records[i].value_list[j++] = atof(token);
token = strtok(NULL,",");
}
strcpy(records[i].name , temp_name);
records[i].list_len = temp_len;
i++;
}
}
i--;
WriteToNewFile(records, i);
FreeUsedMemory(records, i);
DisplayWrittenFile();
fclose(fp);
}
void WriteToNewFile(struct sample *recordList, int i)
{
int j=0;
int k=0;
FILE *fp = fopen("newFile.txt","w+");
while(j<i)
{
fprintf(fp, "%s;%d;", recordList[j].name, recordList[j].list_len);
for(k=0;k<recordList[j].list_len-1;k++)
{
fprintf(fp, "%f", recordList[j].value_list[k]);
}
fprintf(fp, "%f ", recordList[j].value_list[k]);
j++;
}
}
void FreeUsedMemory(struct sample *recordList, int i)
{
int j=0;
while(j<i)
{
free(recordList[j].name);
free(recordList[j].value_list);
//free(recordList[j]);
j++;
}
}
void DisplayWrittenFile()
{
char buff[1000];
FILE *fp = fopen("newFile.txt","r");
while(fgets(buff, 1000, (FILE*)fp) != NULL)
{
printf("%s ", buff);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.