I'm trying to write a string to my file using fprintf () is there a function that i can write only specified number of bytes from my string to the file Example : "Hello_world" if n was 5 i want to write only "Hello" to my file
Asked
Active
Viewed 959 times
-1
-
You ask badly. Modules designed to modify string variables (in C++ or C) can also be found via Google. Your problem is not writing to a file, but working with a string variable - shortening it. So your problem is not "how to write 5 characters long string", but how to cut out too long text. If you know, so please let your question know again. – s3n0 May 20 '18 at 16:14
-
@s3n0 The way of asking is actually not bad. And the way OP wants to handle the problem is more efficient than first changing a string and then writing it completly to a file. Also, please compare your comment to the "be nice" policy. – Yunnosch May 20 '18 at 16:18
-
@Yunnosch: (1) The user mentioned the buffer in the title - as a system programmer I know what the word "buffer" means. (2) The user did not use the word "buffer" once in the description of his problem. (3) The user writes about a string variable whose length is greater than 5 (nonsense "N" as the number of characters), then the string is cut to length 5. There are not mentioned any ... bytes ( in the case of binary access to the file) and even even characters - and no buffer at all! So I thought it was (maybe) a write-line-to-text-file method. If I'm wrong, please correct me. – s3n0 May 20 '18 at 16:37
-
check this question: https://stackoverflow.com/questions/11573974/write-to-txt-file – danglingpointer May 20 '18 at 17:11
-
@s3n0 1) "buffer" in title: true; 1) you know one meaning of "buffer", possibly system-programming-related; you assume that OP thinks of the same meaning: questionable 2) "buffer" not in body: true 3) OP mentions string of length >5": implicitly true 3) you describe something as "nonsense": unclear what, questionably polite; 3) no clarification request of something which seems nonsense to you: unhelpful 3) "string is cut to length 5": no idea what you are referring to 3) "no mentioned any bytes" vs. "write only specified number of bytes": mismatch. Then "I thought it was ...": more assumptions – Yunnosch May 20 '18 at 19:21
-
@s3n0 Ratio of "true" to "questionable": 3:6 That means you are not totally "wrong", but I wouldn't describe your comments as "correct and helpful" either, on top of the simply too bluntly unhelpful "You ask badly." – Yunnosch May 20 '18 at 19:24
-
@Yunnosch: In the first comment, I warned the user that the query is wrong and what the user MUST REPAIR in the query so that other users can help. You may not have seen it, but that does not mean that all the users are like you. I've seen 5 options in his queries. I'm not in the mood to list everything. The question was typed incorrectly. It is not clear what exactly the user needs to advise. To combine in one sentence the write buffer, the file, the string, and the "length" of 5 something (Unicode byte? string variable? buffer? file?) is crap. – s3n0 May 20 '18 at 19:32
-
The programmer must know exactly what he wants. If he does not know, he will never help. – s3n0 May 20 '18 at 19:35
-
i think my question is pretty clear and i got a clear answer from people who knows the answer rather than judging new programmers – Tick tack May 21 '18 at 22:11
1 Answers
1
If you're looking to use fprintf only, you could create a temporary buffer till the position that you need to copy.
char s[20] = "Hello World", temp[20];
int n = 5;
strncpy(temp, s, n);
And then carry on with writing temp to file using fprintf().
Incorporating point made by Yunnosch, if modifying the original string is permitted, s[n] = '\0'; would be more time efficient. Also look at user3121023's comment about precision specifier.
Speeeddy
- 154
- 7
-
Please compare the efficiency and implicity of your solution to any of the methods which really write from the original buffer only a given number of bytes, including a potential second step to append any terminator. E.g. see other answer, `fwrite()`. – Yunnosch May 20 '18 at 16:27