Thread: fgets problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    fgets problem

    I try to store a sentences in key.programme using fgets but it never work. It just scan the first word just like a scanf. It dont scan a whitespace. Please help. TY
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct record{
        char* name;
    	char* programme;
    	int id;
    	float cgpa;
    };
    
    typedef struct record record;
    
    int main(){
    
    	record key;
    
    	printf("Programme:");
    	fflush(stdin);
    	fgets(key.programme, sizeof(key.programme),stdin);
    	printf("Input received: %s", key.programme);
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You need to allocate memory for the string ex. in your struct make it an array of 255

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    what if i dont want to. I just wanted to use char* ? is it legal?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure it's legal. C doesn't care if you use valid pointers or not. Your application will care, but C doesn't.


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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct record{
        char name[20];
    	char programme[20];
    	int id;
    	float cgpa;
    };
    
    typedef struct record record;
    
    int main(){
    
    	record key[1];
    
    	printf("Programme:");
    	fflush(stdin);
    	fgets(key[0].programme, sizeof(key[0].programme),stdin);
    	printf("Input received: %s", key[0].programme);
        key[1].programme = "hello world";
    	printf("Input received: %s", key[1].programme);
    }
    I try to assign a sentences in it but it always say it's type is unmatch. What should I do?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Key[1].programme doesnt exist, you can only index key[0], also line 21 you can not directly assign a string like that, you must use strcpy in the string.h library

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Still no hope. incompatible types when assigning to type 'char[20]' from type 'char *'


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct record{
        char name[20];
    	char programme[20];
    	int id;
    	float cgpa;
    };
    
    typedef struct record record;
    
    int main(){
    
    	record key[1];
    
        key[0].programme = "hello world";
    	printf("Input received: %s", key[0].programme);
    }

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Line 17 you can not assign directly try including string.h library and doing something along these lines

    strcpy(key[0].programme,"hello world");

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    oh it works. Why we can't directly assign "Hello World" in it? Char* seems working well on this.

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    C strings are just arrays of characters, you can only directly assign like that if it is initialization for example you declare char* name="Joe Smith"; After that you no longer can tamper with it directly. Also do not forget that you can't initialize any members in a struct declaration. You have to initialize the struct when you create an instance of the struct.
    Last edited by camel-man; 03-02-2012 at 10:44 PM.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    oo ok thanks. get it. lesson learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets
    By mrbains in forum C Programming
    Replies: 2
    Last Post: 01-30-2011, 07:42 AM
  2. fgets() Problem
    By kihina in forum C Programming
    Replies: 3
    Last Post: 02-08-2010, 11:03 AM
  3. fgets problem
    By belhifet in forum C Programming
    Replies: 14
    Last Post: 11-29-2006, 05:07 PM
  4. fgets problem
    By ober in forum C Programming
    Replies: 5
    Last Post: 04-21-2002, 06:47 PM
  5. fgets problem
    By gambitmj in forum C Programming
    Replies: 5
    Last Post: 02-26-2002, 08:55 AM