Thread: Beginner with C, need help to understand

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    3

    Beginner with C, need help to understand

    Hello together

    I'am Maurizio and beginner on language C, but on me Linux/Unix envoirments it's realy good, if i can write meny little help features.

    Here i will try to add any Array, and understand Array :-)

    Explain my view.
    1. open the file GoodIP.txt, read, here i have a lot of IP inside
    and this ammount change change every day.

    2. Here the reading IP Addresses will be store to my array "Folder" varible "max"

    3. printout to screen, to see if this ammount of Addresses are on the right way since here.

    Code:
    #include <stdio.h>
    void main()
    {
        // 1. read file GoodIP.txt
        FILE *myFile;
        myFile = fopen("GoodIP.txt", "r");
    
        // 2. read into array
         int score[max];
            for (i = 0; i < max; i++)
    
        {
        // 3. Print GoodIP from Array
            printf("Number is: %d\n\n", max[i]);
        return 0;
        }
    }

    my goal:
    5. open file "bannlist.txt", and delete this here the IPaddresses,
    that are inside in the "GoodIP.txt"

    6. close and save

    7. restart Service "systemctl restart fail2ban"

    this code will be compile with gcc, on Debian.
    Thanks meny time, for reciving any answer.
    Regards
    Mauri

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,418
    Post a few lines of GoodIP.txt

    Post a few lines of bannlist.txt

    > void main()
    main returns an int, not void.

    > myFile = fopen("GoodIP.txt", "r");
    It's far more convenient if you pass the filenames as command line parameters.
    So you could do
    myFile = fopen(argv[1], "r");


    > 6. close and save
    > 7. restart Service "systemctl restart fail2ban"
    You need to make your main return int, so you can do this in your shell script
    Code:
    myprogram GoodIP.txt bannlist.txt
    # check that myprogram was successful, and only do
    # the systemctl restart if it was.
    if [ $? -eq 0 ]; then
        systemctl restart fail2ban
    fi
    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.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    3
    Hello Salem
    First thanks for your fast answer!

    Snip GoodIP
    root@mail:/etc/fail2ban# cat GoodIP
    015.37.246.220
    140.211.11.3
    151.248.219.221
    151.248.219.221
    158.181.117.6
    81.221.120.2
    82.126.76.252
    82.136.87.212
    83.164.148.131
    91.121.119.198
    94.237.32.243
    94.237.32.243
    94.237.32.243
    root@mail:/etc/fail2ban# cat GoodIP | wc -l
    69
    Snip ip.blacklist
    root@mail:/etc/fail2ban# cat ip.blacklist
    98.242.146.160
    98.242.201.128
    98.242.202.130
    98.242.204.67
    99.38.89.33
    99.41.175.38
    99.64.183.238
    99.7.234.226
    99.97.25.108
    root@mail:/etc/fail2ban# cat ip.blacklist | wc -l
    17002

    But from where do the application know, which file this will pick to read?
    Code:
    #include <stdio.h>
    int m
    ain()
    {
        // 1. read file GoodIP.txt
        FILE *myFile;
        myFile = fopen(argv[1], "r");
     
        // 2. read into array
         int score[max];
            for (i = 0; i < max; i++)
     
        {
        // 3. Print GoodIP from Array
            printf("Number is: %d\n\n", max[i]);
        return 0;
        }
        // 4. Service restart fail2ban
            if [ $? -eq 0 ]; then
            systemctl restart fail2ban
        fi
    }
    regards
    Mauri

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,418
    Well you concentrate on just reading the first file.

    Start with
    char lines[100][30];
    and read all the lines in the good file.

    Ignore everything else I wrote for now, as you've completely misunderstood the usage.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    3
    I Need to understand more from this langauge, equal C or C++,
    Here you can write evrything, but understading and implementing on the right way are a other part.

    i see the book "Jumping in to C++" but i don't want Start with "hello world"

    Exist here other ways ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    Quote Originally Posted by mauric
    I Need to understand more from this langauge, equal C or C++,
    It is true that standard C++ makes reference to standard C, but in the end C and C++ are two different languages, so you need to be clear whether you want to learn C, or learn C++, or learn both C and C++ (but that is probably better done separately).

    Quote Originally Posted by mauric
    i see the book "Jumping in to C++" but i don't want Start with "hello world"

    Exist here other ways ?
    I don't see what's wrong with starting with a "hello world" program: it is a way to outline the structure of a very simple program in pretty much any programming language, and you get past that pretty quickly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-16-2016, 04:14 AM
  2. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  3. Replies: 6
    Last Post: 03-18-2012, 10:11 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  5. Help me understand this
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2002, 07:58 AM

Tags for this Thread