Вопрос

At the begining, I'd like to say that I'm very beginer with linux and stuff.

I'm reading a file (line by line) with use of a GetLine.

At the start, I open file descriptor with function open.

Then I change (int)f_descriptor to (FILE*)f_stream (because GetLine requires FILE* arg).

I'm splitting whole line into words (space is a separator) and I place them into a char** words_array. Everything works Ok as long as it's not the last word in the line. For some reason, last words have got some strange chars at the end of them. It doesnt happen always.

enter image description here

Why could this happen?

Это было полезно?

Решение

By the look of things, I suspect you're not null-terminating the last string. The length it's reporting is correct, but the fact that you've got extra bytes might mean that you're copying things into an area of memory that has some, but not all, null bytes in it initially but you're not adding an explicit null byte. If you want a better answer it will help if you can post some of the code where you're reading the data.

Другие советы

You can't simply change an (int) f_descriptor into a (FILE*) f_stream. They're two very very different things. If you use open() to get f_descriptor, you need to use read() and write() to access the file, but if you use fopen() to get f_stream, you use fgetch(), fread(), fwrite(), gets(), puts(), fprintf(), fscanf(), etc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top