Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stream io in c

Reply
Thread Tools

stream io in c

 
 
Richard Bos
Guest
Posts: n/a
 
      07-31-2008
Ron Ford <(E-Mail Removed)> wrote:

> On Thu, 31 Jul 2008 01:26:25 +0000, Richard Heathfield posted:
>
> > Ron Ford said:


> >> FILE *fp;
> >> char name[]="text58.txt";
> >>
> >> fp=&name;


> > question. What on earth do you think this assignment means? Why did you
> > type it? (Answering that question might be the most important way you can
> > learn from this thread.)

>
> LHS is a pointer. Since I could not create a sensical thingamajig of type
> FILE--everything I tried was a syntax error--I tried chars. There's no
> guarantees on pointers like this; why would there be a prohibition?


That's a nice example of magical thinking. "This looks, in one trivial
detail, like that; therefore, this should work, in all essentials, like
that." No. A pointer to FILE and a pointer to array of char are quite
different, and what's much more important, a FILE and an array of char
are very different. If you want to be a programmer, you must learn to
make sure that you _know_, rather than guess.

Richard
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-31-2008
Ron Ford wrote:

<snip>

> Thanks, Keith, I seem to be doing much better:
>
>
> #include <stdio.h>
> int main(void)
> {
> FILE *fp;
> char name[]="text58.txt";


If you are using tabs to indent your code, consider switching to spaces,
since tabs are often stripped out by Usenet software.

> int c;
>
> if ((fp = fopen(name, "wb")) == NULL)
> {
> printf("can't open %s\n", fp);


The 's' type specifier expects a char* argument that must point at a
string. You have supplied it a FILE* argument, a certain route to
undefined behaviour.

> return 1;


And one is not a portable return value. The portable values are 0,
EXIT_SUCCESS and EXIT_FAILURE.

Surely you must have noted these elementary things as they are often
mentioned in this group, besides being in it's FAQ.

> }
> else
> {
> for (c = 0; c <= 255; c ++) {
> putc(c, fp);


Since you are writing raw byte values, you might consider changing the
misleading filename extension.

Also the maximum value of an unsigned byte in C is given by UCHAR_MAX.
It is not necessarily 256. And note that 'c' is declared as an int, not
unsigned char.

> }
> fclose(fp);
> }
> return 0;
> }
> // gcc -o chars mkchars2.c


This command is not a conforming implementation of ISO C. For that you
need:

gcc -ansi -pedantic /* For conformance to C90 */
gcc -std=c99 -pedantic /* For incomplete but good conformance to C99
*/

Also add the -Wall and -W flags for extra diagnostics which are always a
help.

<snip>

 
Reply With Quote
 
 
 
 
Ron Ford
Guest
Posts: n/a
 
      07-31-2008
On Thu, 31 Jul 2008 15:04:37 +0530, santosh posted:

>> // gcc -o chars mkchars2.c

>
> This command is not a conforming implementation of ISO C. For that you
> need:
>
> gcc -ansi -pedantic /* For conformance to C90 */
> gcc -std=c99 -pedantic /* For incomplete but good conformance to C99
> */
>
> Also add the -Wall and -W flags for extra diagnostics which are always a
> help.


//mkchars.c:



#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *fp;
char name[]="text62.txt";


int c;

if ((fp = fopen(name, "wb")) == NULL)
{
printf("can't open %s\n", fp);
return EXIT_FAILURE;
}
else
{
for (c = 0; c <= 255; c ++) {
putc(c, fp);
}
fclose(fp);
}
return 0;
}
// gcc -o chars -std=c99 -pedantic mkchars3.c

--
When a new source of taxation is found it never means, in practice, that
the old source is abandoned. It merely means that the politicians have two
ways of milking the taxpayer where they had one before. 8
H. L. Mencken
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-31-2008
Ron Ford wrote:

> On Thu, 31 Jul 2008 15:04:37 +0530, santosh posted:
>
>>> // gcc -o chars mkchars2.c

>>
>> This command is not a conforming implementation of ISO C. For that
>> you need:
>>
>> gcc -ansi -pedantic /* For conformance to C90 */
>> gcc -std=c99 -pedantic /* For incomplete but good conformance to
>> C99
>> */
>>
>> Also add the -Wall and -W flags for extra diagnostics which are
>> always a help.

>
> //mkchars.c:
>
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> FILE *fp;
> char name[]="text62.txt";
>
>
> int c;
>
> if ((fp = fopen(name, "wb")) == NULL)
> {
> printf("can't open %s\n", fp);


Can you spot the mistake in this printf call?

> return EXIT_FAILURE;
> }
> else
> {
> for (c = 0; c <= 255; c ++) {
> putc(c, fp);
> }
> fclose(fp);
> }
> return 0;
> }
> // gcc -o chars -std=c99 -pedantic mkchars3.c


If you had added the -Wall and -W flags to gcc, it would have warned you
of your botch-up with printf.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-31-2008
On 31 Jul, 02:10, Ron Ford <(E-Mail Removed)> wrote:
> On Tue, 29 Jul 2008 19:49:54 -0600, Ron Ford posted:
>
> > I think the solution in c could be as easy as instantiating a loop from
> > zero to 255, putchar'ing in the body of the loop, then redirecting output
> > to a file. *If the creation of the file comes from the source, I suspect it
> > would have to be in 'rb' mode.

>
> I've tried some variations with this.
>
> int i;
> for (i = 0; i <= UCHAR_MAX; i++) putchar (i);


