C: loading a file with files to open using other program
Let me see if I can explain this well enough. This is C programming. I have a link List that I use to put path/filename into. It works fine. I just used it for this function that opens and reads a file with the path/filename in the file and put them into the link list instead. It works too.
It is when I am reading the files out of the lest after it has been populated by the file that it is causing me issues. I am getting a bad image return on the file when it uses the file to populate it. the kicker is. When I only put one path/filename into the file then load that it works fine. It loads the image and displays it without issue, yet, anything more than that it goes back to giving me errors bad image. Code:
filename /home/userx/Pictures/slack-wallpapers-0.446/images/slack-orange-1600x1200.jpg so being that it works with just one path/filename within a file but no more, then logic states it has to be the file itself. But what could be doing that? end line off every line within the file, if yes, then how to fix that, if no, then what? Code:
void read_file(FILE *ptr, char *file) |
HAHAHA
Sometimes It helps to write it out. :D ANSWER: added Code:
len = strlen(line); Code:
void read_file(FILE *ptr, char *file) |
I've done the same to truncate things like the \r\n in a string where I didn't need that component.
Stylistically I've just done it differently: Code:
line[strlen(line) - 1] = 0x00; |
Quote:
Yep that work too. I'll keep it! thanks! :hattip: |
Don't forget to free(line) after the loop
man getline Code:
... |
Quote:
Code:
Thanks ... |
A variant:
Code:
if (len>0 && line[len-1]=='\n') |
An empty line has to contain at least one char, no? (newline)
|
When the new-line (LF) and the optional carriage-return (CR) have been stripped, it has no characters.
|
Quote:
I do realize this can cause hassles with unexpected strings that are too long, however the functions will not give you more than you have specified for the length of your buffer they will give you one byte less than your buffer length and terminate it with a NULL. For me, in the unexpected case, there really is a problem somewhere that needs to be dealt with. |
Quote:
length -1 due to the array effect then adds the null on the end; Code:
line[len - 1] = 0x00; |
Quote:
|
Quote:
|
Quote:
Modifed Code:
//add null to end |
Quote:
this 0x00 is just saying this '\0' in a different language. This too works BTW Code:
if ( (big>0 && line[big-1]=='\n') || (big>0 && line[big-1]=='\r') ) |
All times are GMT -5. The time now is 03:06 AM. |