Pelles C forum

C language => Beginner questions => Topic started by: vedro-compota on May 22, 2012, 10:15:15 AM

Title: printf() - how to ignore newline
Post by: vedro-compota 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)
Title: Re: printf() - how to ignore newline
Post by: TimoVJL 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
Title: Re: printf() - how to ignore newline
Post by: vedro-compota 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);
}
Title: Re: printf() - how to ignore newline
Post by: CommonTater 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.

 
Title: Re: printf() - how to ignore newline
Post by: DMac 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;
}
Title: Re: printf() - how to ignore newline
Post by: TimoVJL 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;
}
Title: Re: printf() - how to ignore newline
Post by: vedro-compota on May 25, 2012, 07:50:19 AM
thanks for examples, guys)