C dynamic array of structures, no errors, program terminates when ran - c

When ran, the program immediately terminates from an issue I believe to be associated with memory allocation. The main function only calls this function and another to delete the memory allocated
DrinkMachine *create(void){
FILE *inFile;
//Pointer to data structure
DrinkMachine *drinkMachine;
// Memory allocation
drinkMachine = calloc(1, sizeof(DrinkMachine));
if(drinkMachine == NULL) // Check success
return NULL;
// Open the input file for reading
inFile = fopen("drink_machine.txt" , "r");
if(!inFile){
puts("Error opening file");
return NULL;
}
// Read drink count from file
fscanf(inFile, "%d", &(drinkMachine->numItems));
printf("DEBUG read file arrayLen: %d\n", drinkMachine->numItems);
// Dynamically create array of drink item structures
drinkMachine->drinkItem = malloc(drinkMachine->numItems*sizeof(DrinkItem));
if(drinkMachine->drinkItem == NULL){
puts("ERROR: Failed to allocate memory");
return NULL;
}
// Put information from file into drinkItem structs
for(int i=0; i < drinkMachine->numItems; ++i){
fscanf(inFile, "%s %lf %d", (drinkMachine->drinkItem[i].name), &(drinkMachine->drinkItem[i].price), &(drinkMachine->drinkItem[i].drinkLeft));
printf("DEBUG drink %d is: %s %lf %d\n", i, (drinkMachine->drinkItem[i].name), (drinkMachine->drinkItem[i].price), (drinkMachine->drinkItem[i].drinkLeft));
}
// Close inFile
fclose(inFile);
// Force output to screen
puts("DEBUG readFile Success!");
fflush(stdout);
return drinkMachine;
}
The program ran into errors or wouldn't properly allocate memory and would successfully output the error message when ran, until I put in the line:
drinkMachine->drinkItem = malloc(drinkMachine->numItems*sizeof(DrinkItem));
At this point the program compiles without warning or errors, but terminates immediately when ran without any output. In case it helps, here are the structures:
typedef struct _DrinkItem{
int id;
char *name;
double price;
int drinkLeft;
int drinkSold;
} DrinkItem;
typedef struct _DrinkMachine{
int version;
int numItems;
int drinkLocation;
DrinkItem *drinkItem;
} DrinkMachine;

You have to allocate storage for each name too. You are reading characters into an unallocated pointer on line 30. You should read the name into a temporary array, get the name’s length, allocate (length+1) bytes of storage to name, and strncpy the data over.

You didn't allocate name space, you didn't handle any input error, you use int without verify that it not negative for a size, you didn't use stderr for error, you used reserved identifier and more.
Here a proposition of code, that I think fix all your errors (didn't test it as you didn't give example of input and output):
#include <stdio.h>
#include <stdlib.h>
typedef struct DrinkItem {
int id;
char *name;
double price;
int drinkLeft;
int drinkSold;
} DrinkItem;
typedef struct DrinkItem {
int version;
size_t numItems;
int drinkLocation;
DrinkItem *drinkItems;
} DrinkMachine;
static void helper_free(DrinkItem *drinkItems, size_t n) {
for (size_t i = 0; i < n; i++) {
free(drinkItems[i].name);
}
free(drinkItems);
}
DrinkMachine *create(char const *path) {
FILE *inFile = fopen(path, "r");
if (!inFile) {
fprintf(stderr, "Error opening file");
return NULL;
}
size_t numItems;
if (fscanf(inFile, "%zu", &numItems) != 1) {
fprintf(stderr, "Error parsing\n");
return NULL;
}
#ifndef NDEBUG
printf("DEBUG read file arrayLen: %zu\n", numItems);
#endif
DrinkItem *drinkItems = malloc(numItems * sizeof *drinkItems);
if (!drinkItems) {
fprintf(stderr, "ERROR: Failed to allocate memory");
return NULL;
}
for (size_t i = 0; i < numItems; ++i) {
char *name = malloc(100);
if (!name) {
helper_free(drinkItems, i);
fprintf(stderr, "ERROR: Failed to allocate memory");
return NULL;
}
double price;
int drinkLeft;
if (fscanf(inFile, "%99s %lf %d", name, &price, &drinkLeft) != 3) {
free(name);
helper_free(drinkItems, i);
fprintf(stderr, "Error parsing\n");
return NULL;
}
drinkItems[i] =
(DrinkItem){.name = name, .price = price, .drinkLeft = drinkLeft};
#ifndef NDEBUG
printf("DEBUG drink %zu is: %s %lf %d\n", i, name, price, drinkLeft);
#endif
}
fclose(inFile);
DrinkMachine *drinkMachine = malloc(sizeof *drinkMachine);
if (!drinkMachine) {
helper_free(drinkItems, numItems);
fprintf(stderr, "ERROR: Failed to allocate memory");
return NULL;
}
*drinkMachine =
(DrinkMachine){.drinkItems = drinkItems, .numItems = numItems};
#ifndef NDEBUG
puts("DEBUG readFile Success!\n");
#endif
return drinkMachine;
}

