473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to printf 'n' characters from a char* to a file?

Hi,

Can you please tell me how can I printf 'n' characters of from a char*
to a file.

I notice there is a snprinf, but I don't see one corresponding to
fprintf?

I am thinking if i can do that without an extra memcpy.

Thank you for any help.
Jun 27 '08 #1
4 8605
In article <8a**********************************@t54g2000hsg. googlegroups.com>,
Jonathan <Jo**************@gmail.comwrote:
>Can you please tell me how can I printf 'n' characters of from a char*
to a file.
>I notice there is a snprinf, but I don't see one corresponding to
fprintf?
>I am thinking if i can do that without an extra memcpy.
printf("%.*s", n, TheCharPointer);

Or if n is known ahead of time and fixed, it can be hard-coded,
such as

printf("%.5s", TheCharPointer);

If the string does not contain at least n characters, you have
a problem. Also, the behaviour is not explicitly defined
(at least not in C89) if the string pointed to does not contain a 0
before the end of the object: you are still technically responsible
for passing a string in the parameter position, even if you've told
it to only copy so many characters out of the string.
--
amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's
"Walter the Farting Dog"
Jun 27 '08 #2
Jonathan said:
Hi,

Can you please tell me how can I printf 'n' characters of from a char*
to a file.
fprintf(fp, "%.*s\n", n, s);

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3
On Jun 5, 3:27*pm, Richard Heathfield <r...@see.sig.invalidwrote:
Jonathan said:
Hi,
Can you please tell me how can I printf 'n' characters of from a char*
to a file.

fprintf(fp, "%.*s\n", n, s);

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Thank you for all the answers.
Jun 27 '08 #4
On Thu, 05 Jun 2008 20:21:29 +0000, Walter Roberson wrote:
printf("%.*s", n, TheCharPointer);
[...]
Also, the behaviour is not explicitly defined (at least not in
C89) if the string pointed to does not contain a 0 before the end of the
object:
<nit>If the array pointed to does not contain a 0 before the end of the
object, it's not a string.</nit>
you are still technically responsible for passing a string in
the parameter position, even if you've told it to only copy so many
characters out of the string.
Really? Could you show what C89 says? I don't have a copy of it, but I'm
interested because both a draft and text based on the official standard do
make it clear '\0' is not required, and the text in both is largely
identical:

C89 draft:
"Characters from the array are written up to (but not including) any
terminating null character; if the precision is specified, no more than
that many characters are written. If the precision is not specified or is
greater than the size of the array, the array shall contain a null
character."

SUSv2, based on C89:
"Bytes from the array are written up to (but not including) any
terminating null byte. If the precision is specified, no more than that
many bytes are written. If the precision is not specified or is greater
than the size of the array, the array must contain a null byte."

I would be very surprised if this text was present in the drafts, dropped
from the C89 standard, only to be added again afterwards by both SUS and
C99.
Jun 27 '08 #5

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

Similar topics

3
by: Bilbo | last post by:
Hello! I have a matrix type of variable, lets call it matrix...can I see on screen it via printf?? how?? Thanks
7
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various...
4
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
12
by: baumann | last post by:
hi all, printf("%c",b) doesn't work properly. #include <stdio.h> int a , b; char d, e; char * p; float f; int main(int argc, char* argv) {
6
by: chiara | last post by:
Hi everybody! I am just at the beginning as a programmer, so maybe this is a stupid question...Anyway,I need to write a function in C to generate generate all possible strings of given length...
9
by: kaustubh.deo | last post by:
I am facing issues printing french chars like using printf function. I have reproduced this issue with simple C program as follows. #include <stdio.h> #include <locale.h> int main(int...
11
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab,...
40
by: somenath | last post by:
Hi All, I have one question regarding the behavior of printf function. In page number of 154 in K&R2 in Table 7-1 it is stated that Characters Argument Type :...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.