Hello All,
I was wondering if someone could help me out with some "coding issues". I am having trouble putting information into a text file and having it read by the program in a different execution, on a different branch. Here is my algorithm for the writing/reading of files: (not very good though...heh)
-----------------------------------
(Execution 1: the user picks choice #1)
1. Open a file (or create it if there isn't one) named "test"
2. Except floating point numbers from a user and assign to variables. (L1, L2, L3)
3. Write each variable into file "test" with a <cr> at the end of each number.
3a. Example of what the output into the text file should look like:
L1: 180<cr>
L2: 1.3329<cr>
L3: 345.4<cr>
4. Save the file and close it.
-----------------------------------
(Execution two with choice #2)
5. Now that the file has been created and the numbers have been written to the file, I need to add a different constant to each of the lines in the file.
5a. Open file "test"
5b. Read line 1 of "test" file.
5c. Add a constant with line 1 of "test" file and assign this new number to the variable L1.
5d. Replace line 1 with new version of variable L1.
5e. Read line two of "test"
5f. Add a different constant with line TWO of "test" and assign this new number to the variable L2.
5g. etc. etc.
6. Save file "test" and close it.
-----------------------------------
So, Here are my problems:
1. I am not sure which mode I use for creating and writing (although I think it is "w")
2. I am okay for right now on step two's syntax
3. For writing numbers into a file, that I want to later use as float data type, do I use some other type on data output other than fprintf()?
4. I know there is fclose(file pointer) but is there also a save function?
5a. I am not sure which mode to open this with. I think "r+" is read/write, but I think that will erase the whole file if you write into it (which means I will need change my algorithm to something like:
Read==>Assign L1
Read==>Assign L2
Read==>Assign L3
Now that I think about it, this is probably a better way of going about solving the problem.)
5b. Here is the biggest problem I think I face. I don't know how to read a file by line. Nor do I know how to assign the line of (what I'm guessing the computer will think is character string data, since it is reading it from a text file) into a float variable.
5c. Once I have line 1 of the "test" file into the L1 variable and so on, I think I will be good on the formulas.
5d. And finally for writing the new L1, L2, and L3 into the file, Should I close the file and reopen it in a different mode? Or do I use a read/write mode when I am first opening the file (if choice two is made).
Here is some sample output if it helps:
Screen:
-------------
Press 1 if this is the first time you are running the program.
Press 2 if you would like to do what this program was made to do.
>>>1
Enter the first number: 180
Enter the second number: 1.3329
Enter the third number: 345.4
Thank you. Please press enter and restart the program if you would like to do what this is made for.
-------------
Press 1 if this is the first time you are running the program.
Press 2 if you would like to do what this program was made to do.
>>>2
/*Does what it is made for*/
-------------
(first time)
test.txt
180
1.3329
345.4
-------------
(next time)
test.txt
181
3.3329
348.4
-------------
Constant List:
Constant ONE 1
Constant TWO 2
Constant THREE 3
Sorry for all of the questions. I've googled this, i've irc'ed this, and I looked in a BOOK!!! and found very little. Thank you VERY MUCH for ANY help you provide. If you need me to explain what the program is for to make the (2) branch less vage, reply. Thanks again.
Ross