Re: adding colums to text



pete wrote:
pete wrote:
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 */

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
.



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: Convert UNIX Line Breaks
    ... Write them to a temp file and run them through unix2dos or a similar tool ... (or just feed the tool from STDIN). ... Sure beats writing your own conversion code. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: EWF File Persistence Corruption
    ... disk flushing after ... Slobodan Brcin wrote: ... >> How about writing the new ini file to a temp file, ...
    (microsoft.public.windowsxp.embedded)
  • RE: Downloading data and save in a zip file (ASP.NET program)
    ... hope this articles helps you a bit ... he is using files on the HDD.but in your case a probably writing the data to ... a temp file and compressing it can be a try. ... Prev by Date: ...
    (microsoft.public.dotnet.framework.aspnet)