문제

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