Copying files using fgets instead of fgetc?












0














I made a code:



#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv) // taking files
{
FILE * fp;
FILE * fp2;
int c;
.
. // checking possible problems
.
while ( (c = fgetc(fp)) != EOF){ // copying one file to another one
fputc (c, stdout);
fputc (c, fp2);
}

fclose(fp);
fclose(fp2);
fp = NULL;
fp2 = NULL;

return EXIT_SUCCESS;
}


I would ask you, if is it possible to make the same while statement with use of fgets function instead of fgetc- like:



fgets(fp, sizeof(fp), stdin));









share|improve this question
























  • Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
    – Jonathan Leffler
    Nov 20 at 22:41












  • Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
    – Jonathan Leffler
    Nov 21 at 0:11
















0














I made a code:



#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv) // taking files
{
FILE * fp;
FILE * fp2;
int c;
.
. // checking possible problems
.
while ( (c = fgetc(fp)) != EOF){ // copying one file to another one
fputc (c, stdout);
fputc (c, fp2);
}

fclose(fp);
fclose(fp2);
fp = NULL;
fp2 = NULL;

return EXIT_SUCCESS;
}


I would ask you, if is it possible to make the same while statement with use of fgets function instead of fgetc- like:



fgets(fp, sizeof(fp), stdin));









share|improve this question
























  • Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
    – Jonathan Leffler
    Nov 20 at 22:41












  • Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
    – Jonathan Leffler
    Nov 21 at 0:11














0












0








0







I made a code:



#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv) // taking files
{
FILE * fp;
FILE * fp2;
int c;
.
. // checking possible problems
.
while ( (c = fgetc(fp)) != EOF){ // copying one file to another one
fputc (c, stdout);
fputc (c, fp2);
}

fclose(fp);
fclose(fp2);
fp = NULL;
fp2 = NULL;

return EXIT_SUCCESS;
}


I would ask you, if is it possible to make the same while statement with use of fgets function instead of fgetc- like:



fgets(fp, sizeof(fp), stdin));









share|improve this question















I made a code:



#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv) // taking files
{
FILE * fp;
FILE * fp2;
int c;
.
. // checking possible problems
.
while ( (c = fgetc(fp)) != EOF){ // copying one file to another one
fputc (c, stdout);
fputc (c, fp2);
}

fclose(fp);
fclose(fp2);
fp = NULL;
fp2 = NULL;

return EXIT_SUCCESS;
}


I would ask you, if is it possible to make the same while statement with use of fgets function instead of fgetc- like:



fgets(fp, sizeof(fp), stdin));






c file fgets fgetc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:44









Jonathan Leffler

559k896651016




559k896651016










asked Nov 20 at 22:19









K. Jarek

32




32












  • Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
    – Jonathan Leffler
    Nov 20 at 22:41












  • Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
    – Jonathan Leffler
    Nov 21 at 0:11


















  • Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
    – Jonathan Leffler
    Nov 20 at 22:41












  • Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
    – Jonathan Leffler
    Nov 21 at 0:11
















Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
– Jonathan Leffler
Nov 20 at 22:41






Note that since you have FILE *fp;, your example fgets(fp, sizeof(fp), stdin) would be wholly inappropriate, even though it would compile — C is not a type-safe language at times. You want char buffer[4096]; and fgets(buffer, sizeof(buffer), stdin) (or use fp instead of stdin if you're reading from a file instead).
– Jonathan Leffler
Nov 20 at 22:41














Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
– Jonathan Leffler
Nov 21 at 0:11




Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? and What do I do when someone answers my question?
– Jonathan Leffler
Nov 21 at 0:11












1 Answer
1






active

oldest

votes


















1














Yes, it's possible to use fgets() and fputs().
If you're using POSIX systems, you might find getline() better than fgets(), but you'd still use fputs() with it. Or you might find fread() and fwrite() appropriate instead. All of those work on arrays — you get to choose the size, you'd likely be using 4096 bytes as a buffer size.



For example:



char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
fputs(buffer, stdout);
fputs(buffer, fp2);
}


Or:



char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
{
fwrite(buffer, sizeof(char), nbytes, stdout);
fwrite(buffer, sizeof(char), nbytes, fp2);
}


Warning: uncompiled code.



Note that in theory you should test that the fwrite() operations worked OK (no short writes). Production quality code would do that. You should also check that the fputs() operations succeed too.



Note that as hinted at by user2357112, the original code will work with arbitrary binary files (containing null bytes '') quite happily (though stdout may be less happy with it). Using fread() and fwrite() will work perfectly with binary files, and the only issue with text files is that they'd ignore newlines and write blocks of text as they're read (which is not usually an actual problem).






share|improve this answer























  • Isn't the fgets version also going to have issues with null bytes in the input?
    – user2357112
    Nov 20 at 22:46










  • @user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
    – Jonathan Leffler
    Nov 20 at 22:54













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402455%2fcopying-files-using-fgets-instead-of-fgetc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Yes, it's possible to use fgets() and fputs().
If you're using POSIX systems, you might find getline() better than fgets(), but you'd still use fputs() with it. Or you might find fread() and fwrite() appropriate instead. All of those work on arrays — you get to choose the size, you'd likely be using 4096 bytes as a buffer size.



For example:



char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
fputs(buffer, stdout);
fputs(buffer, fp2);
}


Or:



char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
{
fwrite(buffer, sizeof(char), nbytes, stdout);
fwrite(buffer, sizeof(char), nbytes, fp2);
}


