NO

Author Topic: printf() - how to ignore newline  (Read 12820 times)

vedro-compota

  • Guest
printf() - how to ignore newline
« on: May 22, 2012, 10:15:15 AM »
\Hi there)

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

big thanks in advance)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: printf() - how to ignore newline
« Reply #1 on: May 22, 2012, 12:18:21 PM »
Do you mean this:
Code: [Select]
char* str= "123\\n4567";
printf("%s", str); // how to ignore here?
to print 123\n4567
May the source be with you

vedro-compota

  • Guest
Re: printf() - how to ignore newline
« Reply #2 on: May 24, 2012, 04:25:25 PM »
no -  I want to escape   \n (new line) character in string for ctime=
Code: [Select]
char* timeme()
{
   const time_t timer = time(NULL);
   return ctime(&timer);
}

CommonTater

  • Guest
Re: printf() - how to ignore newline
« Reply #3 on: May 24, 2012, 06:09:10 PM »
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.

 

Offline DMac

  • Member
  • *
  • Posts: 272
Re: printf() - how to ignore newline
« Reply #4 on: May 24, 2012, 06:52:06 PM »
You could use a trim type function to remove white space from the time string.

Code: [Select]
#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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: printf() - how to ignore newline
« Reply #5 on: May 24, 2012, 07:13:05 PM »
Code: [Select]
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

  • Guest
Re: printf() - how to ignore newline
« Reply #6 on: May 25, 2012, 07:50:19 AM »
thanks for examples, guys)