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?

Recommended Answers

All 2 Replies

getline is not standard C.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.