1
votes

I'm trying to write a fortran code that reads name, surname and number and then writes them into already existing file.

Here is what the code looks like: http://pastebin.com/SV8erDND

Now, there'are only 2 lines in the "Deneme.txt" file, which look like this;

john mayer 110
hugh jackman 111

whenever i try to add another name to the file by the compiler, it overwrites the second line. For example, if I type dennis lui 510, the file now looks like this;

john mayer 110
dennis lui 510

How do I prevent it from overwriting? Also, there doesn't seem to be loop. After adding the first name/surname/number, I want it to go back to first step and ask for another name/surname/number.

1

1 Answers

2
votes

First you read a line from the file, then you're asking the user to enter a name. If it matches the one name you just read, the program asks for another name. If it is a new name, it will start writing where the last read stopped, that is on the second line.

Your check if a name already exists doesn't make much sense, since you only check against the first name. What should be done is to read the whole file, store the data in a hash table and then append at the end any name/number entered by the user that is not found in the hash. Are you trying to implement some kind of database? If so, then I don't think Fortran is the best option to arrive at a easy/quick solution, maybe you should go for a language like perl or python.