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,
Printable View
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,
See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
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.
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,
thanks ,,
HI
Can you review the question again and let me know if you can help ?
thanks,
Sometimes you get what you asked for:
Compile as "filecopy" and use: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).
MK27's program is present.
Dallo07 has not written a single line of code.
MK27, do you see something wrong here?
Probably a good idea not to mention the bugs then...
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"?
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 Matus
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"