add numbers in file

  • Thread starter Thread starter microsoft.news.com
  • Start date Start date
M

microsoft.news.com

I have a text file that has a column of numbers that i need to add to get a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005


I need to add the money colum to get 44,252.00. is that even possible from a
text file?
 
I'm not quite sure I follow the question here but I'll take a stab. If I
don't understand then I'm sorry.

First of all there is no way to just ask the computer what the total is for
column 2. However, you can write a function that will open the text file,
read the file in one line at the time, grabbing the second column and
converting it to a double or currency and then getting the next line.

The text file is going to read in a string for each line that you will have
to parse to get to the 2nd column which looks quite easy if this is the
format of your file. But you still have a string when you parse it so you
will need to cast it as a double or currency in order to add it up as a
number value.

So from my point of view the problem is quite easily obtainable.

Good luck,

glenn
 
since i never done this before, do you have a snippet or can you point me to
an example of this being done?
 
Here are the very basics to get you started
this will open a file with specified path and name of settings.txt
StreamReader sr1 = File.OpenText( path + \\settings.txt);
String input;
//read through file and retreive info
string st = "";
while ((input=sr1.ReadLine())!=null)
{
st = input;
from here you just need to parse the string value held in st to look for
your field. I don't know what to tell you here, if its a space delimited
field then its really easy as all you have to do is give it a start and end
location through the use of the st.substring function to grab what you need.
Then to do the cast to the double for the purposes of adding up your values
you can use the Convert function...
}

good luck,
 
Im reading the file fine, its the adding up the column I need help with.
I'm reading the file and inserting the data into a table. but now i need to
Total the sale price column and insert that into a new column
 
I don't understand really what you are having trouble with. If you have a
variable holding your cost field and I assume it would be a string variable
you just call the Convert function like I said to convert it to a number.

string myvar;
myvar = fieldfromfile;
double item;
double total = 0;

item = Convert.ToDouble( myvar );
total += item;

at the end you will have total that will contain the total amount of items
in teh file...

Your questions and examples are not really shedding much light on exactly
what you are doing so if this doesn't answer you question then maybe someone
else can help you better...

good luck,

glenn
 
Hi,

Of course it's
How to do it depend of how the file is create, by the description you gave
you have two options
1- The columns start in a fixed location
2- Thr columns are separated by a TAB

a seudo code;
string line;
double total = 0;
while( (line= file.ReadLine()) != null )
{
string[] parts = line.Split( "\t" ); // or line.SubString (
startIndex, endIndex );
try{
double += Convert.ToDouble( parts[1] );
}catch{}
}


cheers,
 
Hi Glenn,

The OP is a student trying to get you to help him with homework.

We have an "unwritten convention" of providing assistance without actually
providing code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Looks like homework. If I'm off base, let me know.

A file is like a page of paper. You don't read all the words at once... you
read them in order.
Your program has to read the words in order.

The other posters have provided basic code to open the file and read one
line at a time.
In each line, you need to find the values. Ignacio mentioned that you can
use the Split method on the String object to break up a single string into
an array of strings where each of the smaller ones contains a "word",
depending on how you define it.

You simply need to get a number from that small string (the decimal.Parse()
method comes to mind) and then add the values to a running total.

When you are done, you have your running total.

You can display this total or write it to another file or store it in a
database, depending on the needs of the application.

I hope this helps,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Back
Top