![]() |
| | #16 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
So don't keep trying to obfuscate the issue, brewbuck. Once the OP is comfortable with "normative practices", he/she will be free to get kooky and clever. But no gun jumping!
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #17 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Quote:
I do not see why giving an accurate description of the behavior of a certain function is "obfuscation."
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot | |
| brewbuck is offline | |
| | #18 | |
| Registered User Join Date: Sep 2006
Posts: 2,517
| Quote:
'\0' will automatically be added by fgets *IF* the buffer receiving it, has enough room for it. (It should). Same is true for the newline char, if it is present in the line of text. You may get rid of the newline char very easily, by using the line of code I show in my program - which itCbitC also mentioned. You have a working example program. Why don't you run it, and study it? You're wasting time asking questions which that program already shows the answer to. | |
| Adak is offline | |
| | #19 | ||
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| Quote:
Quote:
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) | ||
| Cactus_Hugger is offline | |
| | #20 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| I forget myself. Probably because strncpy() does behave that way, while fgets() does something sane. Yay for consistency (not)
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #21 |
| Registered User Join Date: Sep 2006
Posts: 2,517
| |
| Adak is offline | |
| | #22 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Code: while (fgets(rptline, sizeof(rptline), rptFile))
{
char *newline = strchr(rptline, '\n');
if (newline)
{
*newline = '\0';
}
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is offline | |
| | #23 |
| Registered User Join Date: Sep 2006
Posts: 2,517
| That's a really good point. On a last line of the file, where the newline may not be present at the end of the line, it could save you from an inaccurate result, as well. filename.txt (without a newline), could then be processed as this wrong name: filename.tx Which would obviously fail. |
| Adak is offline | |
| | #24 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Using fgets() to read directly into the destination string is troublesome to begin with. We're arguing about which method is correct for removing the \n on the assumption that it might not be there. If it isn't there, then the next time around is going to be a broken line as well (most likely, the tail-end of the current long line). In some rare (or not so rare) cases, you might treat this half of a line as being valid data, and attempt to process it. char buff[BUFSIZ]; Using this buffer is good for reading any text file you would want to edit manually with a text editor. - read a line using fgets() - remove the newline if present, perhaps even do some error handling if there isn't one - validate the buffer (in this case, is it the right length with .txt on the end) - use the validated string for whatever. - rinse and repeat Here's a test! myprog.exe < myprog.exe The error checking might grumble a bit, but it shouldn't do anything stupid, and it definitely shouldn't crash.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked List Not Saving Value as Int | bar338 | C Programming | 4 | 05-04-2009 07:53 PM |
| Help Debugging my AVL tree program. | Nextstopearth | C Programming | 2 | 04-04-2009 01:48 AM |
| Compiling 3rd party code problem me too | siavoshkc | C Programming | 1 | 09-12-2007 05:55 AM |
| Linked List of Linked list of linked list : problem with a condtion | gemini_shooter | C Programming | 6 | 03-02-2005 02:45 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |