C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-04-2008, 08:37 PM   #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,
dallo07 is offline   Reply With Quote
Old 12-04-2008, 09:11 PM   #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   Reply With Quote
Old 12-04-2008, 09:15 PM   #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   Reply With Quote
Old 12-04-2008, 10:04 PM   #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   Reply With Quote
Old 12-04-2008, 10:08 PM   #5
Registered User
 
Join Date: Nov 2008
Posts: 5
thanks ,,
dallo07 is offline   Reply With Quote
Old 12-04-2008, 10:09 PM   #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   Reply With Quote
Old 12-05-2008, 02:28 AM   #7
subminimalist
 
MK27's Avatar
 
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
}
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).
__________________

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   Reply With Quote
Old 12-05-2008, 03:06 AM   #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   Reply With Quote
Old 12-05-2008, 10:20 AM   #9
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 12-05-2008, 10:40 AM   #10
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Salem View Post
Probably a good idea not to mention the bugs then...
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   Reply With Quote
Old 12-05-2008, 10:43 AM   #11
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 12-05-2008, 11:12 AM   #12
POeT GuY
 
Matus's Avatar
 
Join Date: Feb 2008
Location: Bz
Posts: 213
And another attack at MK27, this time Salem, lol
__________________
PoEms R InsPiRatiOns of LIfE ExpErienCes!!

Matus is offline   Reply With Quote
Old 12-05-2008, 11:15 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
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.
__________________
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   Reply With Quote
Old 12-05-2008, 11:23 AM   #14
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 11:23 AM   #15
Registered User
 
slingerland3g's Avatar
 
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   Reply With Quote
Reply

Tags
copy, data, file, mode, modify

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22