Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > read and write columns

Reply
Thread Tools

read and write columns

 
 
Bill Cunningham
Guest
Posts: n/a
 
      07-03-2008
I don't think I can do this without some help or hints. Here is the code
I have.

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

double input(double input) {
int count=0,div=0;
double mean=0,linput=0;
FILE *fpr, *fpw;
mean=input+linput/count;
if ((fpw=fopen("data","w"))!=EOF);
if ((fpr=fopen("data","r"))!=EOF);
if ((fscanf(fpr,input,linput,mean))==0;
fprintf(fpw,"%d\t%d\t%d\n",input,input+linput,mean );
}

It's very much incomplete. I have two stream open one for reading and one
for writing but the count that I have been speaking of, I don't know how to
increment it.

Bill


 
Reply With Quote
 
 
 
 
rahul
Guest
Posts: n/a
 
      07-03-2008
On Jul 3, 5:12 am, "Bill Cunningham" <(E-Mail Removed)> wrote:
> I don't think I can do this without some help or hints. Here is the code
> I have.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double input(double input) {
> int count=0,div=0;
> double mean=0,linput=0;
> FILE *fpr, *fpw;


> mean=input+linput/count;

What exactly do you mean? input + (linput / count) or (input +
linput) / count. Do you see a divide by zero problem here? Since input
and linput are define as double, your mean equals infinity.

> if ((fpw=fopen("data","w"))!=EOF);
> if ((fpr=fopen("data","r"))!=EOF);

May be next time you should remember fopen returns FILE * and the
error checking is done against NULL.
So if ((fpw = fopen("data", "w")) != NULL);
But heck; even now it is meaning-less(that ;, if you didn't notice).
Check for NULL and then do nothing - just proceed with the code.

> if ((fscanf(fpr,input,linput,mean))==0;

Again, check for error and do nothing. And this won't compile on any
known compiler(missing parenthesis).

> fprintf(fpw,"%d\t%d\t%d\n",input,input+linput,mean );
> }

input, input + linput are doubles. mean is a double and equals
infinity. %d, if you have a look at the doc, is meant for signed
integers.

> It's very much incomplete. I have two stream open one for reading and one
> for writing but the count that I have been speaking of, I don't know how to
> increment it.

I don't know but it looks like to me that you created this mess on
purpose. There are better ways to have fun if that is what you are
trying to have over here.


 
Reply With Quote
 
 
 
 
rahul
Guest
Posts: n/a
 
      07-03-2008
On Jul 3, 5:12 am, "Bill Cunningham" <(E-Mail Removed)> wrote:
> I don't think I can do this without some help or hints. Here is the code
> I have.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double input(double input) {

Though it is legal, naming the parameter the same as your function
name is not a good practice.
 
Reply With Quote
 
Ron Ford
Guest
Posts: n/a
 
      07-03-2008
On Thu, 03 Jul 2008 04:57:37 +0000, Richard Heathfield posted:

> Barry Schwarz said:
>
>> On Thu, 03 Jul 2008 00:12:03 GMT, "Bill Cunningham" <(E-Mail Removed)>
>> wrote:
>>
>>> I don't think I can do this without some help or hints. Here is the
>>> code
>>>I have.
>>>
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>
>>>double input(double input) {
>>> int count=0,div=0;

>>
>> White space is free. Use it make your code readable.
>>
>>> double mean=0,linput=0;
>>> FILE *fpr, *fpw;
>>> mean=input+linput/count;

>>
>> What is the value of count? Are you allowed to divide by that value?

>
> Good spot, Barry. I missed that completely.
>
>>> if ((fpw=fopen("data","w"))!=EOF);

>>
>> Didn't your compiler generate a diagnostic here?

>
> Just a thought here - Bill Cunningham is a self-confessed slow learner, so
> it might be quicker for us and more productive for him if we just point
> out the *first* mistake. Maybe over time BC will begin to understand that
> he's not going to get help with problem B for so long as his code
> continues to exhibit problem A, and he might - just *might* - work out
> that eliminating problem A from all his code is therefore a productive
> thing for him to do.



I think this idiom is largely unnecessary:

>>> if ((fpw=fopen("data","w"))!=EOF);


If every char you put has to be tested for EOF, then you have a deficient
OS. I gave windows 40 gigs today. It prompts me annoyingly for lack of
hog space.

That the C programming language must be constarined to the average clicker
is a distortion.
--
Ron Ford
 
Reply With Quote
 
Ron Ford
Guest
Posts: n/a
 
      07-03-2008
On Thu, 03 Jul 2008 09:27:48 +0000, Richard Heathfield posted:

> Ron Ford said:
>
> <snip>
>>
>>
>> I think this idiom is largely unnecessary:
>>
>>>>> if ((fpw=fopen("data","w"))!=EOF);

>
> It isn't an idiom. It's just a bug.


When I have to do IO with C, I use safegets, but I don't think that that is
what you mean.

My point was that my OS doesn't say nay to me on a char basis.

I'm curious what you mean.
--
Ron Ford
"Close enough to Texas to feel disinformation."
 
Reply With Quote
 
Ron Ford
Guest
Posts: n/a
 
      07-03-2008
On Thu, 03 Jul 2008 10:11:47 +0000, Richard Heathfield posted:

> Ron Ford said:
>
>> On Thu, 03 Jul 2008 09:27:48 +0000, Richard Heathfield posted:
>>
>>> Ron Ford said:
>>>
>>> <snip>
>>>>
>>>>
>>>> I think this idiom is largely unnecessary:
>>>>
>>>>>>> if ((fpw=fopen("data","w"))!=EOF);
>>>
>>> It isn't an idiom. It's just a bug.

>>
>> When I have to do IO with C, I use safegets, but I don't think that that
>> is what you mean.
>>
>> My point was that my OS doesn't say nay to me on a char basis.
>>
>> I'm curious what you mean.

>
> The fopen function never returns EOF, ever. Since EOF is an int, and since
> fopen returns a FILE *, testing the return value of fopen against EOF is
> completely pointless. (In this case, the semicolon makes it doubly
> pointless.)


I see. What I was getting at was more like:
putchar(anything)!=EOF ....

The test against anything for a putchar call seems prudent but ends up
being a lot of extra keystrokes and code.

EOF has alwyas been negative one on my impkementation.
--
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      07-03-2008
In article <u8id6ivuzwpm$(E-Mail Removed)>,
Ron Ford <(E-Mail Removed)> wrote:

>I see. What I was getting at was more like:
>putchar(anything)!=EOF ....
>
>The test against anything for a putchar call seems prudent but ends up
>being a lot of extra keystrokes and code.


Depending on the application, it may be reasonable just to use
ferror() to test the stream at the end of the output, or at other
natural times such as before reading input. But be sure to also check
the return value of fclose(): buffering both within the stdio library
and in the operating system may mean that errors are not detected
until the file is closed.

>EOF has alwyas been negative one on my impkementation.


EOF must be a negative int.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-03-2008
Ron Ford <(E-Mail Removed)> wrote:

> I see. What I was getting at was more like:
> putchar(anything)!=EOF ....


You may have tried to get at that, but since it wasn't in the OP's
posted code, it would be hard to reach it.

> The test against anything for a putchar call seems prudent but ends up
> being a lot of extra keystrokes and code.


A lot? Rather an exaggeration, isn't it? If you do it right, that check
for EOF will double as your loop termination condition, which you would
have needed anyway.

> EOF has alwyas been negative one on my impkementation.


That's as may be, and it is indeed by far the most common value, but all
the Standard demands is that EOF is negative. Besides, EOF is both
clearer, and only a single character longer, than -1.

Richard
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-03-2008
On 3 Jul, 01:12, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * I don't think I can do this without some help or hints. Here is the code
> I have.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double input(double input) {
> * * * *int count=0,div=0;
> * * * *double mean=0,linput=0;
> * * * *FILE *fpr, *fpw;
> * * * *mean=input+linput/count;
> * * * *if ((fpw=fopen("data","w"))!=EOF);
> * * * *if ((fpr=fopen("data","r"))!=EOF);
> * * * *if ((fscanf(fpr,input,linput,mean))==0;
> * * * *fprintf(fpw,"%d\t%d\t%d\n",input,input+linput,mean );
> * * * *}
>
> It's very much incomplete. I have two stream open one for reading and one
> for writing but the count that I have been speaking of, I don't know how to
> increment it.


WHAT ARE YOU TRYING TO DO

--
Nick Keighley
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-03-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> I don't think I can do this without some help or hints. Here is the code
> I have.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double input(double input) {
> int count=0,div=0;
> double mean=0,linput=0;
> FILE *fpr, *fpw;
> mean=input+linput/count;
> if ((fpw=fopen("data","w"))!=EOF);
> if ((fpr=fopen("data","r"))!=EOF);
> if ((fscanf(fpr,input,linput,mean))==0;
> fprintf(fpw,"%d\t%d\t%d\n",input,input+linput,mean );
> }
>
> It's very much incomplete. I have two stream open one for reading and one
> for writing but the count that I have been speaking of, I don't know how to
> increment it.


Look up the word "increment" in your C textbook.

I thought you were going to run your code through "indent" before
posting it.

You have no "main" function.

The code you posted doesn't compile. Did you even try compiling it
before posting?

You attempt to open the same file, "data", for writing and reading,
using two separate streams. This makes no sense.

You have 3 if statements. You don't do anything in any of them. Just
how do you think an if statement works?

Why are you comparing the result of fopen() to EOF? Read the
documentation for fopen(). Don't guess.

You're calling fscanf() with four arguments: a FILE* (good) and three
doubles. Read the documentation for fscanf. For the umpteenth time,
don't guess.

You haven't said what the code is supposed to do. For the umpteenth
time, state the problem you're trying to solve. If you haven't done
that, writing even a single line of code is a waste of time.

This appears to be similar in intent to something that was discussed
at length in another thread, but I'm not going to assume that there's
any correlation; you never offered a decent problem definition in that
thread either.

In the absence of any problem definition, here's a correct program
that meets all the requirements you've stated:

int main(void)
{
}

If you can tell us what functionality is missing from that program,
*maybe* we can start to help you.

But I doubt it.

I honestly think you need to go back a few steps. Pick a simpler
problem to solve. Define the problem clearly in plain English. Then,
and only then, try to write a program to solve the problem. Never
write a call to a library function without first understanding what
arguments the function expects, what result it returns, and what each
and every argument and result means. This requires reading the
documentation.

Get a copy of K&R2 if you don't already have one. Start at the
beginning and work your way through it. Do all the exercises.

--
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
I need to write Simple JAVA program to read and write from USB serialto use it with Arduino sahm Java 4 10-15-2012 06:24 PM
Read CSV - string Columns - Int columns =?Utf-8?B?YmVub2l0?= ASP .Net 0 05-08-2006 02:11 PM
database columns vs. gui columns and sorting ittay.dror@gmail.com Java 5 03-04-2006 10:48 AM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM
Columns and Inherited Datagrid...Active Schema does not support columns rob thomson ASP .Net Datagrid Control 0 09-04-2003 03:09 PM



Advertisments