Thread: First C program.. please help

  1. #1
    dieselboy
    Guest

    Unhappy First C program.. please help

    Hello all,

    I am truely a beginner to C programming but have written alot of shell code and perl. I am trying to write a small bit of code in C that will do the following:

    Take input from command line such as 192.168.10 and create a file with all the available addresses in that space.. in other words print 192.168.10.1 through 255 into a file. I have started the code but I am not sure I am on the right track.. can someone comment on this:


    #include <stdio.h>
    #define START 1
    #define END 255
    #define STEP 1

    main()

    {
    double addr;

    addr = getchar();
    range = "addr.START";
    printf("range"\n);

    I think this exercise will better help me understand the context of C and move forward with programming..

    ANy help?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi,

    Just a short comment: If you want read in data from the command line (as a program argument), then you must define your Main func. as follows:

    int main (int argc, char **argv) {

    .... the rest of the code...


    return 1;

    }

    argv is an array of strings (it stores all the info). argc is the number of elements in argv.

    These are standard names. If they're not defined then your program will be unable to read command line options.

    Also check out the getopt(), getopt_long() and getopt_long_only() functions (at least getopt() is available in the standard C library, but in GNU's version you'll find all of them.). These are helpful, if there are many command line opts to read...etc.)

    And last but not least: if you want to fetch the data from stdin, I would recommend scanf() instead of getchar(). It is much more robust.

    Thats's it. Good luck! -
    Best Regards,

    Bill

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Just one more thing:

    Why do you use #define? Although, it is valid, you should use local variables instead whenever it's possible.

    >> addr = getchar();

    This is invalid. You have to declare addr first like this.

    char addr;

    Also, getchar will read only one character at a time! This not suitable for reading in strings. Use scanf() or fscanf() or sscanf()

    If you want to create a file, then you'll need stream manipulation functions as well (such as fopen(), freopen()..etc.)

    I suggest you to download the documentation of GNU C Library. This is a help file which contains detailed info about all the standard functions, data types, macros, header files and lot of other stuff.

    You can get it from this site:

    http://www.bloodshed.net/dev/doc/bin/libc.zip

    An online version is also available:

    http://www.gnu.org/manual/glibc-2.2..../libc_toc.html

    Well, that's all for now. - Be good! -











    range = "addr.START";
    Last edited by Bill 101; 11-06-2002 at 02:37 PM.
    Best Regards,

    Bill

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Also, getchar will read only one character at a time! This not suitable for reading in strings. Use scanf() or fscanf() or scanf()
    Or perhaps you should investigate fgets(), if you don't want to build in functions that allow buffer overflows.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi!

    Yes, fgets is better, however I'm not sure what does he mean by "taking input from the command line". If he wants to type in something then scanf() is good, else of course fgets is the right choice.

    getline() is another alternative.

    Bye!
    Best Regards,

    Bill

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Bill 101
    Yes, fgets is better, however I'm not sure what does he mean by "taking input from the command line". If he wants to type in something then scanf() is good, else of course fgets is the right choice.

    getline() is another alternative.
    fgets() will get from the keyboard as well, and is safer than scanf().

    getline() ? That isn't a standard function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >>> addr = getchar();
    >This is invalid. You have to declare addr first like this.
    >char addr;

    getchar() returns an int.

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc , char *argv[] )
    {
        int cnt_main;
        FILE *fPtr;
    
        if ( argc == 1 )
            printf( "Usage : IP addres e.g <190.20.0>" );
        else{
    
           if ( (fPtr = fopen( "network.txt" ,"w" )) != NULL ){
              for ( cnt_main = 0; cnt_main <= 255; cnt_main++ )
                  fprintf(fPtr, "%s.%d\n", argv[1], cnt_main );
    
           fclose( fPtr );
           }
           else
              printf( "Error could not create file" );
        }
    
            return 0;
    }
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  9. #9
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by Hammer
    >>Also, getchar will read only one character at a time! This not suitable for reading in strings. .....
    other method ??

    scanf ("%[^\n]", variable);

    this can help you input the string that you have entered.
    this code could help you when the user enter the input, user enter the name and hit enter. when the io detect the "enter (or '\n') it will save the whole words before the \n into the variable....

    eg :

    | enter your name > beely name of the words

    then the variable is saved as --> beely name of the words.


    === compare this !

    scanf ("%s",variable);

    then the variable contains : beely
    that mean, when it detect a space after a word. the others will discard saving into the variable.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by beely
    other method ??

    scanf ("%[^\n]", variable);

    <snip>
    As I already posted, use fgets() to get a line of data.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM