shouldnt fgets ignore newline characters?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
MrMr[iCE]
Posts: 43
Joined: Mon Oct 03, 2005 4:55 pm

shouldnt fgets ignore newline characters?

Post by MrMr[iCE] »

In the application I'm writing, I'm using fgets to read lines from a text file. Problem is when I render the strings with pspDebugScreenPrintf or my own font routines, I see newline characters in the strings.

Granted fgets does terminate a string at the newline, it should not include the newline characters themselves. I have tried saving in unix format (char 10 for new line) and I still see char 10 at the end. If I save in PC format, then I get both characters (char 13, char 10) at the end. Any way to fix that or is this built into the psp's kernel?
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

Thats whats supposed to happen:
http://www.mkssoftware.com/docs/man3/fgets.3.asp

Do something like:

Code: Select all

fgets(strbuff, 1024, file);
strbuff[strlen(strbuff)-1] = 0;
or...

Walk through your string and patch any nonreadable text with spaces

Code: Select all

int len = strlen(strbuff);
for(int i=0; i!=len; i++)
  if &#40;strbuff&#91;i&#93; < ' ' || strbuff&#91;i&#93; > '~'&#41;
    strbuff&#91;i&#93; = ' ';
Paco
Posts: 54
Joined: Sun Oct 09, 2005 6:53 pm

Post by Paco »

Yeah that peculiarity of fgets() prompted me to write a little function to remove ctrl chars from a string. Then I made it remove leading and trailing spaces. At some point it started removing C and C++ style comments and could applied to entire text files. It has become the oldest piece of code I bring to every project I do, and I'm afraid it will outlive me... thank god it doesn't do unicode! :)
Paco
Post Reply