success! I think

B

Bill Cunningham

I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);
if (a==EOF) {
fclose(ifp);
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.

Bill
 
B

Bartc

Bill Cunningham said:
I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided
to do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);
if (a==EOF) {
fclose(ifp);
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards?

Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!
 
B

Bill Cunningham

Bartc said:
Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!
Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.

Bill
 
R

Richard

Bill Cunningham said:
Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.

Bill

You are trolling aren't you?
 
B

Bill Cunningham

Richard said:
You are trolling aren't you?

Where the hell did that come from? I am doubting that my code is doing
what I wanted and I would like to ask for serious feedback.

Bill
 
R

Richard

Bill Cunningham said:
Where the hell did that come from? I am doubting that my code is doing
what I wanted and I would like to ask for serious feedback.

Bill

Here's some : step through it with a debugger. That will be specific to
your platform properly. Even a casual look at your code should tell you
it doesn't do what you want. Stepping through with a debugger will prove
invaluable since you clearly are a beginner in all aspects of
programming. Rather than take you through all the issues with your code,
try to figure it out yourself first. Go line by line and ask yourself
what it is doing and, as importantly, why.
 
B

Barry Schwarz

I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);

How do you ever get back to read another character.
if (a==EOF) {
fclose(ifp);

If you would learn to indent your code, it might make it much easier
for you to see where you are going wrong.
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on

Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.
fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.

You need a better reference. Find one that describes the purpose of
each parameter of all the standard functions. You can even find free
copies of the draft standard (n1124 and n1256) on the web which will
do this.


Remove del for email
 
B

Barry Schwarz

Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.

Only for some strange definition of work. Your function doesn't
contain any form of looping. It executes fputc and fgetc each exactly
once. How can your function read or write more than one character?


Remove del for email
 
B

Bill Cunningham

Only for some strange definition of work. Your function doesn't
contain any form of looping. It executes fputc and fgetc each exactly
once. How can your function read or write more than one character?

I'm not exactly sure how to use fgetc and fputc. This is the first time
I've put them into code so I'm a little confused on where in the loops to
put them. I've never really used debuggers though I have gdb. I don't think
it takes more than a keaner eye than mine to see the problems though.

Bill
 
B

Bill Cunningham

Barry Schwarz said:
Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.

Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact size
so I don't think fwrite and fread would be the choice here.

Bill
 
B

Bartc

Bill Cunningham said:
Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact
size so I don't think fwrite and fread would be the choice here.

In your original post you gave the impression the code worked and you wanted
comments on the coding standard.

Clearly you hadn't tested the code, unless you tested with a file containing
a single character.

The code below does a file copy byte by byte using fgetc/fputc, using binary
mode, of any type of file.

If you want to translate between text files, mixing binary and text modes on
input and output, that might give some strange effects.

If you want to translate a binary executable file as you indicated in your
post, into text, that is fairly meaningless unless you're talking about some
sort of dump or disassembly, but in that case I'd advise to forget that for
now.

/* Copy input file to output file */
#include <stdio.h>
#include <stdlib.h>

#define inputfile "mv"
#define outputfile "m"

int main(void) {

FILE *ifp, *ofp;
int a;

ifp=fopen(inputfile,"rb");

if (ifp==0){
printf("Can't open input file %s\n",inputfile);
exit (0);
}

ofp=fopen(outputfile,"wb");
if (ofp==0){
printf("Can't open output file %s\n",outputfile);
fclose(ifp);
exit (0);
}

while ((a=fgetc(ifp))!=EOF)
fputc(a,ofp);

fclose(ifp);
fclose(ofp);

}
 
B

Bill Cunningham

Bartc said:
In your original post you gave the impression the code worked and you
wanted comments on the coding standard.

Clearly you hadn't tested the code, unless you tested with a file
containing a single character.

That's what I must've done.
The code below does a file copy byte by byte using fgetc/fputc, using
binary mode, of any type of file.

If you want to translate between text files, mixing binary and text modes
on input and output, that might give some strange effects.

If you want to translate a binary executable file as you indicated in your
post, into text, that is fairly meaningless unless you're talking about
some sort of dump or disassembly, but in that case I'd advise to forget
that for now.

OK
/* Copy input file to output file */
#include <stdio.h>
#include <stdlib.h>

#define inputfile "mv"
#define outputfile "m"

int main(void) {

FILE *ifp, *ofp;
int a;

ifp=fopen(inputfile,"rb");

if (ifp==0){
printf("Can't open input file %s\n",inputfile);
exit (0);
}

ofp=fopen(outputfile,"wb");
if (ofp==0){
printf("Can't open output file %s\n",outputfile);
fclose(ifp);
exit (0);
}

while ((a=fgetc(ifp))!=EOF)
fputc(a,ofp);

fclose(ifp);
fclose(ofp);

}

I appreciate your patience. When I first looked at C for some reason I
became transfixed. I know Basic pretty well but I would like to learn C very
much. Some times I sound confused because much of the time I am. I have
lived in the same town all my life and I pretty much gave up for the most
part on driving because I would start out somewhere and end up on the
opposite side of town. This is the confusion I have to deal with with drugs
that stop my panic attacks.

So I'm not just going to catch onto a language like C. But I will
continue to try but I think I may have a hard road to hoe. Thanks again.

Bill
 
L

lawrence.jones

Bill Cunningham said:
Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact size
so I don't think fwrite and fread would be the choice here.

That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.

-Larry Jones

I think grown-ups just ACT like they know what they're doing. -- Calvin
 
B

Bill Cunningham

That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.
Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.

Bill
 
B

Barry Schwarz

Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.

What makes you think the size of an integer has any significance to
the code you presented?


Remove del for email
 
N

Nick Keighley

    I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
    Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
 FILE *ifp, *ofp;
 int a;
 ifp=fopen("mv","rb");
 ofp=fopen("m","wt");
 a=fgetc(ifp);
 if (a==EOF) {
 fclose(ifp);
 }
 fputc(a,ofp);
 if (a==EOF) {
 fclose(ofp);
 }
   }

    Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.

you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.
 
R

Richard

Bill Cunningham said:
Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.

Bill

Learn to read a man page or a C book or, and I hate to say this, give
up. Personally I think you are trolling.

But if you're not, read your program as a cpu would generally do it -
line by line and statement by statement. Your code and your thinking
seem to bear almost no resemblance to the problem you want to solve.
 
B

Bill Cunningham

you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.

Nick I still have a copy of the file you posted and I am going to keep
it. I think I need to learn more about loops like while. You used an example
of do. Your code was very thorough and precise. I haven't got to do in the
tutorials but I will continue reading and studing your code. Don't think
your post was in vain.

Bill
 
B

Bill Cunningham

What makes you think the size of an integer has any significance to
the code you presented?

I was addressing Lawrence's post concerning fread. It really didn't have
much to do with using fgetc and fputc or the first code I posted.

Bill
 
N

Nick Keighley

you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.

    Nick I still have a copy of the file you posted and I am going to keep
it. I think I need to learn more about loops like while. You used an example
of do. Your code was very thorough and precise. I haven't got to do in the
tutorials but I will continue reading and studing your code. Don't think
your post was in vain.

your current problem seems almost identical to one I
gave a solution for. The only difference is the previous one
used fread/fwrite. I cannot comprehend how you can read my code
and then post code that tries to copy a file ***without using a
loop construct***
 

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

Similar Threads

Working with files 1
Remove repeated words from a file 3
program bug 29
I have a problem in my study 25
How to set Control-L as variable 6
creating a testfile for catv 10
Problem with realloc (I think) 11
comparison error 12

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top