473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

adding colums to text

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
60 2608
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
"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

"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

"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

"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
"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

"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
"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
"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

"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
"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

"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
"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

"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
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
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
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
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
"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
"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
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
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
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
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
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
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

"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
"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
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
"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

"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

"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
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
"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
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

"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
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
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

"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

"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
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
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

"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
"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
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
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
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

"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
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

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

Similar topics

1
by: Ajay Garg | last post by:
What is the way we could implement a business logic from a Table by storing it statemnnets in a colums and defining an execute sql to execute it.Some legal requirements make it diffcult for us to...
5
by: Joy | last post by:
Hi guys, I am in a tricky situation, I really really hope someone will help me. I got a layout with a main container and three colums inside. Main container: 100% height; 90% width; float: left...
0
by: Bernard Dhooghe | last post by:
In http://www-1.ibm.com/partnerworld/pwhome.nsf/weblook/eac_a_webcast_052504.html the writer says (page 50, Multidimensional Clustering Advisor): " The MDC Advisor feature of the DB2 Design...
1
by: Patrick Olurotimi Ige | last post by:
I want to add a HeaderText="Subject" and HeaderImageUrl="" to a Datagrid but it seems it doesn't work! It seems If i specify both header text and a header image, the header image takes...
1
by: basulasz | last post by:
May be it is a bit easy question but i don't know the way to display data in bounded colums. I get data from DB with a datareader, and I want to display them on bounded colums. I dont want to...
1
by: Shilpa | last post by:
Hi , I am shilpa. I have one grid. I am adding data to teh grid thorugh xml. Now i have to create a hyperlink colums when i add data to the grid depending on the value selected in another drop...
1
by: bonk | last post by:
I have a very simple UserControl (derived from System.Windows.Forms.UserControl) that contains several ListViews. The UserControl exposes a single public property: public...
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.