473,698 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove \0 from a string

hi
here my code

FILE *fp;
char *line[2];
#define LINE_MAX 30

fp = fopen("test1.tx t", "r");
if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);

fp = fopen("test2.tx t", "r");
if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[1], LINE_MAX, fp);
fclose(fp);
are there a way to remove \0 for line[0] and line[1]?

thanks

Nov 14 '05 #1
2 9538


collinm wrote:
hi
here my code

FILE *fp;
char *line[2];
#define LINE_MAX 30

fp = fopen("test1.tx t", "r");
if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);

fp = fopen("test2.tx t", "r");
if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[1], LINE_MAX, fp);
fclose(fp);
are there a way to remove \0 for line[0] and line[1]?


Are you sure you want to remove the '\0' character?
Doing this may render the object to not be a string.
It is common to desire to remove the newline char, '\n'.
Perhaps that is what you want to do.
One way:
#include <string.h>
char *s;

if((s = strrchr(line[0],'\n')) != NULL) *s = '\0';

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2


collinm wrote:
hi
here my code

FILE *fp;
char *line[2];
#define LINE_MAX 30

fp = fopen("test1.tx t", "r");
if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);

fp = fopen("test2.tx t", "r");
if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[1], LINE_MAX, fp);
fclose(fp);
are there a way to remove \0 for line[0] and line[1]?


I am not sure what you are asking, but it *might*
be about one or another of these issues:

- The malloc() calls and the way you test for their
success are wrong. Look carefully: You ask malloc() for
LINE_MAX bytes, then you add 1 to whatever value malloc()
returns, then you store the sum in line[0] or line[1],
and finally you compare the sum to NULL. If malloc()
succeeds, line[0] or line[1] points to the second byte
of the allocated area, which is probably not what you
want -- in particular, you need to write free(line[0]-1)
when you release the area. If malloc() fails, adding 1
to NULL produces undefined behavior -- the most likely
outcome is that you will get a sum that is not equal to
NULL but is useless because "it doesn't point anywhere."

- You might have intended malloc(LINE_MAX + 1) instead
of malloc(LINE_MAX ) + 1; if so, that would be correct from
the standpoint of memory management. However, it would be
unnecessary because fgets() will never attempt to store
anything in that "extra" byte. The second argument to
fgets() is the entire length of the buffer *including*
space for the '\0' fgets() will store; fgets() will store
at most LINE_MAX characters *including* the '\0'.

- When you say you want to "remove \0," do you mean
that you want to eliminate the "+1" from the allocations?
If that's it, go ahead: as mentioned above, fgets() will
not store more bytes than its second argument indicates.

- ... or do you actually want to "remove" the '\0'
that fgets() stores after the end of each input line?
That seems a very stupid and dangerous thing to do, but
if you really want you can overwrite the '\0' with a
different character:

*strchr(line[0], '\0') = 'X';

- ... or do you want to remove the '\n' (not '\0')
at the end of the input line? That makes more sense,
since the trailing '\n' is often not wanted. You need
to be just a little bit careful, though, because if the
input line is too long for the buffer fgets() will store
only the first part of it and there will be no '\n' to
"remove." Here's one way to do it:

char *p;
...
p = strchr(line[0], '\n');
if (p != NULL)
*p = '\0';

.... and here's a shorter alternative using a less familiar
function:

line[0][ strcspn(line[0], "\n") ] = '\0';

- Finally, it is a good thing that you are careful to
check for failure of malloc(), even though you have done
it incorrectly. However, it is a bad thing that you have
*not* checked for failure of fopen() or for failure of
fgets()! High-tech motion-sensing locks on your attic
windows do not improve the security of your house if you
leave the front door hanging open.

--
Er*********@sun .com

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12708
by: Christopher Armstrong | last post by:
Hello! I'm trying to write a part of a program that will remove all files in its directory. I have tried the std::remove feature of the standard library, but I don't know its syntax. Also, what's the difference between std::remove and std::erase? Thanks for your time!
12
55564
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are also removed) So far I have (val is value, ar is array, returns new array):
4
3972
by: u7djo | last post by:
Hi, I'm currently building an application in Access and as part of this need to import forms and modules from another database. Some of the imports will be revisions of existing forms/modules so I need to check and see they already exist before importing them so I can remove the old one. I've figured out how to remove the classes and modules before importing the revised version but I'm having problems with the forms. I was hoping to use...
1
19017
by: Craig Buchanan | last post by:
what is the fastest way to remove a value from a string array? something like: dim x as string() = {"A","B","C","D"} 'remove C x.Clear(x, x.IndexOf(x, "C"), 1) Questions:
3
5404
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some example first:
31
4588
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there are more objects that match the first object that i grabbed. If they match then I put them in an array. I would like to remove each match from the arraylist as I find them to speed things up and so that they don't get checked again. If I try...
15
50247
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
10
3206
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to remove the punctuation characters from the "first name" substring. I've looked at the basic_string in C++, but I can't find the functions that will _remove" character(s) from a value. There are operators that are helpful in finding the position...
26
13789
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma and return "12384" What is the most efficient, fastest way to approach this? Thanks,
10
2412
by: Tony Johansson | last post by:
Hello! I have a collection with a lot of object. Each object has the following definition. public class ParametermappingObj { private string Name {get;set;} private string Id {get;set;} some other field defined here ....
0
8683
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, 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...
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, 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...
0
9031
jinu1996
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...
0
8876
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 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...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.