adding colums to text

B

Bill Cunningham

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
 
S

santosh

Bill said:
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.
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

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
 
B

Bartc

Bill Cunningham said:
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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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.


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
 
K

Keith Thompson

Bill Cunningham said:
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).
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

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.


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
 
C

CBFalconer

Bill said:
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.
 
B

Ben Bacarisse

Bill Cunningham said:
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.
 
B

Bill Cunningham

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
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

Um, why do you ask? If you really want to know what they do, RTFM
(Read The Fine Manual).


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
 
B

Barry Schwarz

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
 
B

Barry Schwarz

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
 
K

Keith Thompson

Bill Cunningham said:
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).
 
K

Keith Thompson

Bill Cunningham said:
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.
 
R

Richard

santosh said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top