this is my first post here so I will apologize in advance if I mess anything up.
I am writing a program for my class in C and the assignment wants us to read in a string 20 characters long from a file, then use a union to take the string and make 5 ints out of it.
All of my int values are coming out the same, and I can not figure out what else to do. All the examples I can find only really care about taking in a string then making it into one int or an int and a float.
my union is set up as follows
union hashes
{
char str[20];
int one;
int two;
int three;
int four;
int five;
};
int main (void)
{
union hashes hashes1;
}
A new attempt at showing more of the program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
union hashes
{
char str[20];
int one;
int two;
int three;
int four;
int five;
};
int main (int argc, char *argv[])
{
//declare union
union hashes hashes1;
//declare struct from link.h
struct node* current;
struct node* new_node;
//file pointer to open file
FILE *fpointer;
fpointer = fopen(argv[1], "r");
//activate first node
current = (struct node*) malloc(sizeof(struct node));
//read from file
while (fread(current, 20, 1, fpointer))
{
//copy the string to the union
strncpy(hashes1.str, current->name, 20);
//add the variables
printf("string: %s\nInts: %d %d %d %d %d\n", hashes1.str, hashes1.one, hashes1.two, hashes1.three, hashes1.four, hashes1.five);
new_node = (struct node*) malloc(sizeof(struct node));
current = new_node;
}
}