Hi folks
I am trying to write a program which acts like a p2p server. When the
program starts it reads a file from whereit will read broadcast IP
address, port and another port number. Now I am trying skip the
comments and empty lines by saying if there is newline or # sign at the
first charecter of a line then to skip the line.But the problem is the
program is not working.
I am using the following code:
line = malloc(sizeof (char) *SIZE_OF_LINE);
while(fgets(lin e, SIZE_OF_LINE + 1, fp) != NULL)
{
i f(line[0] != '#' || line[0] != '\n')
{
fprintf(stderr, "i am here\n");
fprintf(stderr, "%s", line);
strcpy(tmpEntry , strtok(line, ":"));
strcpy(tmpValue , strtok(NULL, "\n"));
fprintf(stderr, "\t%s", tmpEntry);
fprintf(stderr, "\t: %s\n", tmpValue);
}
}
and the file the program is reading is:
############### ############### ############### ############### ############### ##
# P2P Conf File
#Please Don't modify this file as this is file is used to
#create all required sockets
# Author : Ahetesham Qazi
############### ############### ############### ############### ############### ##
Brodcast_IP:192 .168.1.255
Brodcast_Port:3 0001
Comm_Port:30101
The program should skip the first seven lines but the problem it's not
skipping. and when it's trying to tokenize the first line and then
printing it it's giving me segmentation fault.
Any suggetion would be greatly appreciated
Thanks
Ahetesham Qazi 14 1758 aq***@inbox.com said:
if(line[0] != '#' || line[0] != '\n')
How many characters can you think of that will fail this condition?
Read it out loud. If this character is not a hash, OR it is not a newline...
So a hash PASSES the test because it is not a newline, and a newline PASSES
the test because it is not a hash.
You meant to use && rather than ||
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
>I am trying to write a program which acts like a p2p server. When the
>program starts it reads a file from whereit will read broadcast IP address, port and another port number. Now I am trying skip the comments and empty lines by saying if there is newline or # sign at the first charecter of a line then to skip the line.
....
>I am using the following code:
line = malloc(sizeof (char) *SIZE_OF_LINE);
while(fgets(li ne, SIZE_OF_LINE + 1, fp) != NULL) {
i f(line[0] != '#' || line[0] != '\n')
A newline is not equal to '#'.
A # is not equal to '\n'.
Another character is not equal to either.
In other words, the condition is always true.
Do you really want to use OR here?
Richard Heathfield wrote: aq***@inbox.com said:
if(line[0] != '#' || line[0] != '\n')
How many characters can you think of that will fail this condition?
Read it out loud. If this character is not a hash, OR it is not a newline...
So a hash PASSES the test because it is not a newline, and a newline PASSES
the test because it is not a hash.
You meant to use && rather than ||
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Thanks for the solution. Actually this is a strictly predefined file.
So whithout the actual data
the only charecter can be at the start of the file is "#" or "\n"
Ahetesham Qazi aq***@inbox.com said:
>
Richard Heathfield wrote:
>aq***@inbox.com said:
if(line[0] != '#' || line[0] != '\n')
How many characters can you think of that will fail this condition?
Read it out loud. If this character is not a hash, OR it is not a newline...
So a hash PASSES the test because it is not a newline, and a newline PASSES the test because it is not a hash.
You meant to use && rather than ||
Thanks for the solution. Actually this is a strictly predefined file.
So whithout the actual data
the only charecter can be at the start of the file is "#" or "\n"
Nevertheless, all characters, *including* hash and newline, will pass your
existing test, the one that is intended to filter them out. When you change
the || to && the filter will have the desired effect.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) aq***@inbox.com wrote:
Hi folks
I am trying to write a program which acts like a p2p server. When the
program starts it reads a file from whereit will read broadcast IP
address, port and another port number. Now I am trying skip the
comments and empty lines by saying if there is newline or # sign at the
first charecter of a line then to skip the line.But the problem is the
program is not working.
I am using the following code:
line = malloc(sizeof (char) *SIZE_OF_LINE);
while(fgets(lin e, SIZE_OF_LINE + 1, fp) != NULL)
Do not lie to fgets. You only have storage for
SIZE_OF_LINE chars, not SIZE_OF_LINE + 1. aq***@inbox.com wrote:
line = malloc(sizeof (char) *SIZE_OF_LINE);
sizeof(char) is by definition 1, *always*, so this can be reduced to
line = malloc (SIZE_OF_LINE);
--
Nick Keighley
Nick Keighley said: aq***@inbox.com wrote:
>line = malloc(sizeof (char) *SIZE_OF_LINE);
sizeof(char) is by definition 1, *always*, so this can be reduced to
line = malloc (SIZE_OF_LINE);
That's one perfectly sensible approach. Here's another:
line = malloc(SIZE_OF_ LINE * sizeof *line);
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) aq***@inbox.com wrote:
line = malloc(sizeof (char) *SIZE_OF_LINE);
This would be a bit safer as: line = malloc( sizeof( *line ) );
while(fgets(lin e, SIZE_OF_LINE + 1, fp) != NULL)
similarly: while(fgets(lin e, sizeof( *line ), fp) != NULL)
if(line[0] != '#' || line[0] != '\n')
Several things amiss here:
(1) Are you sure if line[0] has been set? Yes, fgets() USUALLY puts
in a "\n", but not in every case.
You should do something like this first: : if( strlen(line) 0 ) {
....
(2) The tests are backwards.. It's more obvious if you write:
if( line[0] == '#' || line[0] == '\n' ) { /* ignore this line */ }
else { /* process it */
...
}
(3) What if the user accidentally types a space or tab, either leading
or trailing the line? Wouldnt hurt to handle these cases too.
(3) You're being awfully optimistic about what's in the file. I'd add
several tests to ensure that strtok finds what you expect, and the
length of the token doesnt overflow the destination. What you have
right now is an excellent way for somebody to crash or own your server
with a buffer overflow if they get write access to this file.
Ancient_Hacker said:
> aq***@inbox.com wrote:
>line = malloc(sizeof (char) *SIZE_OF_LINE);
This would be a bit safer as: line = malloc( sizeof( *line ) );
Er, no it wouldn't.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return substring
before a pattern), AFTER (return substring after a pattern), and
BETWEEN (return substring between 2 patterns).
My questions are:
1. Can any tell me how I can implement such functionality in C#?
2. Is it possible to add/include function...
|
by: comshiva |
last post by:
Hi all,
I have converted my existing ASP.NET project from 1.1 to 2.0 and i have
found that everything works fine except the linkbutton control in my
datagrid which throws an javascript error when clicked. I thought the
control might be the problem, so i deleted the old control and binded
the new linkbutton control but am still getting the same error. Am
using visual studio 2005.
Source code inside my grid:...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c */
#include <time.h>
|
by: WaterWalk |
last post by:
Hello, I'm currently learning string manipulation. I'm curious about
what is the favored way for string manipulation in C, expecially when
strings contain non-ASCII characters. For example, if substrings need
be replaced, or one character needs be changed, what shall I do? Is it
better to convert strings to UCS-32 before manipulation?
But on Windows, wchar_t is 16 bits which isn't enough for characters
which can't be simply encoded...
|
by: micklee74 |
last post by:
hi
if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"
When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
| |
by: Niyazi |
last post by:
Hi,
Does anyone knows any good code for string manipulation similar to
RegularExpresion?
I might get a value as string in a different format. Example:
20/02/2006 or 20,02,2006 or 20.02.2006 etc...
And I want to replace the /,.etc character with - (as 20-02-2006)
|
by: ThatVBGuy |
last post by:
Hello All,
I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below :
Public Function ReadFileContents(FileFullPath As String) As _
String
On Error GoTo...
|
by: crprajan |
last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) .
So the output of the string will be “This is string”
This is very urgent. Please help
|
by: frankeljw |
last post by:
I have 2 Java strings
1st String is a series of names, colons, and numbers
ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44
2nd String is a name
ie) Name2
I need to get the number associated with that name.
|
by: mann_mathann |
last post by:
can anyone tell me a solution:
i cannot use the features in standard c++ string classgh i included
the string.h file but still its not working.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |