Thread: copy data from one file to another

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    copy data from one file to another

    Hi
    I'm a beginner and was wondering how to copy data from one file (have to open that file first on " ?" mode, and then modifying that data when copying it to another file) using C
    thanks,

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To learn C, you need a book, or to work through some of the many on-line tutorials (Google has 'em!), or both.

    There is so much more to C to be learned. Come on back when you have a specific problem with some exercise, or assignment. What you want to modify and how you want to modify it, are all important considerations to be sorted out.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    How to copy data from an existent file to a new file inserting a line numbers at the begining of each line?
    The original file are paragraphs and I have to copy the paragraphs into another file inserting a line of numbers on each line.

    Example:

    original file

    ibvevbelfvbeflvhbefvbh
    uedvheunv
    undojvndfojvndfjvndfjvndfj vdfjnvdjfvn dfvjndfv dfvlndfvjdfvn

    dfvjefvnef
    efgioergerngenfg


    copy of the file into the new one

    1. ibvevbelfvbeflvhbefvbh
    2. uedvheunv
    3. undojvndfojvndfjvndfjvndfj vdfjnvdjfvn dfvjndfv dfvlndfvjdfvn
    4.
    5. dfvjefvnef
    6. efgioergerngenfg

    and so forth

    thanks,
    Last edited by dallo07; 12-04-2008 at 10:07 PM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    thanks ,,

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    HI
    Can you review the question again and let me know if you can help ?
    thanks,

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sometimes you get what you asked for:

    Code:
    #include <stdio.h>	// for "fprintf" and "fgetc"
    #include <stdlib.h>	// for "exit"
    
    	// function prototypes 
    void fatal (int retv, char *message);
    int streamline (FILE *IN);
    
    char *buffer;		// global buffer
    
    int main (int argc, char *argv[]) {	// argv[1] will be the filename to read
    	FILE *fstRO, *fstOUT;		// argv[2] will be the filename to write
    	int retv, ln=1; // ln is for line number
    	
    	if (argc<2) fatal(-1,"Two filenames required.");	// check for them
    		
    		// open the files
    	if ((fstRO=fopen(argv[1],"ro"))==NULL) fatal(-2,"Can't read from file #1.");
    	if ((fstOUT=fopen(argv[2],"w"))==NULL) fatal(-2,"Can't write out to file #2.");
    
    	while ((retv=streamline(fstRO))>0) {		// read from file
    		fprintf(fstOUT, "&#37;d. %s\n",ln,buffer); 	// write to file
    		ln++; 	// increment
    		free(buffer);	// very important since buffer had memory allocated in streamline()
    	}
    	
    	switch (retv) {		// finished reading file
    		case -2: fatal(-3,"Memory allocation failure!"); 	// in case streamline() fails
    		case -1: fprintf(fstOUT, "%d. %s\n",ln,buffer);		// the "special case"
    			break;
    		case 0:	break;
    		default: break;
    	}
    	free(buffer);	// less important since the program is now over	
    		// close the files
    	fclose(fstRO);
    	fclose(fstOUT);
    	return 0;		// THE END
    }
    
    void fatal (int retv, char *message) {
    	puts(message);
    	exit (retv);
    }
    
    int streamline (FILE *IN) {	// returns a string length that INCLUDES the line terminator or 0 for last line
    	int chr, i=0;		// i is for iteration counter
    	buffer=malloc(1);	// begin allocating memory for the line buffer
    	while ((chr=fgetc(IN))!=EOF) {
    		if (buffer==NULL) return -2;		// in case memory allocation fails
    		if (chr=='\n') {	// reached end of line
    			buffer[i]='\0';	// so terminate it
    			return i+1;
    		}
    		buffer[i]=chr;		// copy character into buffer
    		buffer=realloc(buffer,++i+1);	// more memory for next iteration
    	}	// End-Of-File reached
    	if (buffer==NULL) return -1;	// last check
    	if (i>1) {
    		buffer[i]='\0';		// terminate last line if it exists with no newline
    		return -1;		// (a normative "special case")
    	}
    	return 0;		// and return 0 to indicate we're finished
    }
    Compile as "filecopy" and use:
    filecopy filetocopy.txt filetocreat.txt
    edit: there's actually three very minor bugs in this, and Salem found them all (see subsequent posts).
    Last edited by MK27; 12-05-2008 at 01:09 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    MK27's program is present.

    Dallo07 has not written a single line of code.


    MK27, do you see something wrong here?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably a good idea not to mention the bugs then...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Probably a good idea not to mention the bugs then...
    That's because there aren't any!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well obviously, because you can't find them!

    Is your basis of "bug free" down to "it's produced the correct answer with a few tests, and it hasn't crashed"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    And another attack at MK27, this time Salem, lol
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Matus
    And another attack at MK27, this time Salem, lol
    I would hardly call it an attack on anything but MK27's code, but in any case, if you dare to call your code bug free, then it better be bug free.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Well obviously, because you can't find them!
    No, can you?
    Quote Originally Posted by Salem View Post
    Is your basis of "bug free" down to "it's produced the correct answer with a few tests, and it hasn't crashed"?
    No. I would say it appears to run without bugs to me tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well MK27 you will be spoon feeding for a very long time then.

    "Give a man a fish he is fed for a day, teach a man to fish he is fed for life"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread