Re: adding colums to text



Barry Schwarz wrote:
On Sat, 28 Jun 2008 23:11:13 -0400, pete <pfiland@xxxxxxxxxxxxxx>
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
.



Relevant Pages

  • Re: adding colums to text
    ... File is open for writing. ... Reading file into a linked list... ... Calculating sum and average. ... Temp file is open for writing. ...
    (comp.lang.c)
  • Re: adding colums to text
    ... pete wrote: ... File is open for writing. ... Reading file into a linked list... ... Temp file is open for writing. ...
    (comp.lang.c)
  • Re: show total in a list without the entries
    ... Engelbert schrieb: ... My "reply" here will warn people (reading down) ... Sum = 977 ... workplace: Cologne ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: show total in a list without the entries
    ... My "reply" here will warn people (reading down) ... information [and probably asking questions you've answered in your ... Sum = 977 ... workplace: Cologne ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: While Bush and his posse yammer about the good economy
    ... Shouldn't there be a law invoked in that if you're too lazy to sum ... Umm, to be honest, I don't want to go through the trouble of reading ... "When citizens fear their government, ... "When citizens fear their government, you have tyranny; ...
    (rec.sport.pro-wrestling)