Related

Why is fscanf() seg faulting while trying to read from a file?

I am using C in Visual Studio via a remote Linux server. I want to read a file and store the contents in a array of structs. Every time I try to read a line using fscanf() it seg faults. Thanks in advance.
Format of the file I'm trying to read:
F150 5.4 28000 white
RAM1500 5.7 32000 orange
car 4.5 12000 green
truck 6.1 55000 black
Here's a simplified version my program as the other parts function fine:
#include <stdlib.h>
#include <stdio.h>
struct data {
char name[20];
float floatNum;
int intNum;
char color[20];
} temp;
int scan(void) {
int size = 0;
FILE *data;
data = fopen("./hw3.data", "r");
while (1) {
fscanf(data, "%s %f %d %s", temp.name, &temp.floatNum,
&temp.intNum, temp.color);
if (feof(data))
break;
size++;
}
return size;
}
void load(int size, struct data autos[]) {
int i;
FILE *data;
data = fopen("./hw3.data", "r");
for (i = 0; i < size; i++) {
fscanf(data, "%s %f %d %s", autos[i].name, &autos[i].floatNum,
&autos[i].intNum, autos[i].color);
}
}
int main() {
int size;
struct data *autos;
size = scan();
autos = malloc(size * sizeof(struct data));
load(size, autos);
return 0;
}
There are multiple possible causes for problems:
you do not test if fopen() succeeds: if the file cannot be opened, the FILE pointer data will be null, causing undefined behavior in fscanf(), possibly a seg fault.
you do not test if malloc succeeds... again causing a seg fault if memory cannot be allocated.
you should close the FILE after reading
your test for feof() is incorrect: it might be true after successfully reading the last item, causing it to be ignored and you might never reach the end of file if one of the items cannot be read. You should just test the return value of fscanf(): it returns the number of successful conversions, so 4 in your case.
you should use %19s to avoid writing beyond the end of the targets arrays, another potential source of undefined behavior.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data {
char name[20];
float floatNum;
int intNum;
char color[20];
};
int scan(const char *filename) {
struct data temp;
int size = 0;
FILE *data = fopen(filename, "r");
if (data == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return -1;
}
while (fscanf(data, "%19s %f %d %19s",
temp.name, &temp.floatNum,
&temp.intNum, temp.color) == 4) {
size++;
}
fclose(data);
return size;
}
int load(const char *filename, int size, struct data autos[]) {
int i;
FILE *data = fopen(filename, "r");
if (data == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return -1;
}
for (i = 0; i < size; i++) {
if (fscanf(data, "%19s %f %d %19s",
autos[i].name, &autos[i].floatNum,
&autos[i].intNum, autos[i].color) != 4)
break;
}
fclose(data);
return i;
}
int main() {
char filename[] = "./hw3.data";
int size = scan(filename);
if (size < 0)
return 1;
if (size > 0) {
struct data *autos = malloc(size * sizeof(struct data));
if (autos == NULL) {
fprintf(stderr, "cannot allocate %zu bytes\n", size * sizeof(struct data));
return 1;
}
int n = load(filename, size, autos);
for (int i = 0; i < n; i++) {
printf("%s %g %d %s\n", autos[i].name, autos[i].floatNum,
autos[i].intNum, autos[i].color);
}
free(autos);
}
return 0;
}

