Thread: fgets, fputs, and malloc.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    fgets, fputs, and malloc.

    Hmm, I was trying to test the stdin and stdout you talked about and here's what I got:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	int x[8], i, j, *p;
    	p = malloc(sizeof(int) * 8);
    	printf("Enter at least 8 integers (press q when done)");
    	while( getchar() != 'q')
    	{
    	for(i = 0; i < strlen(x); i++) fgets(x[i], 15, stdin);
    	for(j = 0; j < strlen(x); j++) fputs(x[j], 15, stdout);
    	}
    
    	free(p);
    	
    	getchar();
    	return 0;
    }
    There are a lot of errors:
    Code:
    untitled.c:12: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
    untitled.c:12: warning: passing argument 1 of ‘fgets’ makes pointer from integer without a cast
    untitled.c:13: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
    untitled.c:13: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast
    untitled.c:13: warning: passing argument 2 of ‘fputs’ makes pointer from integer without a cast
    untitled.c:13: error: too many arguments to function ‘fputs’
    What am I doing wrong?

    and btw,
    for example,
    if I do this:
    Code:
    int x[], *p;
    p = malloc(sizeof(int)); // what do I put here? * what?
    I mean, what is the malloc parameter if I don't know x's size?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, no, no. Don't use fgets to read int non-char buffers.
    It's used to read strings. If you don't need to read a string, you can use scanf, or fscanf.
    int x[] isn't valid. You must specify a size.
    As for your p - well, that's kinda up to you and what you want to use it for. Just make sure you have enough space allocated when messing around writing data to it.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fgets(), strlen() and friends take pointers to strings (not integers -- x is delcared as an array of integers). Convert the input from a string to integer then put it in x[n] no need for strlen()...

    > int x[], *p;
    Is invalid, you haven't specified x's size. malloc will allocate only the memory you ask for, you should know how much you need (if not, resize with realloc()).

    [edit]Pretty much what post 2 says [/edit]

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Thanks but the program still doesn't work:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char x[8], *p;
    	int i, j;
    	
    	p = malloc(sizeof(char) * 8);
    	printf("Enter your name");
    	for(i = 0; i < strlen(x); i++) fgets(x[i], 15, stdin);
    	for(j = 0; j < strlen(x); j++) fputs(x[j], 15, stdout);
    
    	free(p);
    	
    	getchar();
    	return 0;
    }
    Last edited by eXeCuTeR; 11-30-2007 at 04:31 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And why do I see so many problems with that code?
    Why do you need p in the first place? You're not using it anywhere. Secondly, you're allocating 8 INTs of memory for it while p is a CHAR pointer.
    x contains 8 elements, and you're trying to read 15 bytes. Very bad. Buffer overrun right there.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also since x is uninitialized, you'll probably overrun the buffer with that strlen(x).

    * get rid of p and the dynamic memory allocation
    * redesign your program

    In that order.

    Remember, fgets() reads a string -- not a single character. You'd only need to call it once to get the user's name.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strlen(x) won't work until there is a C string in x. Your current code doesn't set x to a string at all.

    You then try to read 15 bytes into one element of a 8 byte char array - that definitely won't work. Try enabling some warnigns in your compiler - it should definitely warn you that you are passing a single char instead of a char pointer to fgets(). And fputs only takes two parameters, the string and the FILE that you want to write to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char x[8], *p;
    	int i;
    	
    	p = malloc(sizeof(char) * 8);
    	
    	printf("Enter your name");
    	fgets(x, 8, stdin);
    	
    	for(i = 0; i < strlen(x); i++)
    		fputs(x[i], strlen(x), stdout);
    
    	free(p);
    	
    	getchar();
    	return 0;
    }
    I corrected anything you told me (well, except from the memory allocating which doesn't appear to be a problem) and it still doesn't work!

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're not actually using p!? So why bother.

    Again, fputs() takes a pointer to a char array of strings, it also only takes 2 args. x[i] is a single char, stop guessing.

    Code:
    fgets(x, sizeof(x), stdin);
    
    fputs(x, stdout);

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fputs writes a pointer to a stream, NOT a char. x[i] = char x = char* (pointer)
    And p is still superfluous - it's not used by anything. You only allocate memory and release it - totally unnecessary,
    Actually, enable warnings on your darn compiler, because char != char*. And writing 8 bytes from a single char? Oh, the horror!

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    why fputs(x, stdout);?
    it gets 3 parameters and not 2, how is this possible?

    and why is my memory allocating is bad?
    I mean, what if the user's name is Chris so not all the memory is used, only 5 byte of it and not 8.

    and why fgets(x, sizeof(x), stdin) - fgets(x, 8, stdin) is good either, isn't it?

    and how do I make a warning is the input isn't a character?
    Last edited by eXeCuTeR; 11-30-2007 at 05:32 AM.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    perhaps he should be usuing fputc()

    ssharish

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eXeCuTeR View Post
    why fputs(x, stdout);?
    it gets 3 parameters and not 2, how is this possible?

    and why is my memory allocating is bad?
    I mean, what if the user's name is Chris so not all the memory is used, only 5 byte of it and not 8.

    and why fgets(x, sizeof(x), stdin) - fgets(x, 8, stdin) is good either, isn't it?

    and how do I make a warning is the input isn't a character?
    Look up fputs, e.g.:
    http://www.hmug.org/man/3/fputs.php

    No, it doesn't take three parameters, it figures strlen all by itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fputs() only takes 2 parameters, http://www.cplusplus.com/reference/c...dio/fputs.html

    Remember we're talking about fputs() not fgets(). Your memory allocating isn't bad as such, another word is useless. Firstly, You're not using the memory you allocated nor do you check if malloc() failed (ie, returned a NULL pointer) or not.

    > fgets(x, sizeof(x), stdin) - fgets(x, 8, stdin) is good either, isn't it?

    Yeah until you change the size of x, to read in 5 or 16 characters and wonder why your program crashes or it still only reads 8 when x is 16 bytes long.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by eXeCuTeR View Post
    why fputs(x, stdout);?
    it gets 3 parameters and not 2, how is this possible?
    Because fputs doesn't need to read into a buffer and therefore does not need to know the buffer length. It reads the string and writes all the characters in the string until it encounters the NUL char.

    Quote Originally Posted by eXeCuTeR View Post
    and why is my memory allocating is bad?
    I mean, what if the user's name is Chris so not all the memory is used, only 5 byte of it and not 8.
    Because you're not using it for anything. All you're using the local variable!

    Quote Originally Posted by eXeCuTeR View Post
    and why fgets(x, sizeof(x), stdin) - fgets(x, 8, stdin) is good either, isn't it?

    and how do I make a warning is the input isn't a character?
    Err, what is supposed to do?
    For warnings, check compiler settings. You could tell what compiler you're using so someone can help.

    Quote Originally Posted by ssharish2005 View Post
    perhaps he should be usuing fputc()

    ssharish
    What's the point since obviously the entire string is written?

Popular pages Recent additions subscribe to a feed