Edited, because if the file being read did not end with a line break ('\n'
), the last character would be deleted. A test is placed to verify that the last character of the line read is '\n'
before replacing.
As the user72726 explained in his reply, the fgets function includes line advance when reading from the file ('\n'
). At first I thought of inserting the semicolon when reading the file, but this limits some other treatment you may want to do.
I suggest taking this line forward while reading from the archive
while (fgets(linha1,255,file)!=NULL) {
linha *l1 =(linha*) malloc(sizeof(linha));
if (linha1[strlen(linha1) - 1] == '\n') // testa se o último caractere é \n
linha1[strlen(linha1) - 1] = '\0'; // troca o \n pelo char null
l1->produtos =(char*) malloc(strlen(linha1)+1);
strcpy(l1->produtos,linha1);
l1->prev = tail;
tail = l1;
}
and then add the semicolon and the line advance when displaying on the screen
while (current != NULL) {
printf("- %s;\n",current->produtos);
current = current->prev;
}
It works because functions that handle strings stop when finding a null character - see the ref. from the following link strcpy, at the end of the first sentence "including the terminating null Character (and Stopping at that point)".
Note that if done sizeof(linha1)
will look different from strlen(linha1)
, the normal, if I’m not mistaken, is the difference being 1 but here will be 2 because linha1
is a pointer that at the end has two null characters instead of 1, which strlen does not count.
The above paragraph is incorrect because sizeof(linha1)
in the case of this program will always be 255, as it is the size allocated in the declaration.
One important thing, though off the subject of the question, is to release all the memory you use. So, if it were me, for each malloc I would put a memfree on the program even if the program was to close soon after.
References (other functions appear in the menu on the left):
http://www.cplusplus.com/reference/cstring/strlen/
http://www.cplusplus.com/reference/cstring/strcpy/
adding the null character will cause memory leakage, because so you are discarding the
\0
original. Although it is not much (1byte), it is good practice to return all memory to OS.– user72726
@user72726 I don’t think so, because the compiler has information about how much memory the pointer (line1) uses, it doesn’t depend on string handling methods to know how much memory it needs to release, it already knows. We’re only fooling the functions that mess with the string, not the memory management.
– fbiazi
@user72726 I hadn’t even noticed, the array line1 is declared with size 255, simpler yet, no problem in changing end-of-line character by null. 255 bytes of , content will always be released. In reality this program has no memfree, I would definitely put one for each malloc. I’ll include a comment on that in the reply right now.
– fbiazi