I want to copy dest.c into orig.c and then display the result, so I wrote this:


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

main(  )
{
FILE *fin = fopen("C:dest.c"," rb"), *fout = fopen("C:orig.c","ab") ;
int ch;


while ((ch=fgetc(fin))!=EOF)
 fputc(ch,fout);
 
 while((ch = fgetc(fout))!=EOF)
 putchar(ch);
 
 fclose(fin);
fclose(fout);

}


It copies dest.c into orig.c, but it displays a bunch of paragraphs in the cmd. Is this normal, or is there a way to overcome this?


Also, this adds millions of paragraphs (not exagerating) to the modified file, so the output must be too big and so the cmd only show part of it. Like it does with the command tree.
Well, there is a couple problems with your code. Here's a fixed version that should work fine:

1. The main problem appears to be you are opening it in rb and ab mode, change these to r and w mode
2. Also, your path might also have an issue. change C: to C:\

3. Use this too, by the way if need be:

Code:
fcloseall();


And a C++ version that I enjoy more. Smile


Code:
ifstream in("In.txt");
    ofstream out("Out.txt");
    string str;
    while(getline(in,str)) {
        out<<str;
    }
    in.close();
    out.close();
MateoConLechuga wrote:
Well, there is a couple problems with your code. Here's a fixed version that should work fine:

1. The main problem appears to be you are opening it in rb and ab mode, change these to r and w mode
2. Also, your path might also have an issue. change C: to C:\

3. Use this too, by the way if need be:

Code:
fcloseall();


And a C++ version that I enjoy more. Smile


Code:
ifstream in("In.txt");
    ofstream out("Out.txt");
    string str;
    while(getline(in,str)) {
        out<<str;
    }
    in.close();
    out.close();


1. That overwrites orig.c with dest.c. And still creates millions of paragraphs/ empty code lines.
2. It returns NULL. Don't forget I'm using cmd, on Windows. Maybe what you sugested works on Linux?

3. I can't use fcloseall, the compiler says "Undefined reference..."
I wrote #include<stdlib.h>, still doesn't work. How do I use it?
Sorry, I needed to swap those two. 'r' and 'w' should match up to orig.c and dest.c respectively. Not sure if fcloseall() is only linux, but since that is pretty much all I use it on, that could be the case. Smile Just make that one change then, and see what happens.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement