how to remove newline character

Mar 30, 2010 at 12:55pm

hello every one in the below code


void List::Fileoperation()
{
static s3eFile* a_FileHandle;
static char* a_DataToBeRead;
int flag=0;

char* ch;
a_FileHandle = s3eFileOpen("LabValues.csv", "r");
int j=0;
ch = new char[100];
while(s3eFileReadString(ch,100,a_FileHandle)!=0)
{
//the above loop read one string at a time from a file including newline charater
items[j++] = ch;//copying string to character pointer array
ch = new char[100];
}


s3eFileClose(a_FileHandle);
}
in the above code am reading strings from a file the deffect here is it will read ne line character also.
when i start copying the string i don't want to copy the newline character
with that character itself am not able to compare with the another string which has same name but doesnot have the newline character


plz help me how to remove the newline chracter from the string..




Mar 30, 2010 at 2:34pm
you dont have to do ch = new char[100];
it should work with out it.
you always get 100bytes of new memory but dont delete the old one, so you waste a lot of memory.
if it does not work without new (but i dont know why it should not) use delete[] ch; before ch = new char[100];

but to your question:
i assume you dont want to add the "new line" to your items array.
try
1
2
3
if (ch != '\n') { 
  items[j++] = ch;
}

a spontaneous idea (i dont have any chance to test this now, sorry)
maybe you also need to check for \r on windows (if someone could confirm or refute this, it would be nice, cuz im not sure)
Last edited on Mar 30, 2010 at 2:35pm
Mar 30, 2010 at 6:51pm
closed account (1yR4jE8b)
You are right, in windows you need to check for both '\r' AND '\n'

On a mac, you only need to check for '\r', and in Linux you only need to check for '\n'
Mar 31, 2010 at 4:08am
sorry dude ..
i already mentioned that while(s3eFileReadString(ch,100,a_FileHandle)!=0) read one string at a time
lets assume there is a string "astrology"

it reads that string like "astrology#" assume #is a newline character
and how to delete that character is my question

if (ch != '\n') {
items[j++] = ch;
}


i think as per your code mentioned above you want me to read character by character and check for "\n"
but it is not possible here

anyway thank you for your replay


regards
yakub
Apr 13, 2010 at 5:39pm
char* p;
if (ch=='\n') {p=&ch; p=p+1;}
//should work
Topic archived. No new replies allowed.