Thread: Need Help!!!!!!!!!

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    %d is for ints. %c is for chars. If you output a char with a %d, you will get a number. If you read in a %d into an int and output it with a %d, you will get a number. If you input a %c and output a %c, you will get a character. If you read in with fgetc and output with %c, you will get a character. If you read in with getc and output with %c, you will get a character.

  2. #17
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by leiming View Post
    Maybe it's better to use fscanf than getc here.

    Code:
    	while( !feof(fp) && fscanf(fp, "%d\n", &c) != 0){
    		printf( "%d\n", c);
    	}
    Why do I always get put off when I see the use of the function feof() as a condition to stop a loop?

  3. #18
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by Aia View Post
    Why do I always get put off when I see the use of the function feof() as a condition to stop a loop?
    I think the two conditions work together will not produce an error.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But I believe the example should work if there are only integers on the lines.
    Of course, it will also read the newlines and probably choke when reading it, so the code isn't perfect.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    but dosent this:
    Code:
    fscanf(fp, "%d\n", &c)
    Specify the input format as an integer followed by a new line?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dunno. I have no freakin' idea how fscanf works. That's why I hate the thing.
    But then there's really nothing to complain about, is there?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well the scanf part of it reminds me of that horrible method of getting user input, but that dosent necessarily mean that fscanf is bad. After all parsing text from a file is generally quite a picky thing anyway.

    Still I dont know if it would be as good for error handling as reading in strings, then using srttol to convert them, but that would require a load of extra code.
    Last edited by mike_g; 02-01-2008 at 04:44 AM.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mike_g View Post
    but dosent this:
    Code:
    fscanf(fp, "%d\n", &c)
    Specify the input format as an integer followed by a new line?
    Not quite:
    Quote Originally Posted by ISO C, 7.19.6.2
    A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.
    So, an integer, then some kind of white-space.

Popular pages Recent additions subscribe to a feed