I have a problem in my study

Q

qazwsx746

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);
if (argc==1)
filecopy(stdin,stdout);
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}
 
C

Cong Wang

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);
if (argc==1)
filecopy(stdin,stdout);
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}

Well, first, if you know function main is int. Add the type befor its
name. Do NOT leave that position blank. Second, 'stdin' doesn't has an
EOF until you press Ctrl+d, thus you can't exit.
 
R

Richard Heathfield

Cong Wang said:
I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);
if (argc==1)
filecopy(stdin,stdout);
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}

Well, first, if you know function main is int. Add the type befor its
name. Do NOT leave that position blank.

That's a minor style point, and not his problem.
Second, 'stdin' doesn't has an
EOF until you press Ctrl+d, thus you can't exit.

Wrong.
 
M

mark_bluemel

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

I don't like the formatting of your code, but I can't see an obvious
fatal flaw.

With more braces (cos' I wanted to see what I was doing) and some
tidied indentation, it worked fine on my system.

A better explanation of what your problem was may help - do you mean
that if you just ran your program with no arguments, you couldn't exit
from it? If so, do you know what you need to type on your platform to
indicate EOF on stdin?
 
C

Cong Wang

Richard said:
Cong Wang said:
I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);
if (argc==1)
filecopy(stdin,stdout);
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}

Well, first, if you know function main is int. Add the type befor its
name. Do NOT leave that position blank.

That's a minor style point, and not his problem.

But I think it's important. ;-p

Well, I forgot he used Window$. In fact, Win uses <Ctrl-Z><CR> as EOF.
I am sorry, I can't afford a Windows and know little about it. ;-(
 
A

Andrew Poelstra

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
Well, first off, it's best to be explicit about what main() returns:
int main(int argc, char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);

You can't have a prototype within another function. Move this to the
top of your program or into a header file.
if (argc==1)
filecopy(stdin,stdout);

Please use spaces liberally; horizontal space is free in this
particular piece of code, and it makes things much easier to read.
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}

Looks good, except that 1 is not a portable return value from main().
#include said:
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}

That looks okay as well, although I strongly suggest that you fix your
formatting. As an example, here's your last function:

void filecopy(FILE *ifp, FILE *ofp)
{
int c;
while ((c = getc(ifp)) != EOF)
putc(c,ofp);
}


I believe your problem lies in the fact that you aren't inputting EOF
to your program. On DOS this would be Ctrl+Z, and on UNIX, it would
be Ctrl+D.
 
Y

yuyang.sz

-------------------------------------------------------------------------
#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);

if (argc==1)
{
printf("No File!!!");
return;
}
else
while (--argc>0)
if ((fp=fopen(*++argv,"r"))==NULL) {
printf("cat:can't open %s\n",*argv);
return 1;
} else {
filecopy(fp,stdout);
fclose(fp);
}
return 0;
}
void filecopy(FILE *ifp,FILE *ofp)
{int c;
while ((c=getc(ifp))!=EOF)
putc(c,ofp);
}
--------------------------------------------------------------------------
 
H

Herbert Rosenau

You can't have a prototype within another function. Move this to the
top of your program or into a header file.

Not true. That is absolutely legal. The visibility of the prototye is
restricted to the function only but that's all.
Please use spaces liberally; horizontal space is free in this
particular piece of code, and it makes things much easier to read.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
M

Mark McIntyre

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);

its normal to declare function prototypes outside main. C doesn't have
the idea of local function definitions.
while ((c=getc(ifp))!=EOF)

how do you signal EOF? Remember, its not a character, its a signal.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

Herbert Rosenau

I use operating systems of windows xp ,
and compiler is trubo c 3.0
when I input data,the following program can't be exited.
what happen to this program?
Is the wrong with the compiler?

#include <stdio.h>
main(int argc,char *argv[])
{ FILE *fp;
void filecopy(FILE *,FILE *);

its normal to declare function prototypes outside main. C doesn't have
the idea of local function definitions.

No. It is normal to declare a prototype where it gets used. That
includes it inside a function.

Yes, you would declare all prototypes outside any function - but when
you are sure that you needs it only inside one function it can be a
good idea to hide that from any else.
how do you signal EOF? Remember, its not a character, its a signal.

Hey, the OP had declared c as int - and getc() does not return char
but int. Read your book what getc returns - an char wided to int or
EOF.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
R

Richard Heathfield

Herbert Rosenau said:

Hey, the OP had declared c as int - and getc() does not return char
but int.

Yes, but I think you'll find he was talking about how to indicate
end-of-file from a keyboard.
Read your book what getc returns - an char wided to int or
EOF.

s/char/unsigned char/
 
M

Mark McIntyre

No. It is normal to declare a prototype where it gets used. That
includes it inside a function.

Don't be ridiculous. If I didn't know better, I'd say you were a
troll.
Hey, the OP had declared c as int - and getc() does not return char
but int. Read your book what getc returns - an char wided to int or
EOF.

Perhaps you want to learn to read. Where did I say getc returned a
char?

Next time, as DP would say, engage your brain before posting.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

Herbert Rosenau

Herbert Rosenau said:



Yes, but I think you'll find he was talking about how to indicate
end-of-file from a keyboard.


s/char/unsigned char/
Grmpf, I tell my compilers always to use unsigned char for char as I
don't like signed char in any way, so I forget about signed char
except on the comiler flags or rareley when I declare them specially
as such for special usage.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
H

Herbert Rosenau

Don't be ridiculous. If I didn't know better, I'd say you were a
troll.


Perhaps you want to learn to read. Where did I say getc returned a
char?

Next time, as DP would say, engage your brain before posting.
You sayed getc() does not return EOF what is clearly false.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
F

Flash Gordon

Herbert said:
You sayed getc() does not return EOF what is clearly false.

No, Mark asked how you signal EOF and said to remember it is not a
character, he did not says that getc does not return it.
 
K

Keith Thompson

Herbert Rosenau said:
You sayed getc() does not return EOF what is clearly false.

Herbert, I don't see where anyone said that "getc() does not return
EOF", at least not in what's quoted here. There were some
misstatements, but not that one. (I won't bother to enumerate them.)

The question Mark was addressing above was, if I recall correctly, the
problem that the OP was actually having: how does someone running the
program trigger an end-of-file condition? The answer is typically to
type control-D or control-Z at the keyboard, if the program's stdin is
coming from a keyboard. I think you misunderstood that point.

I suspect that you and Mark both understand all of this, and are
basically in agreement, but one or both of you got caught up on the
wording.
 
M

Mark McIntyre

You sayed getc() does not return EOF what is clearly false.

I said nothing of the sort. I said
"how do you signal EOF? Remember, its not a character, its a signal. "

Or do you claim that EOF is a character ? Thats a classic newby
mistake.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

Herbert Rosenau

Or do you claim that EOF is a character ? Thats a classic newby
mistake.

EOF is an int.

As C knows nothing about devices such as keyboard, screen, printers,
plotters, punch cards and so on there is no standard way you can do
from outside C to identify what ever may result in an EOF except to
read the documentation of your OS. The C runtime will ever deliver EOF
to your program when it sees that the stream is _behind_ the data it
holds.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top