By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,543 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,543 IT Pros & Developers. It's quick & easy.

Clearing an Array

P: 158
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.

Expand|Select|Wrap|Line Numbers
  3.     fd = open("/usr/nodes.ini", O_RDWR);
  4.     len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
  6.     while(i < 5000)
  7.     {
  9.       if(buffer[i] == 10){ // check to see if it is a new line.
  10.       n=0; // Reset the Ip loop.
  12.       printf("Node: %s\n", node);
  14.       // Clear Node;
  15.       //int y=0;
  16.       //while(y < 16){ node[y] = "X"; y++; }
  17.       Tried this but nothing would write to node after.      
  19.       char node[16]; // Tried just to reintialize node but no dice.
  20.       memset(node, 0, 16);    // Saw this method on the forums but no dice.
  22.        i++;
  23.       }   
  24.      else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
  27.     printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
  28.        n++;
  29.       i++;
  30.     }
Aug 6 '08 #1
Share this Question
Share on Google+
9 Replies

P: 424
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
Aug 6 '08 #2

Expert Mod 5K+
P: 9,197
There are a lot of errors here.

Expand|Select|Wrap|Line Numbers
  1. fd = open("/usr/nodes.ini", O_RDWR);
  3. 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:
  5.     buffer = (char*)malloc(5000* sizeof(char));   
  7.     len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
  9. Where is i initialized to 0??
  10.     VVVVVVVVVVVVVV    
  11.     while(i < 5000)
  12.     {
  14. Thiss leads me to think your trying to read a text file in as binary.
  15. That's the hard way.
  17.       if(buffer[i] == 10){ // check to see if it is a new line.
  18.       n=0; // Reset the Ip loop.
  20.       printf("Node: %s\n", node);
  22.       // Clear Node;
  23.       //int y=0;
  25. 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.
  27.       //while(y < 16){ node[y] = "X"; y++; }
  28.       Tried this but nothing would write to node after.      
  30. 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.
  31. In any case, this is a NEW array. All you are doing here is fiddling with the new array. Where is the original one?
  33.       char node[16]; // Tried just to reintialize node but no dice.
  35. Do not use functions just because you saw them somewhere. You have to know exactly what it is you want to do.
  36.                                          VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  37.       memset(node, 0, 16);    // Saw this method on the forums but no dice.
  39.        i++;
  40.       }   
  41.      else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
  44.     printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
  45.        n++;
  46.       i++;
  47.     }
Aug 6 '08 #3

P: 62
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.

Aug 7 '08 #4

Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. void funct()
  2. {
  4.     if ( 1 == 2)
  5.     {
  7.     }
  8.     int a = 3;
  9. }
This is OK in C++ but this is C and not C++.
Aug 8 '08 #5

P: 62
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.)

Aug 8 '08 #6

Expert Mod 5K+
P: 9,197
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:
Expand|Select|Wrap|Line Numbers
  1. int X;
  2. cin >> X;
  3. int array[X];         //OK in g++
  4.                          //NOT OK using g++ -pedantic
  5.                          //NOT OK in ANS C++
  6.                          //NOT OK using Microsoft's compiler.
Aug 8 '08 #7

P: 62
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


NOTE: The above post all refers to C, not C++.
Aug 8 '08 #8

Expert 100+
P: 849
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.
Aug 8 '08 #9

P: 62
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.

Aug 8 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.