Thread: annoying file i/o

  1. #16
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Quote Originally Posted by Salem
    Still lots of bugs in there
    > while ((buf[i++] = fgetc(stream)) != EOF
    You're comparing a char with EOF, not an int with EOF

    > while (buf[i--] != '\n')
    If you don't read a newline, then this marches off back towards the beginning of memory is a horrible buffer underrun.
    Besides, it also means you lose a whole bunch of file data.
    int *fchar(char buf[], FILE *stream, size_t size)
    	int i = 0;
    	while (i < size - 1) 
    		buf[i++] = fgetc(stream);
    	while (buf[i--] != '\n' && i > 0)
    	buf[i+1] = '\0';
    	return 0;
    >#include "/Source/str.h"
    Adding unistd.h made otherwise portable code less portable. What on earth is this for?
    unistd.h is for 'exit()', I also forgot to 'close()' the file.

    I'm just playing with C.

    Last edited by loopy; 06-13-2004 at 05:55 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1

    Simultaneous Multiple Processes

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    *shakes head*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    exit() is in stdlib.h not unistd.h close is in unistd.h, but I think you meant fclose which of course is in stdio.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM