Your algorithm won't work because:
-
You are opening the file in append mode which starts out with the file pointer at the end of the file so the while loop has nothing to read.
-
If you didn't use append mode then you have the problem that reads and writes use the same file pointer so you read from one record and then write to the next record leaving the original record untouched.
-
If you fixed that then you still have the problem that $newval=$val+1; may cause the $val field go from one digit to two digits or two digits to three digits etc. causing you to write one byte into the next field.