annoying file i/o

This is a discussion on annoying file i/o within the C Programming forums, part of the General Programming Boards category; this has annoyed the hell out of me Code: if (!feof(fp)){ fscanf(fp, "\n\n\n\n\n%s\n%s\n%s\n%s\n", line1, line2, line3, line4); } it keeps ...

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    annoying file i/o

    this has annoyed the hell out of me

    Code:
    if (!feof(fp)){
     fscanf(fp, "\n\n\n\n\n%s\n%s\n%s\n%s\n", line1, line2, line3, line4);
    }
    it keeps on reading the top line.is there a way to get it read down like this that will work.or why isnt this working?
    thanks
    Last edited by Rare177; 06-10-2004 at 10:45 PM. Reason: missed [/CODE]

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you could aways use fgets in a loop and read the lines like normal people...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    well is there a reason why this is not working?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >well is there a reason why this is not working?

    yes

    [edit]Also, you may have missed this:

    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    Last edited by Dave_Sinkula; 06-10-2004 at 11:28 PM. Reason: Added feof link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    ok so does any one have any idea's on what to use , and i have never used fgets before
    all im wanting to do is read 5 lines down into a file.thanks

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just answered your own question. Use fgets. Seriously, what's the point of programming if you don't want to learn anything new or try any different way of doing things.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i mentioned it for a hint to a link :P

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >i mentioned it for a hint to a link

    Perhaps something akin to this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I answered, with a link. But doesn't your compiler have any documentation? It's a standard library function. On MSVC, punch F1 and search. On *nix, type "man fgets" or "man 3 fgets".

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    I wrote my own function:

    Code:
    char *fget(char buf[], FILE *stream, size_t size) 
    {
    	int i = 0;
    	while ((buf[i++] = fgetc(stream)) != EOF && i != size) 
    		;;
    	buf[i] = '\0';
    	return buf;
    }
    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
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    buf[i] = '\0';
    I'll assume that when you pass size to the function, it includes room for the null terminator right? Otherwise you're running past your array when this happens:
    Code:
    while ((buf[i++] = fgetc(stream)) != EOF && i != size)
    But basicly, yours isn't reading a line, which is what the origional poster wanted. Yours just reads a number of bytes. Also, what's the point of returning the buffer, when you're passing and modifying the same buffer?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Quote Originally Posted by quzah
    Code:
    buf[i] = '\0';
    I'll assume that when you pass size to the function, it includes room for the null terminator right? Otherwise you're running past your array when this happens:
    Code:
    while ((buf[i++] = fgetc(stream)) != EOF && i != size)


    [b]But basicly, yours isn't reading a line, which is what the origional poster wanted. Yours just reads a number of bytes. Also, what's the point of returning the buffer, when you're passing and modifying the same buffer?[b]

    Quzah.
    Yes, always! : )

    I'm still learning! : )
    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
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  13. #13
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by loopy
    I wrote my own function:
    Code:
    char *fget(char buf[], FILE *stream, size_t size) 
    {
    	int i = 0;
    	while ((buf[i++] = fgetc(stream)) != EOF && i != size) 
    		;;
    	buf[i] = '\0';
    	return buf;
    }
    It looks fine, but it doesn't take care of the '\n'. It's not 'line oriented' like fgets() is.

    Also, why ';;' ? I think '{}' is what you actually meant.

    In fact, the code is buggy. The output expression should be ... && i < size - 1. If not, you'll stick the '\0' at buf[size] that is out of the bounds. It's an undefined behaviour.
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Here is the FINAL version. ; )

    Code:
    int *fchar(char buf[], FILE *stream, size_t size)
    {
    	int i = 0;
    	while ((buf[i++] = fgetc(stream)) != EOF && i < size -1)
    		;;
    	while (buf[i--] != '\n')
    		;;
    	buf[i] = '\0';
    	return 0;
    }
    Called:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <unistd.h>
    #include "/Source/str.h"
    
    int main(int *argc, char *argv[])
    {
    	char buf[1024];
    	int i = 0;
    	FILE *file;
    	if (!argv[1]) {
    		fprintf(stdout, "Usage: File as first arg...\n");
    		return 1;
    	}	
    	if ((file = fopen(argv[1], "r")) == NULL) {
    		fprintf(stderr, "fopen: Failed.\n");
    		exit(1);
    	}
    	fchar(buf, file, sizeof(buf));
    	fprintf(stdout, "%s\n", buf);
    	return 0;
    }
    In case anyone wants to play with it. ; )

    {edit}

    Changed it, I didn't notice anything strange.
    Last edited by loopy; 06-13-2004 at 04:56 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
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    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.

    > #include "/Source/str.h"
    Adding unistd.h made otherwise portable code less portable. What on earth is this for?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
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, 08:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01: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, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21