Advertisement

how to keep fgets() from loading the carraige return?

Started by February 19, 2003 09:59 PM
6 comments, last by TwistedMatrix 21 years, 8 months ago
does anyone know how to keep fgets() from copying the carrage return into the char buffer?
- Twisted Matrix
i have been doing it like this but there is probly a better way

fgets(str,100,f);
i=0;
while (str != ''\n'') {
path_suffix=str;<br> i++;<br> } path_suffix=''\0''; </i>
- Twisted Matrix
Advertisement
i use StrTrim.
StrTrim, hmm, I''ve never heard of that before, I use

fgets(str,?,f);
char* c = strchr(str,''\n'');
*c = ''\0'';
Why not use


  // string_to_use is the string in question// might have to use (strlen(string_to_use) + 1)// depending on systemstring_to_use[strlen(string_to_use)] = '\0';  


Or you could always just use fgetc and loop until you encounter the newline character ('\n').

-----------------------------
Final Frontier Trader

[edited by - biovenger on February 20, 2003 11:35:42 AM]
-----------------------------Final Frontier Trader
quote: Original post by AikonIV
fgets(str,?,f);
char* c = strchr(str,'\n');
*c = '\0';


That's the method I use. However, you should check to make sure c isn't NULL before doing the assignment. Otherwise, if the last line in your file doesn't have a newline, you'll get a core dump or GPF.


[edited by - Dave Hunt on February 20, 2003 1:30:43 PM]
Advertisement
You might also want to check for ''\r'', too.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
you are opening the file in text mode aren''t you???

infile = fopen (filename, "rt");


which will automatically convert \n\r into a single (and consistant) \n character.

This topic is closed to new replies.

Advertisement