I'm working on a project for school. Normally I'm a C++ guy, but I have to go through standard C before that, so I'm struggling with certain aspects being different, such as getline. Here's essentially what I'm doing, I have a data file that has a certain about of lines of data. Each set of data is organized the same way and contains 19 pieces to it, consisting of floats, characters, and integers, so there is no trouble there. I read the first 18 elements using fscanf, which works fine, but then switch to getline for the last element which is a string that can contain spaces. However, any time I run this, it crashes with a SIGSEGV, citing strcpy and getdelim as the culprit. Here's the lies of code that really matter, I can post the whole function if necessary, it's just a bit big.

FILE *inputFile = fopen("data.txt", "r");
int bytes = 100;
char *namestr;
int x = 0;

// Get the last part, the name, and the terminating character with getline
namestr = (char *) malloc ((bytes + 1));	
x = getline(&namestr, &bytes, inputFile); // SIGSEGV

Anyone have any ideas?

Edited by shmeeps: Forgot the file variable :P

7 Years
Discussion Span
Last Post by shmeeps

We're required to use getline in this project.

However, I seem to have gotten it to work, so I'm going to go ahead and mark this solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.