473,804 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

success! I think

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 1368

"Bill Cunningham" <no****@nspam.c omwrote in message
news:gtvMj.4699 $bx3.1392@trndd c02...
>
"Barry Schwarz" <sc******@dqel. comwrote in message
news:j3******** *************** *********@4ax.c om...
>> 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.

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(input file,"rb");

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

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

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

fclose(ifp);
fclose(ofp);

}

--
Bart
Jun 27 '08 #11

"Bartc" <bc@freeuk.comw rote in message
news:Sh******** *********@text. news.virginmedi a.com...
>
"Bill Cunningham" <no****@nspam.c omwrote in message
news:gtvMj.4699 $bx3.1392@trndd c02...
>>
"Barry Schwarz" <sc******@dqel. comwrote in message
news:j3******* *************** **********@4ax. com...
>>> 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.

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.
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(input file,"rb");

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

ofp=fopen(outpu tfile,"wb");
if (ofp==0){
printf("Can't open output file %s\n",outputfil e);
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
Jun 27 '08 #12
Bill Cunningham <no****@nspam.c omwrote:
>
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
Jun 27 '08 #13

<la************ @siemens.comwro te in message
news:gp******** ****@jones.home ip.net...
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,size of(int),1,fp);

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

Bill
Jun 27 '08 #14
On Mon, 14 Apr 2008 01:41:05 GMT, "Bill Cunningham" <no****@nspam.c om>
wrote:
>
<la*********** *@siemens.comwr ote in message
news:gp******* *****@jones.hom eip.net...
>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,siz eof(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
Jun 27 '08 #15
On 12 Apr, 22:43, "Bill Cunningham" <nos...@nspam.c omwrote:
* * 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.
--
Nick Keighley
Jun 27 '08 #16
"Bill Cunningham" <no****@nspam.c omwrites:
<la************ @siemens.comwro te in message
news:gp******** ****@jones.home ip.net...
>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,size of(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.
Jun 27 '08 #17


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
--
Nick Keighley
Jun 27 '08 #18
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
Jun 27 '08 #19
On 14 Apr, 22:32, "Bill Cunningham" <nos...@nspam.c omwrote:
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***
--
Nick Keighley

Jun 27 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1567
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.
0
1536
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.
0
1325
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
1
2279
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
23
3033
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
1
2277
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.
0
1152
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?
1
2591
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?
45
2233
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...
5
3493
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
0
9716
marktang
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...
0
9596
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,...
0
10356
jinu1996
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...
0
10103
tracyyun
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...
0
9179
agi2029
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...
0
6874
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();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.