473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Clearing an Array

158 100+
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
  1.  
  2.  
  3.     fd = open("/usr/nodes.ini", O_RDWR);
  4.     len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
  5.  
  6.     while(i < 5000)
  7.     {
  8.  
  9.       if(buffer[i] == 10){ // check to see if it is a new line.
  10.       n=0; // Reset the Ip loop.
  11.  
  12.       printf("Node: %s\n", node);
  13.  
  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.      
  18.  
  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.
  21.  
  22.        i++;
  23.       }   
  24.      else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
  25.  
  26.  
  27.     printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
  28.        n++;
  29.       i++;
  30.     }
  31.  
  32.  
  33.  
Aug 6 '08 #1
9 2784
arnaudk
424 256MB
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.
Aug 6 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
There are a lot of errors here.

Expand|Select|Wrap|Line Numbers
  1. fd = open("/usr/nodes.ini", O_RDWR);
  2.  
  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:
  4.  
  5.     buffer = (char*)malloc(5000* sizeof(char));   
  6.     VVVVVVVVVVVVVVVVVVVVVVVV
  7.     len = read(fd, buffer, 5000); // Find out how to do dynamic size would be nice.
  8.  
  9. Where is i initialized to 0??
  10.     VVVVVVVVVVVVVV    
  11.     while(i < 5000)
  12.     {
  13.  
  14. Thiss leads me to think your trying to read a text file in as binary.
  15. That's the hard way.
  16.       VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVv      
  17.       if(buffer[i] == 10){ // check to see if it is a new line.
  18.       n=0; // Reset the Ip loop.
  19.  
  20.       printf("Node: %s\n", node);
  21.  
  22.       // Clear Node;
  23.       //int y=0;
  24.  
  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.
  26.       VVVVVVVVVVVVVVVVVVVVVVVVVVVV
  27.       //while(y < 16){ node[y] = "X"; y++; }
  28.       Tried this but nothing would write to node after.      
  29.  
  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?
  32.       VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV 
  33.       char node[16]; // Tried just to reintialize node but no dice.
  34.  
  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.
  38.  
  39.        i++;
  40.       }   
  41.      else{node[n] = buffer[i]; } // Its not a new line so add to the node Ip.
  42.  
  43.  
  44.     printf("N=%d | Bufi=%c | Node = %s\n\n",n, buffer[i], node);
  45.        n++;
  46.       i++;
  47.     }
  48.  
Aug 6 '08 #3
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
Aug 7 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
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. {
  3.  
  4.     if ( 1 == 2)
  5.     {
  6.  
  7.     }
  8.     int a = 3;
  9. }
  10.  
This is OK in C++ but this is C and not C++.
Aug 8 '08 #5
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
Aug 8 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
  7.  
Aug 8 '08 #7
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++.
Aug 8 '08 #8
Laharl
849 Expert 512MB
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
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
Aug 8 '08 #10

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

Similar topics

3
by: Miki Tebeka | last post by:
Hello All, I'd like to bind <CTRL+L> so that it will clear IDLE's screen (in the interactive prompt). Any pointers/suggetions? Thanks. Miki
4
by: Gary | last post by:
Hi How does one clear an Array? Situation is when I am executing my program everything works fine, but when I try and repeat the program without resetting my program I get this error,...
28
by: Terry Andersen | last post by:
I have an array that I initialize to zero, like: Buffer = {0x00}; How do I in my code reset this array to all zeros ones more? Without running a whole for loop? Best Regards Terry
10
by: Liz - Newbie | last post by:
Does anyone know how to clear arrays? My C# books talk about creating arrays or talk about using Clear or RemoveAt but these methods don't appear to be available for my array. I have an array...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
13
by: Adam Honek | last post by:
Instead of the ZeroMemory API what would one use in VB.NET to clean an array using a custom structure? The .clear isn't a member of it by default. Any suggestions? Thanks, Adam
4
by: timothytoe | last post by:
I can think of several easy ways to clear out an array. The most obvious: arr=; arr.length=0; arr=null; What do most JavaScript programmers expect to see? Since JS is transmitted, do I...
16
by: Ed Bitzer | last post by:
Trying to send groups of email with program using System.Net.Mail. I do not clear MailMessage but repeatedly loop changing only the Bcc entries. Works fine if all addresses are valid. As a simple...
3
by: Martin | last post by:
Is clearing a structure the following way well defined in C89? The structure ACTION contains no floating point or pointer members. Only integral types. My thoughts concern the padding - can and...
4
by: gobblegob | last post by:
I am trying to clear the array STR but it does not clear it. This sub is in a module. Dim STR As Array 'Dim str(6) As String STR =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.