8 Replies - 666 Views - Last Post: 29 January 2011 - 05:45 AM Rate Topic: -----

#1 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Copying from 1 file to another in C

Posted 27 January 2011 - 11:55 AM

#include <stdio.h>
 main()
  {

    char Row[80];

    FILE *SF, *VF;

    SF = fopen("Input.txt","r");

    VF = fopen("Output.txt","w");

    fgets(Row, 80, SF);

    fprintf(VF, "%s", Row);

    fclose(VF);

    fclose(SF);

    system("pause");

  }


Can any1 tell me how can I copy more than just one row? I wrote this based on my material that my professor gave me, but there is no info about how can I copy more than one row.

Is This A Good Question/Topic? 0
  • +

Replies To: Copying from 1 file to another in C

#2 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Copying from 1 file to another in C

Posted 27 January 2011 - 11:59 AM

See the fread and fwrite functions.
Alternatively, you could still copy the whole file line by line by using loops.

This post has been edited by diego_pmc: 27 January 2011 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Copying from 1 file to another in C

Posted 27 January 2011 - 01:14 PM

Try something like this:

while(!fgets(Row, 80, SF))
{
  fprintf(VF, "%s", Row);
}


My C is a little rusty but that should read until the end of the file and write the contents to VF.
Was This Post Helpful? 0
  • +
  • -

#4 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 03:06 AM

View Posteker676, on 27 January 2011 - 01:14 PM, said:

Try something like this:

while(!fgets(Row, 80, SF))
{
  fprintf(VF, "%s", Row);
}


My C is a little rusty but that should read until the end of the file and write the contents to VF.

Unfortunately it does not...
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 04:38 AM

If your goal is simply to copy a file, even just a text file, ignore string function. Basically, you want to read all the data and write all the data.

If you do it a byte at a time, you can do this:
#include <stdio.h>
int main() {
	FILE *SF, *VF;

	SF = fopen("Input.txt","rb"); /* note the "b" */
	VF = fopen("Output.txt","wb");
	while (!feof(SF)) {
		fputc ( (fgetc(SF), VF);
	}
	fclose(VF);
	fclose(SF);

	return 0
}



For extra credit, set up a buffer and use fread and fwrite to grab binary chunks instead of just bytes.
Was This Post Helpful? 1
  • +
  • -

#6 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 05:25 AM

View Postbaavgai, on 29 January 2011 - 04:38 AM, said:

If your goal is simply to copy a file, even just a text file, ignore string function. Basically, you want to read all the data and write all the data.

If you do it a byte at a time, you can do this:
#include <stdio.h>
int main() {
	FILE *SF, *VF;

	SF = fopen("Input.txt","rb"); /* note the "b" */
	VF = fopen("Output.txt","wb");
	while (!feof(SF)) {
		fputc ( (fgetc(SF), VF);
	}
	fclose(VF);
	fclose(SF);

	return 0
}



For extra credit, set up a buffer and use fread and fwrite to grab binary chunks instead of just bytes.


It says there is a syntax error before ; token in line 8
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 05:27 AM

Do the parentheses match up here?

fputc ( (fgetc(SF), VF);

Was This Post Helpful? 1
  • +
  • -

#8 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1635
  • View blog
  • Posts: 3,111
  • Joined: 30-May 10

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 05:41 AM

baavgai's answer at post #5 trips up in using feof() to control a loop.
http://sourceforge.n....php?title=Feof

feof() returns the state of a past event (say a file read). It is not a measure of the current state, nor a prediction of a future state.
If you have a 10 byte file, and you read 10 characters, you will be at the end of the file, but feof() will still be false. It is the 11'th read that sets the state (and the 11'th read will also return it's own EOF state as well).

int c;
while ( (c=fgetc(SF)) != EOF ) {
  fputc ( c, VF);
}


Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Copying from 1 file to another in C

Posted 29 January 2011 - 05:45 AM

I suspected the feof would bork, but the best way to find out is to try. The missing syntax was unintentional, however.

Honestly, I'd generally used fread for such an application. The function returns number of bytes read, so you needn't worry about the messy before/after logic of EOF checks.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1