Reading lines from file

I am trying to read strings and integers from a simple text file to my array. But the problem is that I get some random characters in a line in the middle of my list. It probably has to do with a newline problem, but I am not sure. The text file looks like this:
4
Mr Tambourine Man
Bob Dylan
1965
Dead Ringer for Love
Meat Loaf
1981
Euphoria
Loreen
2012
Love Me Now
John Legend
2016
The first number (4), indicates how many songs there are in the list. I have made a struct which will be able to hold the songs and dynamically allocate memory for each pointer.
Struct:
typedef struct Song {
char *song;
char *artist;
int *year;
} Song;
Allocated:
Song *arr;
arr = (Song*)malloc(sizeof(Song));
Function:
int loadFile(char fileName[], Song *arr, int nrOf) {
FILE *input = fopen(fileName, "r");
if (input == NULL) {
printf("Error, the file could not load!\n");
} else {
int i = 0;
fscanf(input, "%d\n", &nrOf);
for (int i = 0; i < nrOf; i++) {
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
arr[i].year = (int*)malloc(sizeof(int));
fgets(arr[i].song, 100, input);
fgets(arr[i].artist, 100, input);
fscanf(input, "%d\n", arr[i].year);
}
printf("The file is now ready.\n");
fclose(input);
}
return nrOf;
}
Are you able to find the problem? Or do you have a better solution?
This is wrong:
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
You are only allocating buffers of size 1, there's no scaling. This gives you undefined behavior when you overrun the buffers by loading more data into them than they can hold.
I would expect those to read:
arr[i].song = malloc(100);
and so on. Note that no cast is necessary, and sizeof (char) is always 1.
Also, this:
arr[i].year = (int*)malloc(sizeof(int));
is super-strange. There's absolutely no reason to dynamically allocate a single integer, just make the field an int and store the value there directly.
First Issue:
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
Are only allocating 1 byte for your char* pointers, song and artist. You can allocate a size for this:
arr[i].song = (char*)malloc(100 * sizeof(char)); /* or malloc(100) */
arr[i].artist = (char*)malloc(100 * sizeof(char));
Or you can simply malloc() enough space from you buffer:
char buffer[100];
fgets(buffer, 100, input);
/* check for failure, remove newline */
arr[i].song = malloc(strlen(buffer)+1);
/* check error from malloc */
strcpy(arr[i].song, buffer);
Or even use strdup():
arr[i].song = strdup(buffer);
Which is a substitute for malloc()/strcpy().
Note: You can also read Do I cast the result of malloc?.
Second Issue:
Your current struct:
typedef struct Song {
char *song;
char *artist;
int *year;
} Song;
Can be simplified to:
typedef struct {
char *song;
char *artist;
int year;
} Song;
Because year does not need to be a pointer. Easier to manage if its just an int. This avoids having to do allocations like:
arr[i].year = (int*)malloc(sizeof(int));
Other Recommendations:
You should check the return of fscanf() and fgets() as its safe to do this. It helps just incase your file will have incorrect data. This goes the same for malloc(), which can return NULL is unsuccessfully allocated on the heap.
Here is some code with the above considerations in mind:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct {
char *song;
char *artist;
int year;
} Song;
Song *create_array(FILE *input, int *nrof);
void load_data(Song *arr, FILE *input, int nrof);
void print_free_data(Song *arr, int nrof);
void get_buffer(char buffer[], FILE *input);
int main(void) {
FILE *input;
Song *arr;
int nrof;
input = fopen("artist.txt", "r");
if (input == NULL) {
fprintf(stderr, "Error reading file\n");
exit(EXIT_FAILURE);
}
arr = create_array(input, &nrof);
load_data(arr, input, nrof);
print_free_data(arr, nrof);
fclose(input);
return 0;
}
Song *create_array(FILE *input, int *nrof) {
Song *arr;
if (fscanf(input, "%d ", nrof) != 1) {
fprintf(stderr, "Cannot find number of songs\n");
exit(EXIT_FAILURE);
}
arr = malloc(*nrof * sizeof(*arr));
if (arr == NULL) {
fprintf(stderr, "Cannot allocate %d spaces for array\n", *nrof);
exit(EXIT_FAILURE);
}
return arr;
}
void load_data(Song *arr, FILE *input, int nrof) {
char buffer[SIZE];
for (int i = 0; i < nrof; i++) {
get_buffer(buffer, input);
arr[i].song = malloc(strlen(buffer)+1);
if (arr[i].song == NULL) {
fprintf(stderr, "Cannot allocate song\n");
exit(EXIT_FAILURE);
}
strcpy(arr[i].song, buffer);
get_buffer(buffer, input);
arr[i].artist = malloc(strlen(buffer)+1);
if (arr[i].artist == NULL) {
fprintf(stderr, "Cannot allocate artist\n");
exit(EXIT_FAILURE);
}
strcpy(arr[i].artist, buffer);
if (fscanf(input, "%d ", &arr[i].year) != 1) {
fprintf(stderr, "Cannot find year for Song: %s Album: %s\n",
arr[i].song, arr[i].artist);
exit(EXIT_FAILURE);
}
}
}
void get_buffer(char buffer[], FILE *input) {
size_t slen;
if (fgets(buffer, SIZE, input) == NULL) {
fprintf(stderr, "Error from fgets(), line not read\n");
exit(EXIT_FAILURE);
}
slen = strlen(buffer);
if (slen > 0 && buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
fprintf(stderr, "Too many characters entered\n");
exit(EXIT_FAILURE);
}
}
void print_free_data(Song *arr, int nrof) {
for (int i = 0; i < nrof; i++) {
printf("%s\n%s\n%d\n\n", arr[i].song, arr[i].artist, arr[i].year);
free(arr[i].song);
arr[i].song = NULL;
free(arr[i].artist);
arr[i].artist = NULL;
}
free(arr);
arr = NULL;
}
Which Outputs correct data:
Mr Tambourine Man
Bob Dylan
1965
Dead Ringer for Love
Meat Loaf
1981
Euphoria
Loreen
2012
Love Me Now
John Legend
2016
Your memory allocation is incorrect. The structure should have char arrays for the song and artist names and an int for the year, and you should modify your API to return the array and its size to the caller:
int loadFile(const char *fileName, Song **arr, int *numberp);
Here is a corrected and simplified of your program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Song {
char song[100];
char artist[100];
int year;
} Song;
/* call as
if (loadFile(fileName, &songs, &songs_size) < 0) {
// deal with error...
}
*/
int loadFile(const char *fileName, Song **arrp, int *numberp) {
FILE *input;
Song *arr;
int i, nrOf;
input = fopen(fileName, "r");
if (input == NULL) {
fprintf(stderr, "Cannot open file %s\n", filename);
return -1;
} else {
if (fscanf(input, "%d\n", &nrOf) != 1) {
fprintf(stderr, "%s: missing number of items\n", filename);
fclose(intput);
return -1;
}
arr = calloc(sizeof(*arr), nrOf);
if (arr == NULL) {
fprintf(stderr, "cannot allocate memory for %d items\n", nrOf);
fclose(intput);
return -1;
}
for (int i = 0; i < nrOf; i++) {
char cc;
if (fscanf(input, "%99[^\n]%*c%99[^\n]%*c%d%c",
sarr[i].song, arr[i].artist,
&arr[i].year, &cc) != 4 || cc != '\n') {
fprintf(stderr, "%s: invalid format for item %d\n",
filename, i);
break;
}
}
printf("The file is now ready.\n");
fclose(input);
*arrp = arr;
*numberp = i;
return i;
}
}

