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. -
-
-
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++;
-
}
-
-
-
9 2784
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.
There are a lot of errors here. -
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++;
-
}
-
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
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: -
void funct()
-
{
-
-
if ( 1 == 2)
-
{
-
-
}
-
int a = 3;
-
}
-
This is OK in C++ but this is C and not C++.
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
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: -
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.
-
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++.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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,...
|
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
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |