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
Jun 27 '08
30 1366
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
That's it right there. The ch you used I don't see all of what's going
on in your code. Basically I can't read it all.
Especially down around do.
and then post code that tries to copy a file ***without using a
loop construct***
***By Nick K.***
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf [2048];
FILE *fp_in;
FILE *fp_out;
size_t ch_read;
if ((fp_in = fopen ("as.exe", "rb")) == NULL)
{
fprintf (stderr, "failed to open input file\n");
exit (EXIT_FAILURE);
}
if ((fp_out = fopen ("a.exe", "wb")) == NULL)
{
fprintf (stderr, "failed to open output file\n");
fclose (fp_in);
exit (EXIT_FAILURE);
}
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
&& ferror(fp_in))
{
fprintf (stderr, "read error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
{
fprintf (stderr, "write error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
}
while (ch_read == sizeof(buf));
fclose (fp_in);
fclose (fp_out);
return 0;
}
----
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
&& ferror(fp_in))
{
Here's where I get lost.
Bill
Bill Cunningham wrote:
That's it right there. The ch you used I don't see all of what's
going on in your code. Basically I can't read it all.
Especially down around do.
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
&& ferror(fp_in))
{
Here's where I get lost.
Same here. Most of this is error-checking code that is not so important when
learning.
Without that error code, the main loop comes down to this, where I've
substituted N for sizeof(BUF):
do
{ ch_read = fread (buf, 1, N, fp_in);
fwrite (buf, 1, ch_read, fp_out);
}
while (ch_read == N);
This will read and write blocks of N characters, until either 0 characters
have been read, indicating end-of-file, or 1..N-1 characters have been read,
indicating this was the last (partial) block.
-- Bartc
Bartc said:
<snip>
Most of this is error-checking code that is not so important
when learning.
I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all - and this view is borne out by a huge amount of supposedly
professional code out there in the real world that completely ignores the
idea that anything might ever not quite work out the way the programmer
wants it to.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On 16 Apr, 12:00, "Bartc" <b...@freeuk.co mwrote:
Bill Cunningham wrote:
* *That's it right there. The ch you used I don't see all of what's
going on in your code. Basically I can't read it all.
Especially down around do.
do
* *{
* * * *if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
* * * * * * && *ferror(fp_in))
* * * *{
Here's where I get lost.
Same here. Most of this is error-checking code that is not so important when
learning.
eek!
Without that error code, the main loop comes down to this, where I've
substituted N for sizeof(BUF):
* * do
* * { * ch_read = fread (buf, 1, N, fp_in);
* * * * fwrite (buf, 1, ch_read, fp_out);
* * }
* * while (ch_read == N);
the original post had a piece of p-code
almost identical to the above.
>
This will read and write blocks of N characters, until either 0 characters
have been read, indicating end-of-file, or 1..N-1 characters have been read,
indicating this was the last (partial) block.
--
Nick Keighley
On 16 Apr, 03:07, "Bill Cunningham" <nos...@nspam.c omwrote:
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
* * That's it right there. The ch you used I don't see all of what's going
on in your code. Basically I can't read it all.
Especially down around do.
and then post code that tries to copy a file ***without using a
loop construct***
and I still don't understand why you tried to mange without a loop...
It doesn't have to be a do while (I'm not very fond of 'em)
but you do need a loop
***By Nick K.***
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
* * char buf [2048];
* * FILE *fp_in;
* * FILE *fp_out;
* * size_t ch_read;
* * if ((fp_in = fopen ("as.exe", "rb")) == NULL)
* * {
* * * * fprintf (stderr, "failed to open input file\n");
* * * * exit (EXIT_FAILURE);
* * }
* * if ((fp_out = fopen ("a.exe", "wb")) == NULL)
* * {
* * * * fprintf (stderr, "failed to open output file\n");
* * * * fclose (fp_in);
* * * * exit (EXIT_FAILURE);
* * }
* * do
* * {
* * * * if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
* * * * * * *&& *ferror(fp_in))
* * * * {
* * * * * * fprintf (stderr, "read error\n");
* * * * * * fclose (fp_in);
* * * * * * fclose (fp_out);
* * * * * * exit (EXIT_FAILURE);
* * * * }
* * * * if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
* * * * {
* * * * * * fprintf (stderr, "write error\n");
* * * * * * fclose (fp_in);
* * * * * * fclose (fp_out);
* * * * * * exit (EXIT_FAILURE);
* * * * }
* * }
* * while (ch_read == sizeof(buf));
* * fclose (fp_in);
* * fclose (fp_out);
* * return 0;
}
----
*do
* * {
* * * * if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
* * * * * * *&& *ferror(fp_in))
* * * * {
Here's where I get lost.
ok. I suppose assigning and testing all in one statement
is a bit Cish (though I'm not the onlt perpetrator!).
My code with the write taken out
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) != sizeof(buf)
&& ferror(fp_in))
{
TERMINATE;
}
}
while (ch_read == sizeof(buf))
simplify the if
do
{
ch_read = fread (buf, 1, sizeof(buf), fp_in);
/* terminate the program if too few characters were read
and an error occurred. Note a "short" read could be
simply caused by getting to the end of the file and
that shouldn't be treated as an error */
if ((ch_read != sizeof(buf) && ferror(fp_in))
{
TERMINATE;
}
}
while (ch_read == sizeof(buf)); /* read until we get a short non-
error read */
is that clearer?
or this (no do-while)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf [2048];
FILE *fp_in;
FILE *fp_out;
size_t ch_read;
if ((fp_in = fopen ("as.exe", "rb")) == NULL)
{
fprintf (stderr, "failed to open input file\n");
exit (EXIT_FAILURE);
}
if ((fp_out = fopen ("a.exe", "wb")) == NULL)
{
fprintf (stderr, "failed to open output file\n");
fclose (fp_in);
exit (EXIT_FAILURE);
}
ch_read = sizeof(buf);
while (ch_read == sizeof(buf))
{
ch_read = fread (buf, 1, sizeof(buf), fp_in);
if ((ch_read != sizeof(buf) && ferror(fp_in))
{
fprintf (stderr, "read error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
{
fprintf (stderr, "write error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
}
fclose (fp_in);
fclose (fp_out);
return 0;
}
this makes an extra test
--
Nick Keighley
GOD is REAL
unless a type declaration to the contrary is made
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:5-*************** ******@bt.com.. .
Bartc said:
<snip>
>Most of this is error-checking code that is not so important when learning.
I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all
OK, keep the extended checking for the next lesson then.
The essential checks are verifying the file handles, and checking for
end-of-file, and these are done here.
Other checks are for errors unlikely in a learning situation, but themselves
become essential if this code was put into a library function to be used in
many different circumstances.
Otherwise the code looks overwhelming.
Quote from K&R2 p.164: "Although output errors are rare, they do occur (for
example, if a disk fills up), so a production program should check this as
well". Note the word 'production'.
--
Bart
Bartc said:
<snip>
>
Quote from K&R2 p.164: "Although output errors are rare, they do occur
(for example, if a disk fills up), so a production program should check
this as well". Note the word 'production'.
Yes. I have a great deal of respect for Brian Kernighan, but that doesn't
mean I agree with him about every single thing - and this is one of the
places where I disagree.
When illustrative (i.e. exegetic) code includes full error-checking, the
explanation can be in danger of getting a little lost, so there's a case
for giving two versions of such code: (a) here's the basic idea; (b)
here's how it looks when we do it properly. How else will the student
learn how to deal with the imperfect real world?
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:5-*************** ******@bt.com.. .
I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all - and this view is borne out by a huge amount of supposedly
professional code out there in the real world that completely ignores the
idea that anything might ever not quite work out the way the programmer
wants it to.
I haven't got to do and certainly not switch yet. But I'm getting pretty
good with for and using while without braces. A continuous loop with
basically two decsions. I learned how to open and close a file and use fread
fwrite simply at first without error checking. I knew error checking was
good needed to be learned too so that was the next step. After I was opening
and closing files and writing both binary and text (with fprintf). I added
error checking. Then tried for fgetc and that's where I am now. I personally
would have to agree with Bart. A person just needs to realize at the outset
they will need error checking in real life.
I had to conquer the open /write/close beast then the error checking and
I also notice most people don't use feof() but EOF.
Bill
[snip]
is that clearer?
or this (no do-while)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf [2048];
FILE *fp_in;
FILE *fp_out;
size_t ch_read;
if ((fp_in = fopen ("as.exe", "rb")) == NULL)
{
fprintf (stderr, "failed to open input file\n");
exit (EXIT_FAILURE);
}
if ((fp_out = fopen ("a.exe", "wb")) == NULL)
{
fprintf (stderr, "failed to open output file\n");
fclose (fp_in);
exit (EXIT_FAILURE);
}
ch_read = sizeof(buf);
while (ch_read == sizeof(buf))
{
ch_read = fread (buf, 1, sizeof(buf), fp_in);
if ((ch_read != sizeof(buf) && ferror(fp_in))
{
fprintf (stderr, "read error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
{
fprintf (stderr, "write error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
}
fclose (fp_in);
fclose (fp_out);
return 0;
}
this makes an extra test
That's the way I learned yes that's great! Except I didn't learn with the
while braces that seems to be an option and the sequential reading makes for
good error check I've found out. I use return -1 instead of the exit
macros.
Bill
I put together this code that seems to work great by studying your code
and other code I have saved and been studying .
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *ifp,*ofp;
int a;
if(argc!=3) {
fprintf(stderr, "usage error\n");
return -1;
}
ifp=fopen(argv[1],"rb");
if (ifp==0) {
fprintf(stderr, "input error\n");
return -1;
}
ofp=fopen(argv[2],"wb");
if (ofp==0) {
fprintf(stderr, "output error\n");
return -1;
}
while((a=fgetc( ifp))!=EOF)
fputc(a,ofp);
fclose(ifp);
fclose(ofp);
printf("done\n" );
return 0;
}
A little less overhead without stdlib and using return -1 if that really
matters. It seems to work like cp.
Bill This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stephan Deibel |
last post by:
Hi,
O'Reilly Associates has agreed to print a second volume of Python
Success Stories and I am looking for contributors of new stories.
This booklet will showcase Python in the context of a variety of
successful software projects, explaining why Python was a good
choice. It is used by O'Reilly as a freebie to market its own
books, and it's a great way to get the word out about Python
and your company or project.
|
by: Stephan Deibel |
last post by:
Hi,
I just wanted to let y'all know that the Python Success Stories
collection has nine new additions:
http://www.pythonology.com/success
These 28 stories include significant testimonials that can make
it easier to sell technical decision-makers (i.e., your boss)
on Python.
|
by: Stephan Deibel |
last post by:
Hi,
O'Reilly Associates is going to be printing volume III of the
Python Success Stories series in June and I'm looking for
submissions of new stories.
The stories have been quite valuable for people introducing
Python to new users and companies.
The deadline for me to receive stories for editing is May 1st and
|
by: cmitchell |
last post by:
Hi,
I have daily backups set up for a database, however today i noticed in
the Journal under task history i see under the success column that the
status is expired. Normally the success column says successful or not
successful. Any ideas?
Cheryl
|
by: |
last post by:
hi
i try to malloc 2G size mem . so i wirte code like that.
it failt in freebsd 5.3 and win2k
what's bug in my code ? how can i change it ?
thank all :)
benjiam
| |
by: audipen |
last post by:
Hi,
I trap the 'OnBuildProjConfigDone' and perform some custom enhancement
to the assembly. I check the 'bool Success' parameter before I perform
my step. I go ahead only if 'Success == true"
My question is - Is there any way in which I can indicate success or
failure of my post build step.. 'Success' is an in-parameter, so I cant
set it to false. Also the return type of the handler is void.
|
by: spam.noam |
last post by:
Hello,
What is the convention for writing C functions which don't return a
value, but can fail?
If I understand correctly,
1. PyArg_ParseTuple returns 0 on failure and 1 on success.
2. PySet_Add returns -1 on failure and 0 on success.
Am I correct? What should I do with new C functions that I write?
|
by: yuchang |
last post by:
Hi,
Using the FormView control is very efficient. But I want to do some
action,like showing a success message or redirect to another page, after
inserting, updating and deleting. How do I get these message?
|
by: Nicholas M. Makin |
last post by:
Well I have gone and done it. I started a fight with C/C++ programmers over
whether VB was a good language. They seem to think it is a toy. Now I read
SOMEWHERE that VB was either the most successful language of one of the most
successful languages ever. I can not however find a source to support such a
statment.
I know that VB has lower development costs than C/C++ due to its speed of
development and its memory mannagement, but again I...
|
by: divingIn |
last post by:
Hi,
I have a form that should popup mentioning Success so i am using alert() box but it shows a yellow exclamation(!) which i dont want. Is there any other more intuitive kind of success message box i can use?
TIA
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |