printf() - how to ignore newline

Started by vedro-compota, May 22, 2012, 10:15:15 AM

Previous topic - Next topic

vedro-compota

\Hi there)

Please tell me  - how to ignore new line character  in string during  printing it by printf() -
char* str= "123\n4567";
printf("%s", str); // how to ignore here?


big thanks in advance)

TimoVJL

Do you mean this:char* str= "123\\n4567";
printf("%s", str); // how to ignore here?
to print 123\n4567
May the source be with you

vedro-compota

no -  I want to escape   \n (new line) character in string for ctime=
char* timeme()
{
   const time_t timer = time(NULL);
   return ctime(&timer);
}

CommonTater

I'm sorry... but I don't think we're understanding what you want to do...

If you have a string like 123\n456  and feed that to printf() or just about any other C display command, it's going to print as:

123
456

Now what do you want to see?

123456

If that's the case then you need to search for the \n which will probably exist in the string as either 0x0A and/or 0x0D and delete them before printing.


DMac

You could use a trim type function to remove white space from the time string.

#include <stdio.h>
#include <time.h>

char *trim(char *S)
{
while (*S == 32 || *S == 9 || *S == 10 || *S == 11 || *S == 13)
{
S++;
}

register int i = strlen(S);
while (i > 0 && (S[i - 1] == 32 || S[i - 1] == 9 || S[i - 1] == 10 || S[i - 1] == 11 || S[i - 1] == 13))
{
i--;
}
S[i] = '\0';
return S;
}

char *timeme(void)
{
const time_t timer = time(NULL);
return ctime(&timer);
}

int main(void)
{
char *s = timeme();
printf("The Date Time: %s trimed.\n", trim(s));
return 0;
}
No one cares how much you know,
until they know how much you care.

TimoVJL

char *timeme(void)
{
const time_t timer = time(NULL);
char *s = ctime(&timer);
*(s+24) = 0;
return s;
}
May the source be with you

vedro-compota