Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6573

Re: writing and copying a text file

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.c
Subject Re: writing and copying a text file
Date 2011-06-21 21:42 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <itr38e$9b7$1@reader1.panix.com> (permalink)
References <41ff0881-2f1b-4462-8b26-28ca698b180f@a11g2000yqm.googlegroups.com>

Show all headers | View raw


In <41ff0881-2f1b-4462-8b26-28ca698b180f@a11g2000yqm.googlegroups.com> lolueec <marvee1981@gmail.com> writes:

> I'm new to C language and I'm trying to copy and write a text file.
> Here is my code - can some one explain why it is not working?

> #include <stdio.h>

> int main()
> {
> int x;
> FILE *handle1, *handle2;

> handle1=fopen("a text file","r+);
> handle2=fopen("a text file to write","w+");

> while( (x=fgetc(handle1) != EOF){
> fputc(x,handle2); // write to the file
> }

> fclose(handle1);
> fclose(handle2);

> }

You've got some syntax errors here.  (Surely your compiler told you about
them?)

You're missing a double-quote on this line:

  handle1=fopen("a text file","r+);

And you're missing a closing parenthesis in this line:

  while( (x=fgetc(handle1) != EOF){

Aside from that the program should work, assuming your platform allows
filenames like "a text file", and assuming that the input file exists
and is readable.

You might want to do some basic errorchecking on your fopen() statements
to make sure that they succeed, or print an error message if they fail.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

writing and copying a text file lolueec <marvee1981@gmail.com> - 2011-06-21 14:22 -0700
  Re: writing and copying a text file John Gordon <gordon@panix.com> - 2011-06-21 21:42 +0000
  Re: writing and copying a text file Ike Naar <ike@sverige.freeshell.org> - 2011-06-21 21:56 +0000
    Re: writing and copying a text file Keith Thompson <kst-u@mib.org> - 2011-06-21 15:52 -0700
      Re: writing and copying a text file Ike Naar <ike@sverige.freeshell.org> - 2011-06-22 06:33 +0000
  Re: writing and copying a text file Shao Miller <sha0.miller@gmail.com> - 2011-06-21 18:02 -0500
  Re: writing and copying a text file Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-22 08:30 +0100
  Re: writing and copying a text file Vinicio Flores <vfloreshdz@gmail.com> - 2011-07-03 20:32 -0600
    Re: writing and copying a text file lawrence.jones@siemens.com - 2011-07-04 12:27 -0400

csiph-web