for (i = 0; i < UCHAR_MAX; i++) putchar (i);

<snip>

--
Nick Keighley

It's always September. But sometimes it is _more_ September.
Jim Cameron (clc)
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-31-2008
Nick Keighley <(E-Mail Removed)> writes:

> On 31 Jul, 02:10, Ron Ford <(E-Mail Removed)> wrote:
>> On Tue, 29 Jul 2008 19:49:54 -0600, Ron Ford posted:
>>
>> > I think the solution in c could be as easy as instantiating a loop from
>> > zero to 255, putchar'ing in the body of the loop, then redirecting output
>> > to a file. *If the creation of the file comes from the source, I suspect it
>> > would have to be in 'rb' mode.

>>
>> I've tried some variations with this.
>>
>> int i;
>> for (i = 0; i <= UCHAR_MAX; i++) putchar (i);

>
> for (i = 0; i < UCHAR_MAX; i++) putchar (i);


I suspect a joke, but I'm not getting it. Can you labour the point
for the rest of us?

--
Ben.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-31-2008
Ben Bacarisse <(E-Mail Removed)> writes:

> Nick Keighley <(E-Mail Removed)> writes:
>
>> On 31 Jul, 02:10, Ron Ford <(E-Mail Removed)> wrote:
>>> On Tue, 29 Jul 2008 19:49:54 -0600, Ron Ford posted:
>>>
>>> > I think the solution in c could be as easy as instantiating a loop from
>>> > zero to 255, putchar'ing in the body of the loop, then redirecting output
>>> > to a file. *If the creation of the file comes from the source, I suspect it
>>> > would have to be in 'rb' mode.
>>>
>>> I've tried some variations with this.
>>>
>>> int i;
>>> for (i = 0; i <= UCHAR_MAX; i++) putchar (i);

>>
>> for (i = 0; i < UCHAR_MAX; i++) putchar (i);

>
> I suspect a joke, but I'm not getting it. Can you labour the point
> for the rest of us?


I will hazard he meant:

,----
| for (int i = 0; i < UCHAR_MAX; i++) putchar (i);
`----

as in C99.

Although frankly I think it#s horrible layout. Hard to debug. Hard to
see the body.

,----
| for (int i = 0; i < UCHAR_MAX; i++)
| putchar (i);
`----

....


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-31-2008
Richard<(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> writes:
>
>> Nick Keighley <(E-Mail Removed)> writes:
>>
>>> On 31 Jul, 02:10, Ron Ford <(E-Mail Removed)> wrote:
>>>> On Tue, 29 Jul 2008 19:49:54 -0600, Ron Ford posted:
>>>>
>>>> > I think the solution in c could be as easy as instantiating a loop from
>>>> > zero to 255, putchar'ing in the body of the loop, then redirecting output
>>>> > to a file. *If the creation of the file comes from the source, I suspect it
>>>> > would have to be in 'rb' mode.
>>>>
>>>> I've tried some variations with this.
>>>>
>>>> int i;
>>>> for (i = 0; i <= UCHAR_MAX; i++) putchar (i);
>>>
>>> for (i = 0; i < UCHAR_MAX; i++) putchar (i);

>>
>> I suspect a joke, but I'm not getting it. Can you labour the point
>> for the rest of us?

>
> I will hazard he meant:
>
> ,----
> | for (int i = 0; i < UCHAR_MAX; i++) putchar (i);
> `----
>
> as in C99.


The bit I am not getting is changing test from the correct one to one
that does not do what the OP wants. If the intent was to change the
scope of i, why change the test as well?

--
Ben.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      07-31-2008
Nick Keighley wrote:
> Ron Ford <(E-Mail Removed)> wrote:
>

.... snip ...
>
>> I've tried some variations with this.
>>
>> int i;
>> for (i = 0; i <= UCHAR_MAX; i++) putchar (i);

>
> for (i = 0; i < UCHAR_MAX; i++) putchar (i);


No, the original is correct. He wants to write out the entire
range available to the char type.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2008
Ron Ford <(E-Mail Removed)> writes:
> On Thu, 31 Jul 2008 15:04:37 +0530, santosh posted:
>
>>> // gcc -o chars mkchars2.c

>>
>> This command is not a conforming implementation of ISO C. For that you
>> need:
>>
>> gcc -ansi -pedantic /* For conformance to C90 */
>> gcc -std=c99 -pedantic /* For incomplete but good conformance to C99
>> */
>>
>> Also add the -Wall and -W flags for extra diagnostics which are always a
>> help.

>
> //mkchars.c:
>
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> FILE *fp;
> char name[]="text62.txt";
>
>
> int c;
>
> if ((fp = fopen(name, "wb")) == NULL)
> {
> printf("can't open %s\n", fp);
> return EXIT_FAILURE;
> }
> else
> {
> for (c = 0; c <= 255; c ++) {
> putc(c, fp);
> }
> fclose(fp);
> }
> return 0;
> }
> // gcc -o chars -std=c99 -pedantic mkchars3.c


Compared to the previous version you posted, you added
"#include <stdlib.h>", you changed the file name from "text58.txt" to
"text62.txt", and you changed "return 1;" to "return EXIT_FAILURE;".

Oh, and you didn't mention what changes you had made; I had to save
both versions and compare them myself.

You ignored the vast majority of santosh's excellent advice.
Specifically:

Your indentation is still non-existent.

You're still trying to print a FILE* value using "%s".

You're still using a misleading file name.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 1 04-17-2007 09:15 AM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments