text file

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

microsoft.news.com

Is it possible to read a text file then add a colum in the text file?

example file looks like this:

day1 45
day2 90
day 3
day 4 65

I need to add 45, 90, 0 , 65 all together. i never done this before so i'm
not sure how to do it. Is it even possible, if so does someone have a
snippet i could look at ?
 
Normally you can open a file in a read, write or append mode. So simple
solution will be

open a temp output file
read a line from input file
while not end of input file
append a string the to line read
write the appened record to the temp output file
read next line from input file
end while
close infile
close outfile
delete infile
rename outfile to infile

This is a very simple solution. If you need program for this please let
me know
 
I have code that opens the file and reads it, I have that working.

Now, how can i add the column and get the total?

I need the total of :

day1 45
day2 90
day 3
day 4 65


45 + 90 + 0 + 65 = 200

how can i do that in code with file in the format as above?
 
i figured it out.

microsoft.news.com said:
I have code that opens the file and reads it, I have that working.

Now, how can i add the column and get the total?

I need the total of :

day1 45
day2 90
day 3
day 4 65


45 + 90 + 0 + 65 = 200

how can i do that in code with file in the format as above?
 
Is it possible to read a text file then add a colum in the text file?

example file looks like this:

day1 45
day2 90
day 3
day 4 65

I need to add 45, 90, 0 , 65 all together. i never done this before so i'm
not sure how to do it. Is it even possible, if so does someone have a
snippet i could look at ?

Well, it sounds like you want to:

1) Start off with a total of 0.
2) For each line:
a) Split it (look at String.Split)
b) Check whether there's a number
c) Parse the number (look at Int32.Parse)
d) Add the number to the total
3) Keep going until you've run out of lines.

Which bit of the above is unfamiliar to you?
 
Back
Top