![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 5
| copy data from one file to another 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, |
| dallo07 is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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. |
| Adak is offline | |
| | #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. |
| dallo07 is offline | |
| | #5 |
| Registered User Join Date: Nov 2008
Posts: 5
| thanks ,, |
| dallo07 is offline | |
| | #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, |
| dallo07 is offline | |
| | #7 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| 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, "%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
}
filecopy filetocopy.txt filetocreat.txt edit: there's actually three very minor bugs in this, and Salem found them all (see subsequent posts).
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 12-05-2008 at 01:09 PM. |
| MK27 is offline | |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| MK27's program is present. Dallo07 has not written a single line of code. MK27, do you see something wrong here? |
| Adak is offline | |
| | #9 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #10 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| That's because there aren't any!
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #11 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #12 |
| POeT GuY Join Date: Feb 2008 Location: Bz
Posts: 213
| And another attack at MK27, this time Salem, lol |
| Matus is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #14 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| No, can you? No. I would say it appears to run without bugs to me tho.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #15 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| 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" |
| slingerland3g is offline | |
![]() |
| Tags |
| copy, data, file, mode, modify |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| File transfer- the file sometimes not full transferred | shu_fei86 | C# Programming | 13 | 03-13-2009 12:44 PM |
| gcc link external library | spank | C Programming | 6 | 08-08-2007 03:44 PM |
| Possible circular definition with singleton objects | techrolla | C++ Programming | 3 | 12-26-2004 10:46 AM |
| spell check in C using a dictionary file | goron350 | C Programming | 10 | 11-25-2004 06:44 PM |
| gcc problem | bjdea1 | Linux Programming | 13 | 04-29-2002 06:51 PM |