What is this bug about? Structs, Pointers, Dynamic Memory Allocation , C

I'm writing a simple banking application in c
It saves the information in a file.
I want to load the file each time the app runs, and add the information from the file to the struct, for this purpose, I have written two functions called "loadfile" and "allocate"
In the function "loadfile" if I un-comment the comment lines, the OS throws a "stopped working" in my face :|
Can you help me with it ?
when I use (acc+i) in the "loadfile", the error shows up.
Is there a syntax problem? :o
thanks
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account;
account *acc;
int allocate ( account *acc ) {
int num = 0 ;
char tempname[20],tempbranch[10];
int tempid = -1 ,tempbalance;
FILE *file;
file = fopen("D://bank.txt","r");
while ( !feof(file) ) {
fscanf(file,"%s %d %d %s ",tempname, &tempid, &tempbalance, tempbranch);
if (tempid != -1)
num++;
}
acc = ( account *) realloc ( acc, num * sizeof(account) );
fclose(file);
printf(" num in allocate function : %d",num);
return num;
}
int loadfile (account *acc) {
int num = allocate(acc);
char tempname[20],tempbranch[10];
int tempid ,tempbalance;
if ( num != 0 ) {
int i = 0 ;
FILE *file;
file = fopen("D:\\bank.txt","r+");
for ( i = 0 ; !feof(file) && i < num ; i++ ) {
fscanf(file,"%s ",tempname );
fscanf(file,"%d ",&tempid );
fscanf(file,"%d ",&tempbalance );
fscanf(file,"%s ",tempbranch );
printf("\n i is %d \n",i);
/* strcpy( ((acc+i)->name) , tempname);
(acc+i)->id = tempid;
(acc+i)->balance = tempbalance;
strcpy( ((acc+i)->branch) , tempbranch); */
}
fclose(file);
}
return num;
}
There are a lot of issues with the posted code. It is unclear how a separate allocation function is helpful, and the use of the file-scope variable acc is not advisable. I see no point in using realloc() here, since allocation is done only once. If you do use realloc(), you should store the result in a temporary pointer, because the function can return a NULL pointer if there is an allocation error. This leads to a memory leak if you assign directly to the pointer that you are reallocating from. I have rewritten the code to illustrate some fixes, trying to maintain the general structure of the original code.
You should check the return values of the functions that you call. realloc() and malloc() return a pointer to the allocated memory, or a NULL pointer in the event of an allocation error. You should check this value and handle the result. The scanf() functions return the number of successful assignments made. You should check this value to verify that input is as expected. It is almost always a bad idea to control a loop with feof(), because this function relies upon the end-of-file indicator being set, and this indicator is only set when an I/O operation has failed.
In the program below, fgets() is used to read a line of input from the file into a buffer, and sscanf() is used to extract the input data from the buffer. In the allocation phase, EOF or an empty line signals the end of the data. No parsing is done here, only a count of lines. For each line, space is allocated for an account. Note that the code checks for errors on opening and closing the file, as well as for an allocation error.
The loadfile() function again uses fgets() to read a line of input into buffer, and then uses sscanf() to scan the buffer. Note the use of width specifiers for the strings. These are one less than the size of the arrays that they read into, to leave space for the '\0' placed at the end of the strings by sscanf(). Also note that in the event that fewer than 4 assignments are made, the program exits with an error message. If the assignments to the temporary variables were successful, the account is updated with the data.
There are many ways that this code could be improved (most obviously by getting rid of the global variable acc), but this should provide you with a good starting point.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account;
account *acc = NULL;
int allocate(void)
{
int num = 0 ;
char buffer[1000];
FILE *file;
file = fopen("D://bank.txt","r");
if (file == NULL) {
fprintf(stderr, "Unable to open file in allocate()\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), file) != NULL &&
buffer[0] != '\n') {
num++;
}
acc = malloc(num * sizeof(*acc));
if (acc == NULL) {
fprintf(stderr, "Allocation error in allocate()\n");
exit(EXIT_FAILURE);
}
if (fclose(file) != 0) {
fprintf(stderr, "Unable to close file in allocate()\n");
exit(EXIT_FAILURE);
}
printf(" num in allocate function : %d\n",num);
return num;
}
int loadfile(void)
{
int num = allocate();
char buffer[1000], tempname[20],tempbranch[10];
int tempid ,tempbalance;
if ( num != 0 ) {
int i = 0 ;
FILE *file;
file = fopen("D://bank.txt","r+");
if (file == NULL) {
fprintf(stderr, "Unable to open file in loadfile()\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), file) != NULL
&& buffer[0] != '\n') {
if (sscanf(buffer, "%19s %d %d %9s",
tempname, &tempid, &tempbalance, tempbranch) != 4) {
fprintf(stderr, "%d: Malformed input data\n", i);
exit(EXIT_FAILURE);
}
strcpy(acc[i].name, tempname);
acc[i].id = tempid;
acc[i].balance = tempbalance;
strcpy(acc[i].branch, tempbranch);
++i;
}
if (fclose(file) != 0) {
fprintf(stderr, "Unable to open file in loadfile()\n");
exit(EXIT_FAILURE);
}
}
return num;
}
int main(void)
{
int num = loadfile();
for (int i = 0; i < num; i++) {
printf("%s %d %d %s\n",
acc[i].name, acc[i].id, acc[i].balance, acc[i].branch);
}
return 0;
}
I can't explain all your problems. It will take me hours. I hope this code will be self-describing. Ask me in a comment if you need some help.
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account_t;
static account_t *parse_account_file(FILE *file, size_t *size) {
if (file == NULL || size == NULL) {
return NULL;
}
size_t i = 0;
account_t *account = malloc(sizeof *account);
if (account == NULL) {
return NULL;
}
int ret;
while (
(ret = fscanf(file, "%19s %d %d %9s\n", account[i].name, &account[i].id,
&account[i].balance, account[i].branch)) == 4) {
account_t *old = account;
account = realloc(account, sizeof *account * (++i + 1));
if (account == NULL) {
free(old);
return NULL;
}
}
if (ret == EOF) {
if (ferror(file)) {
perror("parse_account_file()");
} else {
*size = i;
account_t *old = account;
account = realloc(account, sizeof *account * i);
if (account == NULL) {
return old;
}
return account;
}
} else {
fprintf(stderr, "error parsing\n");
}
free(account);
return NULL;
}
int main(void) {
char const *name = "D:\\bank.txt";
FILE *file = stdin;
size_t size;
account_t *account = parse_account_file(file, &size);
fclose(file);
if (account == NULL) {
return 1;
}
for (size_t i = 0; i < size; i++) {
printf("%s %d %d %s\n", account[i].name, account[i].id, account[i].balance,
account[i].branch);
}
free(account);
}

C Linked List from file not exiting after printing

So, I have to print a linked list from a file input, which I've managed to get working:
#include <stdio.h>
#include <stdlib.h>
typedef struct Vehicle{
int option;
char make [30];
char model[30];
int car_manufacture_date;
float maximum_velocity;
float mass;
int seats;
struct Vehicle *next;//linked list node
} vehicle_t;
int main (){
FILE* fp;
fp = fopen("vehicles.crash.txt", "r");
vehicle_t* first_car = malloc(sizeof(vehicle_t));
if (first_car == NULL){
printf("Error. Failed to allocate memory to first_car\n");
exit(1);
}
vehicle_t* current_vehicle = malloc(sizeof(vehicle_t));
if (current_vehicle == NULL){
printf("Error. Failed to allocate memory to current_vehicle\n");
exit(1);
}
vehicle_t* new_vehicle = malloc(sizeof(vehicle_t));
if (new_vehicle == NULL){
printf("Error. Failed to allocate memory to new_vehicle\n");
exit(1);
}
printf("GOOD1\n");
current_vehicle = first_car;
new_vehicle = first_car;
printf("GOOD2\n");
//Loading vehicles from file to linked list
if (fp != NULL)
{
printf("GOOD3\n");
while (fscanf(fp,"%d %s %s %d %f %f %d", &new_vehicle->option, new_vehicle->make, new_vehicle->model, &new_vehicle->car_manufacture_date,
&new_vehicle->maximum_velocity, &new_vehicle->mass, &new_vehicle->seats) != EOF)
{
printf("GOOD4\n");
current_vehicle->next = new_vehicle;
current_vehicle = current_vehicle->next;
new_vehicle = malloc(sizeof(vehicle_t));
if (first_car == NULL){
printf("Error. Failed to allocate memory\n");
new_vehicle->next=NULL;
exit(1);
}
printf("GOOD5\n");
}
close(fp);
printf("Input completed\n");
}
else
printf("Error! couldn't find file\n");
current_vehicle = first_car;
while (current_vehicle != NULL)
{
printf("Option: %d\tMake: %s\tModel: %s\tManufactured: %d\tMax Velocity: %.2f\tMass: %.2f\tSeats: %d\n",
current_vehicle->option, current_vehicle->make, current_vehicle->model, current_vehicle->car_manufacture_date,
current_vehicle->maximum_velocity, current_vehicle->mass, current_vehicle->seats);
new_vehicle = current_vehicle->next;
current_vehicle = current_vehicle->next;
};
printf("Printing completed");
return 0;
}
Everything works fine right until the last file item is printed out, after which the program crashes. From what I've seen in other posts, the while loop matches them all.
The printed "GOOD" statements are just checkpoints
The text in the file is formatted as: 1 Toyota Camry 2010 200.0 1100.0 5
I think you forget to set the ->next field of the last list node to NULL. You just called malloc, so the value could be anything random, making your printing failed.
The main reason for the infinite loop was failing to set the final next to NULL [as others have mentioned].
But, I think this is easier to see with the code simplified as yours, although mostly correct, was more complicated than it needed to be.
Anyway, this compiles but I didn't test it [please pardon the gratuitous style cleanup]:
#include <stdio.h>
#include <stdlib.h>
typedef struct Vehicle {
int option;
char make[30];
char model[30];
int car_manufacture_date;
float maximum_velocity;
float mass;
int seats;
struct Vehicle *next; // linked list node
} vehicle_t;
int
main()
{
FILE *fp;
fp = fopen("vehicles.crash.txt", "r");
if (fp == NULL) {
printf("Error! couldn't find file\n");
exit(1);
}
vehicle_t *first_car = NULL;
vehicle_t *previous_vehicle = NULL;
// Loading vehicles from file to linked list
while (1) {
vehicle_t *new_vehicle = malloc(sizeof(vehicle_t));
if (new_vehicle == NULL) {
printf("Error. Failed to allocate memory to new_vehicle\n");
exit(1);
}
// NOTE: the lack of this [in your equivalent code] was the reason
// for the infinite loop
new_vehicle->next = NULL;
if (fscanf(fp, "%d %s %s %d %f %f %d",
&new_vehicle->option, new_vehicle->make, new_vehicle->model,
&new_vehicle->car_manufacture_date, &new_vehicle->maximum_velocity,
&new_vehicle->mass, &new_vehicle->seats) == EOF) {
free(new_vehicle);
break;
}
if (first_car == NULL)
first_car = new_vehicle;
else
previous_vehicle->next = new_vehicle;
previous_vehicle = new_vehicle;
}
fclose(fp);
vehicle_t *current_vehicle = first_car;
while (current_vehicle != NULL) {
printf("Option: %d\tMake: %s\tModel: %s\tManufactured: %d\tMax Velocity: %.2f\tMass: %.2f\tSeats: %d\n",
current_vehicle->option, current_vehicle->make,
current_vehicle->model, current_vehicle->car_manufacture_date,
current_vehicle->maximum_velocity, current_vehicle->mass,
current_vehicle->seats);
current_vehicle = current_vehicle->next;
};
printf("Printing completed");
return 0;
}

C - Opening differents files using same pointer

I'm trying to retrieve informations by many plain-text files, which will be then stored in a proper struct. To do so, I'm using a function that takes member of the struct to populate and source of the plain-text file where the informations are stored.
Posting my "test" code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _elem
{
const char *title;
int ok;
int almost;
int nope;
int hits;
float last_rank;
};
typedef struct _elem Chapter;
Chapter *generate_array(const char *source, int *elems);
int engine_start(Chapter *elem, char *source);
int main()
{
const char path_f[100];
int elements = 0;
int i = 0;
Chapter *dict;
printf("Insert the name of the source:\n");
scanf("%s", path_f);
printf("\nGenerating dictionary, please wait...\n");
dict = generate_array(path_f, &elements);
if (dict == NULL)
{
printf("Aborting.\n");
exit(1);
}
while (i < elements)
{
printf("Element %d:\n", (i + 1));
printf("\nTitle: %s\n", dict[i].title);
printf("Ok: %10d\n", dict[i].ok);
printf("Almost: %5d\n", dict[i].almost);
printf("Nope: %8d\n", dict[i].nope);
printf("Hits: %8d\n", dict[i].hits);
printf("Rank: %8.2f\n", dict[i].last_rank);
printf("\n");
i++;
}
return EXIT_SUCCESS;
}
Chapter *generate_array(const char *source, int *elems)
{
FILE *src;
int sources;
int i = 0;
char **srcs;
Chapter *generated;
src = fopen(source, "r");
if (src == NULL)
{
printf("[!!] Error while reading file!\n");
return NULL;
}
fscanf(src, "%d", &sources);
if (sources <= 0)
{
printf("[!!] Wrong number of sources, exiting.\n");
return NULL;
}
srcs = (char **) malloc(sizeof(char *) * sources);
while (i < sources && !feof(src))
{
srcs[i] = (char *) malloc(sizeof(char) * 100);
fscanf(src, "%s", srcs[i++]);
}
fclose(src);
generated = (Chapter *) malloc(sizeof(Chapter) * i);
*elems = i;
i = 0;
while (i < *elems)
{
if(engine_start( &generated[i], srcs[i] )) i++;
else
{
printf("[!!] Error in file %s, aborting.\n", srcs[i]);
return NULL;
}
}
return generated;
}
int engine_start(Chapter *elem, char *source)
{
FILE *parser;
int done = 0;
parser = fopen(source, "r");
if (parser == NULL) printf("[!!] Error while opening %s, aborting.\n", source);
else
{
fgets(elem->title, 100, parser);
fscanf(parser, "%d %d %d %d %f", &(elem->ok), &(elem->almost),
&(elem->nope), &(elem->hits),
&(elem->last_rank) );
fclose(parser);
done = 1;
}
return done;
}
Now this is the main file where are stored paths to the other plain-text files:
lol.dat
5
lold/lol1.dat
lold/lol2.dat
lold/lol3.dat
lold/lol4.dat
lold/lol5.dat
And one example of lolX.dat:
Qual'è la vittoria di cristo?
3 4 5 12 44.9
I'm getting SIGSEGV after the first iteration of "engine_start", probably due to FILE *parser (but I can be totally wrong, I don't know at this point).
Someone can guide me through this problem? Thank you.
Make the following changes and try-
struct _elem
{
char *title; // allocate the memory for this.
int ok;
int almost;
int nope;
int hits;
float last_rank;
};
You need to allocate memory for element title before assigning something to it.
int engine_start(Chapter *elem, char *source)
{
FILE *parser;
int done = 0;
parser = fopen(source, "r");
if (parser == NULL) printf("[!!] Error while opening %s, aborting.\n", source);
else
{
elem->title=(char *)malloc(100); // include this line.
fgets(elem->title, 100, parser);
fscanf(parser, "%d %d %d %d %f", &(elem->ok), &(elem->almost),
&(elem->nope), &(elem->hits),
&(elem->last_rank) );
fclose(parser);
done = 1;
}
return done;
}

Resources