#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    code won 't execute for loop


    the code below has problem. five_oct_char[l] is always blank
    five_oct_char[l] = array_octal[i];

    =======================================
    root@galaxian:~# ./read_an_nic_file
    array_octal = 0010520021040037680072680052600075360074830097760113040105200126940150720136760147280188400288000181 73

    array_octal[] = 0
    five_oct_char =
    array_octal[] = 0
    five_oct_char =
    array_octal[] = 1
    five_oct_char =
    five_oct_char =
    array_octal[] = 0
    five_oct_char =
    array_octal[] = 5

    =======================================
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char eth_mac_address0[18] = "", eth_mac_address1[18] = "";
    char array_octal[255] = "", nic_address[18] = "";
    
    /*******************************************/
    
    read_nic_file()
     {
        FILE *file_pointer, *file_pointer_0, *file_pointer_1, *fopen();
        int string_length = 0;
        char array_octal[255];
    
        unsigned long oct_long;
        int i = 0, j = 0, k = 1, l = 1, decimal_number = 0, zero_begin = 1;
        /* char five_oct_char[] = { '\0', '\0', '\0', '\0', '\0', '\0' }; */
        char five_oct_char[5] = "";
    
        file_pointer = fopen("/root/inter0", "r");
        if((file_pointer = fopen("/root/inter0", "r")) == NULL)
         {
           perror("Couldn't open file. File does not exist.");
           return 1;
         }
    
        fgets(array_octal, 255, file_pointer);
        printf ("array_octal = %s\n", array_octal);
        string_length = strlen(array_octal) -1;
        if (string_length < 1)
          printf("the file is empty");
        else
          if (array_octal[string_length] == '\n')
            array_octal[string_length] = '\0';
    
    
        for (i = 0; array_octal [i] != '\0'; i++)
        {
          printf ("array_octal[] = %c\n", array_octal[i]);
    ---> PROBLEM      five_oct_char[l] = array_octal[i];
          printf ("five_oct_char = %s\n", five_oct_char);
          if (l == 1 && array_octal [i] == '0')
            {
              zero_begin = 1;
              l++;
              /* printf ("l = %d\n", l); */
            }
          if (l > 1 && array_octal [i] == '0')
            {
              zero_begin = 1;
              l++;
            }
          if (l > 1 && array_octal [i] != '0')
            {
              zero_begin = 0;
              five_oct_char[l] = array_octal[i];
              l++;
            }
          if (l == 5)
            {
              printf ("five_oct_char = %s\n", five_oct_char);
              l = 1;
            }
        }
    
        fclose(file_pointer);
     }
    
    /*******************************************/
    
    main()
      {
         read_nic_file();
      }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    -------------------------------------------------
    char five_oct_char[5] = "";
    ---> PROBLEM five_oct_char[l] = array_octal[i];
    -------------------------------------------------
    I think since you are setting the intial value of five_oct_char to "" the string has no value even when u set the five_oct_char[1] = array_octal[i];

    as the string has got terminated at five_oct_char[0];
    Try if u can set that value of five_oct_char[0] = array_octal[i]; and
    do printf("array_octal = %s\n",array_octal);

    lemme know if that works
    Last edited by Raghuraman; July 23rd, 2003 at 03:28 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    I found the bugs


    Thank you for your help Raghuraman .

    The code "char array_octal[255]" was declared in the function read_nic_file() as well as in the global part of the program. When I tried to reference that it gets confused. I just need to remove the code "char array_octal[255]" from the function
    read_nic_file()

IMN logo majestic logo threadwatch logo seochat tools logo