Help using fgets()

This is a discussion on Help using fgets() within the C Programming forums, part of the General Programming Boards category; My friend is having this problem : Hi , Im having trouble with my code. Can you please look at ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Help using fgets()

    My friend is having this problem :

    Hi , Im having trouble with my code. Can you please look at it and show me ways of using fgets instead of fscanf as I get problems with it .

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int exp_time;
    float aperture_value;
    
    
           fprintf(output,"Please enter an exposure time: ");  
           fscanf(input,"%d",&exp_time);
    	
    	
    	fprintf(output, "Please Enter an Aperture Value: ");
           fgets(aperture_value,10,stdin);
    	
    if 
    (aperture_value !=(1.2||1.4||1.8||2||2.8||4||5.6||8||11||16||22||32))
    {
    	fprintf(output, "The value entered was incorrect.  The accepted values are :\n f1.2, f1.4,  f1.8, f2, f2.8, f4, f5.6, f8, f11, f16, f22, f32.\n\nProgram Closed.\n");
    }
    else{
    (fprintf(output, "Aperture Value accepted."));
    }
    }
    I tried using fgets as shown above in my code, but it didn't work . Thanks , much appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fgets reads a string
    to read float - use fscanf with the matching format
    try not to mix fscanf and fgets in one input stream
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Well the reason is that fgets does not have the syntax you used.

    Its char *fgets(char *Buffer, int num, FILE *fpointer);

    This means that fgets has 3 arguments, the first is an array or buffer say it as you want of characters that you have to create before passing it in fgets().

    eg. char Buffer[BUF_SZ] = ""; or char Buffer[10] ; or dynamically
    char *Buffer = malloc(10); //Remember to free it in the end.

    The second argument is the number of characters that fgets will read,
    in case of using static declaration of Buffer you can use the sizeof(Buffer) but if you have malloced the Buffer you cant. You have to write down the exact amount.

    The third argument is the file pointer that corresponds to the file from which fgets will try to read the data and store it in your buffer.

    So a final example is:
    char Buffer[256] = "";

    fgets(Buffer, sizeof(Buffer), stdin);

    Now it will prompt you to enter the line and when you press enter whatever you write will be stored in Buffer.
    Now if you want just to take some numbers from the Buffer you must use either atoi, either sscanf or whatever function does the job.

    Also fgets has return value but i think you dont need that this time, try to understand the basic part of fgets.

    Hope i helped.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Your "other" friend is much more advanced than you.
    fscanf() not asking for input ?

    You're both at the same school / college anyway - close enough.
    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.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Why is it you all use this tech

    Code:
    FILE * output = stdout;
    FILE * input = stdin;
    
    ....
    ....
    ....
    
    fprintf(output,"Please enter an exposure time: ");  
    fscanf(input,"%d",&exp_time);
    	
    fprintf(output, "Please Enter an Aperture Value: ");
    fgets(aperture_value,10,stdin);
    When you have standard printf fnction to output values which is by default pointing to stdout. Is this what you teacher thought you?

    ssharish

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Quote Originally Posted by Salem View Post
    Your "other" friend is much more advanced than you.
    fscanf() not asking for input ?

    You're both at the same school / college anyway - close enough.
    There seems to be a couple of people from our programming class on these boards.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Quote Originally Posted by ssharish2005 View Post
    Why is it you all use this tech

    Code:
    FILE * output = stdout;
    FILE * input = stdin;
    
    ....
    ....
    ....
    
    fprintf(output,"Please enter an exposure time: ");  
    fscanf(input,"%d",&exp_time);
    	
    fprintf(output, "Please Enter an Aperture Value: ");
    fgets(aperture_value,10,stdin);
    When you have standard printf fnction to output values which is by default pointing to stdout. Is this what you teacher thought you?

    ssharish
    Yes , he insists we use frpintf and fscanf . He said that it will be important in future

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I presume this is so that it's easier to use arguments for input/output later on.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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