By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

adding colums to text

P: n/a
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.

Bill
Jun 28 '08 #1
Share this Question
Share on Google+
60 Replies


P: n/a
Bill Cunningham wrote:
I have a row of values like such, placed in a text file by
fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right
beside it
which is a total of these values and then average them in another
column. For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe
freopen.
Fprintf is enough. Print the first entry of the first column, print a
tab, then print the total, another tab, then the average, then a
newline, your next value for the first column, a newline, next value
for the first column, a newline and so on, until all your values are
printed out. Don't forget to add a final newline to the file and
remember to close it.

Jun 28 '08 #2

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
No, freopen isn't going to be useful.

You generally can't insert data into an existing file. What you can
do is read an existing file and write a new one (and, if you like,
rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing the
modified version to a new file.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 28 '08 #3

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
No, freopen isn't going to be useful.

You generally can't insert data into an existing file. What you can
do is read an existing file and write a new one (and, if you like,
rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing the
modified version to a new file.
I know what you mean but isn't the "a" mode in fopen for appending to
existing data? I will try Santosh's idea first and keep your advice in mind.

Bill
Jun 28 '08 #4

P: n/a

"Bill Cunningham" <no****@nspam.comwrote in message
news:Yrz9k.89$713.42@trnddc03...
>
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
>No, freopen isn't going to be useful.

You generally can't insert data into an existing file. What you can
do is read an existing file and write a new one (and, if you like,
rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing the
modified version to a new file.
I know what you mean but isn't the "a" mode in fopen for appending to
existing data? I will try Santosh's idea first and keep your advice in
mind.
That's a good point. But to do what you seem to want would require writing
the data sideways:

3 2 1
0 0 0

Ie. 10,20,30. Now add a column of squares by appending:

3 2 1
0 0 0

9 4 1
0 0 0
0 0 0
--
Bartc

Jun 28 '08 #5

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g4**********@registered.motzarella.org...
Fprintf is enough. Print the first entry of the first column, print a
tab, then print the total, another tab, then the average, then a
newline, your next value for the first column, a newline, next value
for the first column, a newline and so on, until all your values are
printed out. Don't forget to add a final newline to the file and
remember to close it.
This is a copy of the code I wrote. The program tabs alright but prints
on the next line there is also a stray % right under the first column of
numbers. So I get this from the above column of numbers,

11.00
% 54.00 /* An arbitray number I am inputing */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *fp;
double x;
x=strtod(argv[1],NULL);
fp=fopen("data","a");
fprintf(fp,"%.2f\t",x);
fclose(fp);
return 0;
}

Bill

Jun 29 '08 #6

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
>No, freopen isn't going to be useful.

You generally can't insert data into an existing file. What you can
do is read an existing file and write a new one (and, if you like,
rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing the
modified version to a new file.
I know what you mean but isn't the "a" mode in fopen for appending to
existing data?
You are not appending data -- you plan to insert data before the
newline that ends the last line -- and as Keith points out you just
can't do that.
I will try Santosh's idea first and keep your advice in mind.
Obviously you can try what you like, and I don't have Santosh's reply
to hand so I can't be sure what it says, but nothing in it can alter
the fact that you originally wanted to insert some new text just
before the final newline, and you can't do that by opening the file in
"a" mode.

--
Ben.
Jun 29 '08 #7

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
You are not appending data -- you plan to insert data before the
newline that ends the last line -- and as Keith points out you just
can't do that.
>I will try Santosh's idea first and keep your advice in mind.

Obviously you can try what you like, and I don't have Santosh's reply
to hand so I can't be sure what it says, but nothing in it can alter
the fact that you originally wanted to insert some new text just
before the final newline, and you can't do that by opening the file in
"a" mode.
Ok I see.

Bill
Jun 29 '08 #8

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
No, freopen isn't going to be useful.

You generally can't insert data into an existing file. What you can
do is read an existing file and write a new one (and, if you like,
rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing the
modified version to a new file.
I know what you mean but isn't the "a" mode in fopen for appending to
existing data? I will try Santosh's idea first and keep your advice in mind.
"a" mode lets you append *to the end of an existing file*. It doesn't
let you append to lines within a file (and neither does anything else,
other than creating a new file).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #9

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"santosh" <sa*********@gmail.comwrote in message
news:g4**********@registered.motzarella.org...
Fprintf is enough. Print the first entry of the first column, print a
tab, then print the total, another tab, then the average, then a
newline, your next value for the first column, a newline, next value
for the first column, a newline and so on, until all your values are
printed out. Don't forget to add a final newline to the file and
remember to close it.

This is a copy of the code I wrote. The program tabs alright but prints
on the next line there is also a stray % right under the first column of
numbers. So I get this from the above column of numbers,

11.00
% 54.00 /* An arbitray number I am inputing */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *fp;
double x;
x=strtod(argv[1],NULL);
fp=fopen("data","a");
fprintf(fp,"%.2f\t",x);
fclose(fp);
return 0;
}
The '%' character must have been in the file already.

Append mode will not solve your problem. You want to append data at
the end of each line. The only way to do that is to write a new file,
from scratch, containing the data you read from the original file plus
whatever you want to add on each line.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #10

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
You are not appending data -- you plan to insert data before the
newline that ends the last line -- and as Keith points out you just
can't do that.
>I will try Santosh's idea first and keep your advice in mind.

Obviously you can try what you like, and I don't have Santosh's reply
to hand so I can't be sure what it says, but nothing in it can alter
the fact that you originally wanted to insert some new text just
before the final newline, and you can't do that by opening the file in
"a" mode.
Can fpos and other functions be used to make adjustments to an already
existing file ?

Bill
Jun 29 '08 #11

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>You are not appending data -- you plan to insert data before the
newline that ends the last line -- and as Keith points out you just
can't do that.
>>I will try Santosh's idea first and keep your advice in mind.

Obviously you can try what you like, and I don't have Santosh's reply
to hand so I can't be sure what it says, but nothing in it can alter
the fact that you originally wanted to insert some new text just
before the final newline, and you can't do that by opening the file in
"a" mode.
Can fpos and other functions be used to make adjustments to an already
existing file ?
Adjustment is too vague to be useful. Other functions (there is no
fpos function) can certainly make adjustments -- the most dramatic
being to remove the file altogether. Certain combinations of calls
can be used to truncate a file (make it retain just some initial
segment of its data). Nothing at all allows you to insert data.

There are two ways to handle this sort of problem. As Keith has
suggested you read the data and write out a new file with any extra
bits. At the end, you can rename the file to make it look like you
changed it. This is by far the most common and the safest way to do
what you want.

The other way is less portable (at least is has more potential traps
for unwary programmers). After opening the file read/write, you find
the place you want to add the data, remember the location, and then
make a copy of all the data that follows. You return to your saved
place, write the new data and then the copy of the data you saved. If
I understand your original request, you want to add data just before
the final newline, so this method is actually reasonable (the "saved
data" will always be just a newline character) but I would still
suggest you don't use this technique since it is very hard to get
right.

--
Ben.
Jun 29 '08 #12

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
The '%' character must have been in the file already.

Append mode will not solve your problem. You want to append data at
the end of each line. The only way to do that is to write a new file,
from scratch, containing the data you read from the original file plus
whatever you want to add on each line.
What does ftell, fgetpos and these functions do? I guess they won't help
me either.

Bill
Jun 29 '08 #13

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
The '%' character must have been in the file already.

Append mode will not solve your problem. You want to append data at
the end of each line. The only way to do that is to write a new file,
from scratch, containing the data you read from the original file plus
whatever you want to add on each line.

What does ftell, fgetpos and these functions do?
Um, why do you ask? If you really want to know what they do, RTFM
(Read The Fine Manual).
I guess they won't help
me either.
I don't see how they would.

I think your approach to this problem, and your approach to problems
in general, is backwards. You tend to start with some function that
you think might be useful, and then try to figure out how to use it to
solve your problem. Instead, start with a clear statement of the
problem, and then find out what functions will help you solve it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #14

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
Um, why do you ask? If you really want to know what they do, RTFM
(Read The Fine Manual).
> I guess they won't
help
me either.

I don't see how they would.

I think your approach to this problem, and your approach to problems
in general, is backwards. You tend to start with some function that
you think might be useful, and then try to figure out how to use it to
solve your problem. Instead, start with a clear statement of the
problem, and then find out what functions will help you solve it.
This would be acceptable to me and I guess this is appending.

11.00
52.50

The thing is there are % popping up in the text file that weren't there
before. I might get something like this.

11.00
% 52.50

I am not putting that % in there.

Bill
Jun 29 '08 #15

P: n/a
Bill Cunningham wrote:
"Keith Thompson" <ks***@mib.orgwrote in message
>You generally can't insert data into an existing file. What you
can do is read an existing file and write a new one (and, if you
like, rename the new file so it replaces the original one).

As a first step, try just reading the existing file and writing
the modified version to a new file.
I know what you mean but isn't the "a" mode in fopen for appending
to existing data? I will try Santosh's idea first and keep your
advice in mind.
No, the "a" is for appending to the file, i.e. at the end. There
is nothing to allow appending to lines. Copy the file to a new
one, and append data where you need it during the copy.

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

P: n/a
Bill Cunningham wrote:
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

File is open for writing.
List is freed.
File is closed.

File is open for reading.
File lines:
10.50 52.50 10.50
10.25 52.50 10.50
10.00 52.50 10.50
10.75 52.50 10.50
11.00 52.50 10.50
Line reading buffer is freed.
File is closed.
File is removed.

/* END new.c output */

/* BEGIN new.c */

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

#define DOUBLES {10.5, 10.25, 10, 10.75, 11}
#define NMEMB(A) (sizeof (A) / sizeof *(A))

typedef struct list_node {
struct list_node *next;
void *data;
} list_type;

int get_line(char **lineptr, size_t *n, FILE *stream);
list_type *list_append
(list_type **head, list_type *tail, void *data, size_t size);
void list_free(list_type *node, void (*free_data)(void *));

int main(void)
{
int rc;
size_t index;
FILE *fp;
long unsigned count = 0;
double sum = 0.0;
char fn[L_tmpnam];
double array[] = DOUBLES;
char *buff = NULL;
size_t size = 0;
list_type *head = NULL;
list_type *tail = NULL;

puts("/* BEGIN new.c output */\n");
tmpnam(fn);
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
puts("File lines:");
for (index = 0; index != NMEMB(array); ++index) {
fprintf( fp, "%.2f\n", array[index]);
fprintf(stdout, "%.2f\n", array[index]);
}
fclose(fp);
puts("File is closed.\n");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for reading.");
puts("Reading file into a linked list...");
while ((rc = get_line(&buff, &size, fp)) 0) {
tail = list_append(&head, tail, buff, rc);
if (tail == NULL) {
fputs("tail == NULL\n", stderr);
break;
}
++count;
}
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nFile is open for writing.");
for (tail = head; tail != NULL; tail = tail -next) {
sum += atof(tail -data);
}
for (tail = head, index = 0; index != count; ++index) {
fprintf(fp, "%.2f %.2f %.2f\n",
array[index], sum, sum / count);
tail = tail -next;
if (tail == NULL) {
break;
}
}
list_free(head, free);
puts("List is freed.");
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nFile is open for reading.");
puts("File lines:");
while ((rc = get_line(&buff, &size, fp)) 0) {
fprintf(stdout, "%s\n", buff);
}
free(buff);
buff = NULL;
size = 0;
puts("Line reading buffer is freed.");
fclose(fp);
puts("File is closed.");
remove(fn);
puts("File is removed.");
puts("\n/* END new.c output */");
return 0;
}

int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;

count = 0;
while ((rc = getc(stream)) != EOF
|| !feof(stream) && !ferror(stream))
{
++count;
if (count == (size_t)-2) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
break;
}
if (count + 2 *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n count) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
break;
}
}
if (rc != EOF || !feof(stream) && !ferror(stream)) {
rc = INT_MAX count ? count : INT_MAX;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}

list_type *list_append
(list_type **head, list_type *tail, void *data, size_t size)
{
list_type *node;

node = malloc(sizeof *node);
if (node != NULL) {
node -next = NULL;
node -data = malloc(size);
if (node -data != NULL) {
memcpy(node -data, data, size);
if (*head != NULL) {
tail -next = node;
} else {
*head = node;
}
} else {
free(node);
node = NULL;
}
}
return node;
}

void list_free(list_type *node, void (*free_data)(void *))
{
list_type *next_node;

while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}

/* END new.c */

--
pete
Jun 29 '08 #17

P: n/a
On Sun, 29 Jun 2008 00:07:03 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
This is a copy of the code I wrote. The program tabs alright but prints
on the next line there is also a stray % right under the first column of
numbers. So I get this from the above column of numbers,

11.00
% 54.00 /* An arbitray number I am inputing */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *fp;
double x;
x=strtod(argv[1],NULL);
fp=fopen("data","a");
fprintf(fp,"%.2f\t",x);
fclose(fp);
return 0;
}
How does this code satisfy the condition you specified that you want
to process data in a file? At no point do you open any file for input
and (consistently) at no point do you read any data from a file.
Remove del for email
Jun 29 '08 #18

P: n/a
On Sat, 28 Jun 2008 23:11:13 -0400, pete <pf*****@mindspring.com>
wrote:
>Bill Cunningham wrote:
> I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.

Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.
snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?
Remove del for email
Jun 29 '08 #19

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
Are you writing this new information *just* on the last line, or are
you appending information to each line? (I've been assuming the
latter.)

In other words, do you want the final output to look like this?

10.50
10.25
10.00
10.75
11.00 52.50 10.50

That's a rather odd thing to want to do. You're writing the summary
data on the same line as the last input data item, which seems quite
arbitrary.

If that's really what you want to do, then of course you can do it
(most easily by writing a new file, as I've been saying).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #20

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
[...]
The thing is there are % popping up in the text file that weren't there
before. I might get something like this.

11.00
% 52.50

I am not putting that % in there.
Yes, you almost certainly are.

Check your fprintf format strings. Failing that post the exact code
that generates the file.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #21

P: n/a
Barry Schwarz wrote:
On Sat, 28 Jun 2008 23:11:13 -0400, pete <pf*****@mindspring.com>
wrote:
>Bill Cunningham wrote:
>> I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?
Because the discussions in this thread seem to be going nowhere.

--
pete
Jun 29 '08 #22

P: n/a
Barry Schwarz wrote:
On Sat, 28 Jun 2008 23:11:13 -0400, pete <pf*****@mindspring.com>
wrote:
>Bill Cunningham wrote:
>> I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?
I posted that, because that's really how I would do it.
The first two things that popped into my head
were "linked list" and "extra temp file".
My preference is to always minimize the number of open files.
/* BEGIN new.c output */

File is open for writing.
File is closed.
File is open for reading.
File lines:
10.50
10.25
10.00
10.75
11.00
File is rewound.

Calculating sum and average.
sum is 52.500000. average is 5.250000
File is rewound.

Temp file is open for writing.
Reading file into a temp file...
File is closed.
Temp file is closed.

Temp file is open for reading.
File is open for writing.
File is closed.
Temp file is closed.
Temp file is removed.

File is open for reading.
File lines:
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
File is closed.
File is removed.

/* END new.c output */

/* BEGIN new.c */

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

#define DOUBLES {10.5, 10.25, 10, 10.75, 11}
#define NMEMB(A) (sizeof (A) / sizeof *(A))

int main(void)
{
int rc;
FILE *fp;
FILE *fp_extra_temp;
double temp_double;
double average;
long unsigned count = 0;
double sum = 0.0;
char fn[L_tmpnam];
char fn_extra_temp[L_tmpnam];
double array[] = DOUBLES;

puts("/* BEGIN new.c output */\n");
tmpnam(fn);
tmpnam(fn_extra_temp);
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
for (count = 0; count != NMEMB(array); ++count) {
fprintf( fp, "%.2f\n", array[count]);
}
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
rewind(fp);
puts("File is rewound.\n");
puts("Calculating sum and average.");
while (fscanf(fp, "%lf", &temp_double) == 1) {
sum += temp_double;
++count;
}
average = sum / count;
printf("sum is %f. average is %f\n", sum, average);
rewind(fp);
puts("File is rewound.\n");
fp_extra_temp = fopen(fn_extra_temp, "w");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("Temp file is open for writing.");
puts("Reading file into a temp file...");
while ((rc = getc(fp)) != EOF) {
putc(rc, fp_extra_temp);
}
fclose(fp);
puts("File is closed.");
fclose(fp_extra_temp);
puts("Temp file is closed.");
fp_extra_temp = fopen(fn_extra_temp, "r");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nTemp file is open for reading.");
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
while (fscanf(fp_extra_temp, "%f", &temp_double) != EOF) {
fprintf(fp, "%.2f %.2f %.2f\n", temp_double, sum, average);
}
fclose(fp);
puts("File is closed.");
fclose(fp_extra_temp);
puts("Temp file is closed.");
remove(fn_extra_temp);
puts("Temp file is removed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nFile is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
fclose(fp);
puts("File is closed.");
remove(fn);
puts("File is removed.");
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */
--
pete
Jun 29 '08 #23

P: n/a
pete wrote:
Barry Schwarz wrote:
>On Sat, 28 Jun 2008 23:11:13 -0400, pete <pf*****@mindspring.com>
wrote:
>>Bill Cunningham wrote:
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right
beside it which is a total of these values and then average them in
another column. For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe
freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?

I posted that, because that's really how I would do it.
The first two things that popped into my head
were "linked list" and "extra temp file".
My preference is to always minimize the number of open files.
/* BEGIN new.c output */

File is open for writing.
File is closed.
File is open for reading.
File lines:
10.50
10.25
10.00
10.75
11.00
File is rewound.

Calculating sum and average.
sum is 52.500000. average is 5.250000
File is rewound.

Temp file is open for writing.
Reading file into a temp file...
File is closed.
Temp file is closed.

Temp file is open for reading.
File is open for writing.
File is closed.
Temp file is closed.
Temp file is removed.

File is open for reading.
File lines:
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
File is closed.
File is removed.

/* END new.c output */
I just noticed that the resulting file is wrong.
--
pete
Jun 29 '08 #24

P: n/a
pete wrote:
pete wrote:
>Barry Schwarz wrote:
>>On Sat, 28 Jun 2008 23:11:13 -0400, pete <pf*****@mindspring.com>
wrote:

Bill Cunningham wrote:
I have a row of values like such, placed in a text file by
fprintf.
>
10.50
10.25
10.00
10.75
11.00
>
What I want to do to the above colum is add a new column right
beside it which is a total of these values and then average them in
another column. For example.
>
11.00 52.50
>
In another column is the average of the 5.
>
11.00 52.50 10.50
>
I am assuming that fprintf is going to have to be used and maybe
freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?

I posted that, because that's really how I would do it.
The first two things that popped into my head
were "linked list" and "extra temp file".
My preference is to always minimize the number of open files.
/* BEGIN new.c output */

File is open for writing.
File is closed.
File is open for reading.
File lines:
10.50
10.25
10.00
10.75
11.00
File is rewound.

Calculating sum and average.
sum is 52.500000. average is 5.250000
File is rewound.

Temp file is open for writing.
Reading file into a temp file...
File is closed.
Temp file is closed.

Temp file is open for reading.
File is open for writing.
File is closed.
Temp file is closed.
Temp file is removed.

File is open for reading.
File lines:
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
File is closed.
File is removed.

/* END new.c output */

I just noticed that the resulting file is wrong.
That was because I had tried to fscanf a double as a float.

/* BEGIN new.c output */

File is open for writing.
File is closed.
File is open for reading.
File lines:
10.50
10.25
10.00
10.75
11.00
File is rewound.

Calculating sum and average.
sum is 52.500000. average is 5.250000
File is rewound.

Temp file is open for writing.
Reading file into a temp file...
File is closed.
Temp file is closed.

Temp file is open for reading.
File is open for writing.
File is closed.
Temp file is closed.
Temp file is removed.

File is open for reading.
File lines:
10.50 52.50 5.25
10.25 52.50 5.25
10.00 52.50 5.25
10.75 52.50 5.25
11.00 52.50 5.25
File is closed.
File is removed.

/* END new.c output */

/* BEGIN new.c */

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

#define DOUBLES {10.5, 10.25, 10, 10.75, 11}
#define NMEMB(A) (sizeof (A) / sizeof *(A))

int main(void)
{
int rc;
FILE *fp;
FILE *fp_extra_temp;
double temp_double;
double average;
long unsigned count = 0;
double sum = 0.0;
char fn[L_tmpnam];
char fn_extra_temp[L_tmpnam];
double array[] = DOUBLES;

puts("/* BEGIN new.c output */\n");
tmpnam(fn);
tmpnam(fn_extra_temp);
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
for (count = 0; count != NMEMB(array); ++count) {
fprintf( fp, "%.2f\n", array[count]);
}
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
rewind(fp);
puts("File is rewound.\n");
puts("Calculating sum and average.");
while (fscanf(fp, "%lf", &temp_double) == 1) {
sum += temp_double;
++count;
}
average = sum / count;
printf("sum is %f. average is %f\n", sum, average);
rewind(fp);
puts("File is rewound.\n");
fp_extra_temp = fopen(fn_extra_temp, "w");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("Temp file is open for writing.");
puts("Reading file into a temp file...");
while ((rc = getc(fp)) != EOF) {
putc(rc, fp_extra_temp);
}
fclose(fp);
puts("File is closed.");
fclose(fp_extra_temp);
puts("Temp file is closed.");
fp_extra_temp = fopen(fn_extra_temp, "r");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nTemp file is open for reading.");
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
while (fscanf(fp_extra_temp, "%lf", &temp_double) != EOF) {
fprintf(fp, "%.2f %.2f %.2f\n", temp_double, sum, average);
}
fclose(fp);
puts("File is closed.");
fclose(fp_extra_temp);
puts("Temp file is closed.");
remove(fn_extra_temp);
puts("Temp file is removed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nFile is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
fclose(fp);
puts("File is closed.");
remove(fn);
puts("File is removed.");
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */

--
pete
Jun 29 '08 #25

P: n/a
Keith Thompson wrote:
"Bill Cunningham" <no****@nspam.comwrites:
> I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it
which is a total of these values and then average them in another column.
For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.

Are you writing this new information *just* on the last line, or are
you appending information to each line? (I've been assuming the
latter.)

In other words, do you want the final output to look like this?

10.50
10.25
10.00
10.75
11.00 52.50 10.50

That's a rather odd thing to want to do. You're writing the summary
data on the same line as the last input data item, which seems quite
arbitrary.

If that's really what you want to do, then of course you can do it
(most easily by writing a new file, as I've been saying).

After further consideration,
I think this is what he most probably wants:
10.50 10.50 10.50
10.25 20.75 10.38
10.00 30.75 10.25
10.75 41.50 10.38
11.00 52.50 10.50
/* BEGIN new.c */

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

#define DOUBLES {10.5, 10.25, 10, 10.75, 11}
#define NMEMB(A) (sizeof (A) / sizeof *(A))

int main(void)
{
int rc;
FILE *fp;
FILE *fp_extra_temp;
double temp_double;
double average;
long unsigned count = 0;
double sum = 0.0;
char fn[L_tmpnam];
char fn_extra_temp[L_tmpnam];
double array[] = DOUBLES;

puts("/* BEGIN new.c output */\n");
tmpnam(fn);
tmpnam(fn_extra_temp);
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
for (count = 0; count != NMEMB(array); ++count) {
fprintf( fp, "%.2f\n", array[count]);
}
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
rewind(fp);
puts("File is rewound.\n");
fp_extra_temp = fopen(fn_extra_temp, "w");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("Temp file is open for writing.");
for (count = 1; fscanf(fp, "%lf", &temp_double) == 1; ++count) {
sum += temp_double;
average = sum / count;
fprintf(fp_extra_temp, "%.2f %.2f %.2f\n",
temp_double, sum, average);
}
fclose(fp_extra_temp);
puts("Temp file is closed.");
fclose(fp);
puts("File is closed.");
fp_extra_temp = fopen(fn_extra_temp, "r");
if (fp_extra_temp == NULL) {
fputs("fopen(fn_extra_temp, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nTemp file is open for reading.");
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn), \"w\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("File is open for writing.");
puts("Copying temp file to file.");
while ((rc = getc(fp_extra_temp)) != EOF) {
putc(rc, fp);
}
fclose(fp);
puts("File is closed.");
fclose(fp_extra_temp);
puts("Temp file is closed.");
remove(fn_extra_temp);
puts("Temp file is removed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn, \"r\") == NULL\n", stderr);
exit(EXIT_FAILURE);
}
puts("\nFile is open for reading.");
puts("File lines:");
while ((rc = getc(fp)) != EOF) {
putchar(rc);
}
fclose(fp);
puts("File is closed.");
remove(fn);
puts("File is removed.");
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */
--
pete
Jun 29 '08 #26

P: n/a
santosh <sa*********@gmail.comwrites:
Bill Cunningham wrote:
> I have a row of values like such, placed in a text file by
fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right
beside it
which is a total of these values and then average them in another
column. For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe
freopen.

Fprintf is enough. Print the first entry of the first column, print a
tab, then print the total, another tab, then the average, then a
newline, your next value for the first column, a newline, next value
for the first column, a newline and so on, until all your values are
printed out. Don't forget to add a final newline to the file and
remember to close it.
I would recommend sprintf followed by fprintf. Why? You can see your
formatted string clearly in your debugger before you even start writing
to files and wasting time...
Jun 29 '08 #27

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
Are you writing this new information *just* on the last line, or are
you appending information to each line? (I've been assuming the
latter.)

In other words, do you want the final output to look like this?

10.50
10.25
10.00
10.75
11.00 52.50 10.50

That's a rather odd thing to want to do. You're writing the summary
data on the same line as the last input data item, which seems quite
arbitrary.
Well I guess it would be alright to do this.

11.00
52.50 10.50
Bill
Jun 29 '08 #28

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
Are you writing this new information *just* on the last line, or are
you appending information to each line? (I've been assuming the
latter.)

In other words, do you want the final output to look like this?

10.50
10.25
10.00
10.75
11.00 52.50 10.50

That's a rather odd thing to want to do. You're writing the summary
data on the same line as the last input data item, which seems quite
arbitrary.

Well I guess it would be alright to do this.

11.00
52.50 10.50
You managed not to answer my actual question.

Are you writing this new information *just* on the last line, or are
you appending information to each line?

And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #29

P: n/a
rio
I would recommend sprintf followed by fprintf. Why? You can see your
formatted string clearly in your debugger before you even start writing
to files and wasting time...
I would raccomand of not use[*]printf,[*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors

Jun 29 '08 #30

P: n/a
"rio" <a@b.cwrites:
Richard<rg****@gmail.comwrites:
I would recommend sprintf followed by fprintf. Why? You can see your
formatted string clearly in your debugger before you even start writing
to files and wasting time...

I would raccomand of not use[*]printf,[*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors
I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input), but
what's dangerous about the *printf functions? The only thing I can
think of is that sprintf can overflow the target buffer if you're not
careful; the solution to that is either to use a target buffer that's
known to be big enough, or to use snprintf if it's available.

BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <av@ala.a>"? I've asked you this
before, and you never answered. If you're changing identities to
avoid killfiles, please stop. If you're not the same person, then
never mind.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 29 '08 #31

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?
I am trying to create a moving average of data. Whether it be for 5-10-30
days of securitites prices.

Bill
Jun 29 '08 #32

P: n/a

"pete" <pf*****@mindspring.comwrote in message
news:Sp******************************@earthlink.co m...
After further consideration,
I think this is what he most probably wants:
10.50 10.50 10.50
10.25 20.75 10.38
10.00 30.75 10.25
10.75 41.50 10.38
11.00 52.50 10.50
[snip]

Yes that would work Pete. Your code is beyond my skills though. I'm not into
structs unfortunately. A moving average is what I'm looking for and that
data posted by you would fit the bill. If a person kept going like you have
it set up here one could choose a moving average of 5-10-30 or whatever days
of averaged data.

Bill
Jun 29 '08 #33

P: n/a
Bill Cunningham wrote:
"pete" <pf*****@mindspring.comwrote in message
news:Sp******************************@earthlink.co m...
>After further consideration,
I think this is what he most probably wants:
10.50 10.50 10.50
10.25 20.75 10.38
10.00 30.75 10.25
10.75 41.50 10.38
11.00 52.50 10.50
[snip]

Yes that would work Pete. Your code is beyond my skills though.
That was really the most primitive way I could come up with.
It's mostly just opening and closing files.
I'm not into structs unfortunately.
There's no structs in that code.
A moving average is what I'm looking for and that
data posted by you would fit the bill. If a person kept going like you have
it set up here one could choose a moving average of 5-10-30 or whatever days
of averaged data.
--
pete
Jun 29 '08 #34

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?
I am trying to create a moving average of data. Whether it be for 5-10-30
days of securitites prices.
I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

If (and only if!) you confirm that my description is an accurate
statement of the problem you're trying to solve, I'll give you some
hints about how to solve it. If I haven't described the problem
correctly, please clarify.

Your stubborn insistence on trying to solve a problem when you haven't
clearly stated what the problem is has caused a lot of people to waste
a lot of time.

Presumably you know what you want to accomplish. Go back to your
initial article in this thread, and ask yourself whether someone could
figure out what you're trying to do based only on your description.
Then think about how you could have made your description clear
enough. And next time, *please* try to formulate your question
clearly *before* you post it here.

Incidentally, I could probably write a working program to solve your
problem in about 5 minutes. Guiding you through the process takes a
lot longer. But handing you the solution wouldn't do you much good.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 30 '08 #35

P: n/a
Keith Thompson wrote:
"rio" <a@b.cwrites:
>Richard<rg****@gmail.comwrites:
I would recommend sprintf followed by fprintf. Why? You can see
your formatted string clearly in your debugger before you even
start writing to files and wasting time...

I would raccomand of not use[*]printf,[*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors

I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input),
They are also dangerous with the unadorned %s specifier.

<snip>
BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <av@ala.a>"? I've asked you this
before, and you never answered.
IMO, it isn't necessary to ask him (and why would he admit to have once
been a troll anyway?). His writing style, and his fake email addresses
are a dead giveaway.
If you're changing identities to
avoid killfiles, please stop. If you're not the same person, then
never mind.
Jun 30 '08 #36

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
"Bill Cunningham" <no****@nspam.comwrites:
>"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?
I am trying to create a moving average of data. Whether it be for 5-10-30
days of securitites prices.

I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?
[snip]

Yes that's what I want to do. There were several ways to go about this but
what you described is the way to go.

Bill
Jun 30 '08 #37

P: n/a
Bill Cunningham wrote:
>
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
>"Bill Cunningham" <no****@nspam.comwrites:
>>"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?

I am trying to create a moving average of data. Whether it be for
5-10-30 days of securitites prices.

I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

[snip]

Yes that's what I want to do. There were several ways to go about this
but what you described is the way to go.
Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far), and for each input item read, add the
item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.

Jun 30 '08 #38

P: n/a
Bill Cunningham wrote:
>
"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
>"Bill Cunningham" <no****@nspam.comwrites:
>>"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?

I am trying to create a moving average of data. Whether it be for
5-10-30 days of securitites prices.

I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

[snip]

Yes that's what I want to do. There were several ways to go about this
but what you described is the way to go.
Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far), and for each input item read, add the
item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.

Jun 30 '08 #39

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g4**********@aioe.org...

[snip]
You won't need anything beyond fscanf and fprintf.
fscanf. Ok that's a new function I'll look up how to use it.

Bill
Jun 30 '08 #40

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g4**********@aioe.org...
Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far),
Now I'm a little confused about how to go about a variable to keep track
of the input numbers so far.

and for each input item read, add the
item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.

Jun 30 '08 #41

P: n/a
santosh said, in reply to Bill Cunningham:

<snip>
You won't need anything beyond fscanf and fprintf.
Okay, so fprintf is fair enough - but telling BC about fscanf is like
handing him a loaded shotgun so heavy that the barrel is already pointing
directly at his foot.

--
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
Jun 30 '08 #42

P: n/a
On Mon, 30 Jun 2008 19:28:50 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
"santosh" <sa*********@gmail.comwrote in message
news:g4**********@aioe.org...
>Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far),

Now I'm a little confused about how to go about a variable to keep track
of the input numbers so far.
Initialize an int to zero. After reading each line, add one to the
int (or, if you are feeling brave, increment the int).
>
and for each input item read, add the
>item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.

Remove del for email
Jul 1 '08 #43

P: n/a

"Barry Schwarz" <sc******@dqel.comwrote in message
news:8q********************************@4ax.com...
Initialize an int to zero. After reading each line, add one to the
int (or, if you are feeling brave, increment the int).
Could you give me an example of this? Would you use for ?

int count=0;
What would be the trigger to increment?

Bill
Jul 1 '08 #44

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Barry Schwarz" <sc******@dqel.comwrote in message
news:8q********************************@4ax.com...
Initialize an int to zero. After reading each line, add one to the
int (or, if you are feeling brave, increment the int).

Could you give me an example of this? Would you use for ?

int count=0;
What would be the trigger to increment?
You're asking questions that are so elementary that you're practically
asking us to write the program for you.

Hmm.

Ok, here's an outline in English:

Keep track of the number of lines you've read and of the sum of the
numbers so far. Both start as 0 or 0.0.
For each line in the input file:
Get a floating-point number from the line.
Update the line counter and the sum.
Print the following on the output file:
The number you just read,
The sum of the numbers you've read so far,
The average of the numbers you've read so far
(the sum divided by the count).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jul 1 '08 #45

P: n/a
rio

"santosh" <sa*********@gmail.comha scritto nel messaggio
news:g4**********@registered.motzarella.org...
Keith Thompson wrote:
>"rio" <a@b.cwrites:
>>Richard<rg****@gmail.comwrites:
I would recommend sprintf followed by fprintf. Why? You can see
your formatted string clearly in your debugger before you even
start writing to files and wasting time...

I would raccomand of not use[*]printf,[*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors

I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input),

They are also dangerous with the unadorned %s specifier.
it could be not ok sprintf with "%s" format string too, or sprintf in
general
<snip>
>BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <av@ala.a>"? I've asked you this
before, and you never answered.

IMO, it isn't necessary to ask him (and why would he admit to have once
been a troll anyway?). His writing style, and his fake email addresses
are a dead giveaway.
*now* i'm a troll because i'm not agree with the C way of think
so write in comp.lang.c against C
>If you're changing identities to
avoid killfiles, please stop. If you're not the same person, then
never mind.
yes i'm the same
but i think with the post header =should be only me.
then there should be only one that write so many grammar errors
not consider what people can think of it

Jul 1 '08 #46

P: n/a
rio wrote:
"santosh" <sa*********@gmail.comha scritto nel messaggio
news:g4**********@registered.motzarella.org...
>Keith Thompson wrote:
>>"rio" <a@b.cwrites:
Richard<rg****@gmail.comwrites:
I would recommend sprintf followed by fprintf. Why? You can see
your formatted string clearly in your debugger before you even
start writing to files and wasting time...
I would raccomand of not use[*]printf,[*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors
I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input),
They are also dangerous with the unadorned %s specifier.

it could be not ok sprintf with "%s" format string too, or sprintf in
general
><snip>
>>BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <av@ala.a>"? I've asked you this
before, and you never answered.
IMO, it isn't necessary to ask him (and why would he admit to have once
been a troll anyway?). His writing style, and his fake email addresses
are a dead giveaway.

*now* i'm a troll because i'm not agree with the C way of think
so write in comp.lang.c against C
No.
It's your "nymshifting" that makes you a troll.
>>If you're changing identities to
avoid killfiles, please stop.
Rio,
the content of your posts, makes you the programmer
with the consistently worst possible coding style and advice.
That's how you are known;
that's how you were recognized.

--
pete
Jul 1 '08 #47

P: n/a
rio

"pete" <pf*****@mindspring.comha scritto nel messaggio
news:Iv******************************@earthlink.co m...
rio wrote:
>"santosh" <sa*********@gmail.comha scritto nel messaggio
news:g4**********@registered.motzarella.org...
>>Keith Thompson wrote:
*now* i'm a troll because i'm not agree with the C way of think
so write in comp.lang.c against C

No.
It's your "nymshifting" that makes you a troll.
nymshwhat?
>>>If you're changing identities to
avoid killfiles, please stop.

Rio,
the content of your posts, makes you the programmer
with the consistently worst possible coding style and advice.
That's how you are known;
that's how you were recognized.
it doesn't interst me much how people know me or recognoze me;
people make error 90% of times, so it is the first thing to do
use our own mind and not the people thinking "all'ammasso"

ciao
--
pete

Jul 1 '08 #48

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:lz************@stalkings.ghoti.net...
You're asking questions that are so elementary that you're practically
asking us to write the program for you.

Hmm.

Ok, here's an outline in English:

Keep track of the number of lines you've read and of the sum of the
numbers so far. Both start as 0 or 0.0.
For each line in the input file:
Get a floating-point number from the line.
Update the line counter and the sum.
Print the following on the output file:
The number you just read,
The sum of the numbers you've read so far,
The average of the numbers you've read so far
(the sum divided by the count).
You're talking about reading. I need to write the data first. I've
considered writing two functions to do this, one to write and one to read. I
need to start writing the data first. I have a program that writes the
single numbers and that's all.

Bill
Jul 1 '08 #49

P: n/a
On Jul 1, 6:25*pm, "rio" <a...@b.cwrote:
"pete" <pfil...@mindspring.comha scritto nel messaggionews:Iv******************************@ear thlink.com...
rio wrote:
"santosh" <santosh....@gmail.comha scritto nel messaggio
news:g4**********@registered.motzarella.org...
Keith Thompson wrote:
*now* i'm a troll because i'm not agree with the C way of think
so write in comp.lang.c against C
um, if you don't like C why don't you program
in another language and leave comp.lang.c alone?
No.
It's your "nymshifting" that makes you a troll.

nymshwhat?
nymshifting = name shifting

deliberatly changing your posting name to avoid recognition.
It's ok to use a couple of names (eg. work and home) if
there's a good reason, but changing names just to confuse people
is dishonest.

>>If you're changing identities to
avoid killfiles, please stop.
Rio,
the content of your posts, makes you the programmer
with the consistently worst possible coding style and advice.
That's how you are known;
that's how you were recognized.

it doesn't interst me much how people know me or recognoze me;
people make error 90% of times,
must of us attempt to keep the error rate much lower than this
so it is the first thing to do
use our own mind and not the people thinking "all'ammasso"
"in the mass"?
--
Nick Keighley

Jul 1 '08 #50

60 Replies

This discussion thread is closed

Replies have been disabled for this discussion.