Clearing an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    Clearing an Array

    Hi all,

    What i am trying to accomplish is take in a list of IP addresses from a text file (each Ip is on a new line).

    I got my program mostly working except when an IP address is longer than the last one it keeps the extra numbers beacuse im having trouble clearing the IP.


    Code:
    
        fd = open("/usr/nodes.ini", O_RDWR);
        len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
        
        while(i < 5000)
        {
          
          if(buffer[i] == 10){ // check to see if it is a new line.
          n=0; // Reset the Ip loop.
          
          printf("Node: %s\n", node);
    
          // Clear Node;
          //int y=0;
          //while(y < 16){ node[y] = "X"; y++; }
          Tried this but nothing would write to node after.      
    
          char node[16]; // Tried just to reintialize node but no dice.
          memset(node, 0, 16);    // Saw this method on the forums but no dice.
          
           i++;
          }   
         else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
         
    
        printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
           n++;
          i++;
        }
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    line 7: buffer[i] == '\n' is more readable.
    line 14: node[y] = "X" is wrong. "X" is a null-terminated string. 'X' is a char. Use 'X'.
    line 17: char node[16]; Ahh! You're redeclaring node!
    line 18: See memset reference.

    You'll go a long way by finding out about the functions you're using before using them from an authoritative reference like Stroustrup, manpages or cplusplus.com.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There are a lot of errors here.

      Code:
      fd = open("/usr/nodes.ini", O_RDWR);
      
      I don't know what you mean here. You don't know the amount of data in the file. The meemory you allocate is for your buffer:
         
          buffer = (char*)malloc(5000* sizeof(char));   
          VVVVVVVVVVVVVVVVVVVVVVVV
          len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
      
      Where is i initialized to 0??
          VVVVVVVVVVVVVV    
          while(i < 5000)
          {
      
      Thiss leads me to think your trying to read a text file in as binary.
      That's the hard way.
            VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVv      
            if(buffer[i] == 10){ // check to see if it is a new line.
            n=0; // Reset the Ip loop.
            
            printf("Node: %s\n", node);
       
            // Clear Node;
            //int y=0;
      
      If node is a char array, then node[y] is a char. "X" is a string. So what you are trying to do here is assign the address of "X" to a char. Won't compile.
            VVVVVVVVVVVVVVVVVVVVVVVVVVVV
            //while(y < 16){ node[y] = "X"; y++; }
            Tried this but nothing would write to node after.      
      
      This won't compile in C where variables have to be defined at the beginning of a function. Therefore, you are writing in C++ or the code below gaved you errors.
      In any case, this is a NEW array. All you are doing here is fiddling with the new array. Where is the original one?
            VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV 
            char node[16]; // Tried just to reintialize node but no dice.
      
      Do not use functions just because you saw them somewhere. You have to know exactly what it is you want to do.
                                               VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
            memset(node, 0, 16);    // Saw this method on the forums but no dice.
            
             i++;
            }   
           else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
           
       
          printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
             n++;
            i++;
          }

      Comment

      • edwardrsmith
        New Member
        • Feb 2008
        • 62

        #4
        The above replys are pretty much correct.

        First of all I want to agree with arnaudk when he said that memset was the best way to clear out the array, though you are not using it correctly.

        The only thing I do want to correct is when weaknessforcats said that if it is c you have to declare all the variables at the beginning of the function. Although this is great programming style not all c standards and compilers care.

        Edward

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by edwardrsmoth
          The only thing I do want to correct is when weaknessforcats said that if it is c you have to declare all the variables at the beginning of the function. Although this is great programming style not all c standards and compilers care.
          Exactly what C standards are yout referring to? I was under the impression there was ANS C, which is the C standard. Are you referring to C99? Some vendor goodie?

          Pleaese provide a quotable source so I can verify what you say.

          Visual Studio.NET 2008 won't compile this:

          Code:
          void funct()
          {
          	
              if ( 1 == 2)
          	{
          
          	}
          	int a = 3;
          }
          This is OK in C++ but this is C and not C++.

          Comment

          • edwardrsmith
            New Member
            • Feb 2008
            • 62

            #6
            I guess I should have been more specific before. Each compiler is based off of different rules (standards) for how parts of a program must be laid out. I have never programmed in visual studio but it apears to be compiling using either C89 (also called ANSI C), C90, C94 or C95. All of these require the variables to be declared at the beginning of the function. The newer standard, C99, does not have this limitation. Since I do my programming in Linux I use the gcc compiler which used C99 by defalut. This website briefly explains the standards and should provide insight into the reason the code will not compile under visual studio. This link talks about the difference some more and provides some examples (it is from a thread on this forum from a while ago but it still is a good example.)

            Edward
            Last edited by edwardrsmith; Aug 8 '08, 04:59 PM. Reason: forgot a word.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              That probably explains it: Will the real C please stand up.

              Visual Studio. NET is not a language. It is a tool. When you code C using this product, it compiles using ANS C. No doubt what you call C86. Those other C's are not standard C's. Like C++. ANS C++ is C++ 1998. There is a C++2003 and a C++2005 but those are not standard.

              Neither are the vendor features standard, like all that Microsoft stuff that only works on Windows using Microsoft's compiler.

              When I make replies to posts, I try to give the ANS answer.

              Like arrays:
              Code:
              int X;
              cin >> X;
              int array[X];         //OK in g++
                                       //NOT OK using g++ -pedantic
                                       //NOT OK in ANS C++
                                       //NOT OK using Microsoft's compiler.

              Comment

              • edwardrsmith
                New Member
                • Feb 2008
                • 62

                #8
                I guess it would then all depend on your definition of standard. C89 was created by ANSI in 1989 (hence the 89). It was then ratified by ISO/IEC in 1990 (giving rise to C90). After that there where some revisions to the ISO/IEC standard around 1995-1996 fixing some bugs. There was also a new standard developed by ISO/IEC in 1994-1995 (C94, C95). Finally in 1999 a new standard was born (C99) which was once again created by ISO/IEC. This would be the current international standard and seems to be the current standard used in the United States as well (seeing that ANSI has not made any amendments to its standard and seems to accept all ISO/IEC standards).
                There is a very good overview of this here.

                The actual standard name and number is ISO 9899

                Edward

                NOTE: The above post all refers to C, not C++.
                Last edited by edwardrsmith; Aug 8 '08, 06:16 PM. Reason: Added NOTE.

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  The problem with that is despite the existence of C99, nobody actually completely supports it. I think gcc is closest, but unless you use -c99, it's not complete, and might not even be then (I think). C89/90 is still the most widely supported, and there are also a fair number of people that show up here with questions using mid-90s compilers that definitely don't support C99.

                  Comment

                  • edwardrsmith
                    New Member
                    • Feb 2008
                    • 62

                    #10
                    I completely agree with that and further believe that declaring all variables at the top of a function is an excellent programming habit as it is easier to read, follow and debug not to mention the added advantages when programming with a team.

                    At the same time I believe that it is important to be aware that just because a certain syntax does not work for your computer/compiler/OS does not mean that the code is necessarily wrong or broken, though it is important to be aware of these issues because they may cause problems if you change compilers.

                    Edward

                    Comment

                    Working...