Warning: uncompiled code.



Note that in theory you should test that the fwrite() operations worked OK (no short writes). Production quality code would do that. You should also check that the fputs() operations succeed too.



Note that as hinted at by user2357112, the original code will work with arbitrary binary files (containing null bytes '') quite happily (though stdout may be less happy with it). Using fread() and fwrite() will work perfectly with binary files, and the only issue with text files is that they'd ignore newlines and write blocks of text as they're read (which is not usually an actual problem).






share|improve this answer























  • Isn't the fgets version also going to have issues with null bytes in the input?
    – user2357112
    Nov 20 at 22:46










  • @user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
    – Jonathan Leffler
    Nov 20 at 22:54


















1














Yes, it's possible to use fgets() and fputs().
If you're using POSIX systems, you might find getline() better than fgets(), but you'd still use fputs() with it. Or you might find fread() and fwrite() appropriate instead. All of those work on arrays — you get to choose the size, you'd likely be using 4096 bytes as a buffer size.



For example:



char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
fputs(buffer, stdout);
fputs(buffer, fp2);
}


Or:



char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
{
fwrite(buffer, sizeof(char), nbytes, stdout);
fwrite(buffer, sizeof(char), nbytes, fp2);
}


Warning: uncompiled code.



Note that in theory you should test that the fwrite() operations worked OK (no short writes). Production quality code would do that. You should also check that the fputs() operations succeed too.



Note that as hinted at by user2357112, the original code will work with arbitrary binary files (containing null bytes '') quite happily (though stdout may be less happy with it). Using fread() and fwrite() will work perfectly with binary files, and the only issue with text files is that they'd ignore newlines and write blocks of text as they're read (which is not usually an actual problem).






share|improve this answer























  • Isn't the fgets version also going to have issues with null bytes in the input?
    – user2357112
    Nov 20 at 22:46










  • @user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
    – Jonathan Leffler
    Nov 20 at 22:54
















1












1








1






Yes, it's possible to use fgets() and fputs().
If you're using POSIX systems, you might find getline() better than fgets(), but you'd still use fputs() with it. Or you might find fread() and fwrite() appropriate instead. All of those work on arrays — you get to choose the size, you'd likely be using 4096 bytes as a buffer size.



For example:



char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
fputs(buffer, stdout);
fputs(buffer, fp2);
}


Or:



char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
{
fwrite(buffer, sizeof(char), nbytes, stdout);
fwrite(buffer, sizeof(char), nbytes, fp2);
}


Warning: uncompiled code.



Note that in theory you should test that the fwrite() operations worked OK (no short writes). Production quality code would do that. You should also check that the fputs() operations succeed too.



Note that as hinted at by user2357112, the original code will work with arbitrary binary files (containing null bytes '') quite happily (though stdout may be less happy with it). Using fread() and fwrite() will work perfectly with binary files, and the only issue with text files is that they'd ignore newlines and write blocks of text as they're read (which is not usually an actual problem).






share|improve this answer














Yes, it's possible to use fgets() and fputs().
If you're using POSIX systems, you might find getline() better than fgets(), but you'd still use fputs() with it. Or you might find fread() and fwrite() appropriate instead. All of those work on arrays — you get to choose the size, you'd likely be using 4096 bytes as a buffer size.



For example:



char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
fputs(buffer, stdout);
fputs(buffer, fp2);
}


Or:



char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
{
fwrite(buffer, sizeof(char), nbytes, stdout);
fwrite(buffer, sizeof(char), nbytes, fp2);
}


Warning: uncompiled code.



Note that in theory you should test that the fwrite() operations worked OK (no short writes). Production quality code would do that. You should also check that the fputs() operations succeed too.



Note that as hinted at by user2357112, the original code will work with arbitrary binary files (containing null bytes '') quite happily (though stdout may be less happy with it). Using fread() and fwrite() will work perfectly with binary files, and the only issue with text files is that they'd ignore newlines and write blocks of text as they're read (which is not usually an actual problem).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 0:11

























answered Nov 20 at 22:31









Jonathan Leffler

559k896651016




559k896651016












  • Isn't the fgets version also going to have issues with null bytes in the input?
    – user2357112
    Nov 20 at 22:46










  • @user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
    – Jonathan Leffler
    Nov 20 at 22:54




















  • Isn't the fgets version also going to have issues with null bytes in the input?
    – user2357112
    Nov 20 at 22:46










  • @user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
    – Jonathan Leffler
    Nov 20 at 22:54


















Isn't the fgets version also going to have issues with null bytes in the input?
– user2357112
Nov 20 at 22:46




Isn't the fgets version also going to have issues with null bytes in the input?
– user2357112
Nov 20 at 22:46












@user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
– Jonathan Leffler
Nov 20 at 22:54






@user2357112: yes — it is. If you copy binary data with text functions, you get what you get. If the copying is for arbitrary files, then fread() and fwrite() are appropriate because they handle nulls fine. One problem with fgets() is that it does not tell you how many bytes it read, so if you read a line and the newline is missing, it could be because there was a null byte in the input. But there's no way to know. The getline() function reports how many bytes were read; you could use fwrite() to ensure that those bytes are all written. There are issues with getline(), too.
– Jonathan Leffler
Nov 20 at 22:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402455%2fcopying-files-using-fgets-instead-of-fgetc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python