Thread: Reading/Writing Files

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    7

    Reading/Writing Files

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. I am not sure which mode I use for creating and writing (although I think it is "w")
    That'll be fine.

    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()?
    If you dont' need binary mode (ie: you want a text file), then just use fprintf.

    I know there is fclose(file pointer) but is there also a save function?
    Writing puts it in the file, fclose flushes output and "saves" the file so to speak.

    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:
    Here is all the reference you need for which way to open the file. "r+" opens for reading and updating.

    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.
    Try fgets for reading a line at a time. Then combine it with something like sscanf to pull what you need from your buffer. Alternately, you could just use fscanf to read from the file. You will want to be sure and check the return value for your functions to make sure you're reading all of what you think you should.

    For line numbers, sounds like you want a loop to print the number followed by the line or whatever.
    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).
    Either way is fine.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    that's a lot of questions,
    you open a file with fopen ( filename, mode ) , where mode can be "r" -- read , "w" -- write , "a" -- append. To print float numbers to a text file use fprintf(fp, "%f", float); . To read a whole line use fgets, then to convert that line into float you can use atof or sscanf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux IPC - reading/writing files, getting garbage
    By kbfirebreather in forum C Programming
    Replies: 9
    Last Post: 02-01-2009, 02:55 PM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM