Another way to write to files?

This is a discussion on Another way to write to files? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream> #include <iostream> using namespace std; int main() { char recnam[50]; char text[120]; cout<< "Enter Recipe name \n"; ...

  1. #1
    Registered User Wolfmagi's Avatar
    Join Date
    Mar 2008
    Posts
    1

    Post Another way to write to files?

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char recnam[50];
        char text[120];
        
        cout<< "Enter Recipe name \n";
        cin.getline(recnam, 50, '\n');
        ofstream reci( "recipe.txt" );
        reci<< recnam<< '\n \n';
        cin.getline(text, 120, '\n');
        reci<< text<< '\n';
        cin.getline(text, 120, '\n');
        reci<< text<< '\n';
        cin.getline(text, 120, '\n');
        reci<< text<< '\n';
        cin.getline(text, 120, '\n');
        reci<< text<< '\n';
        reci.close();
    }
    this little program above will write 4 lines of text to a file named "recipe.txt"
    isn't there a better way to do this? (please say yes lol)
    I'm using a "recipe" as an example, but I want to be able to write, basically.. a text editor lol...
    I'm just starting so I grabbed bloodshed Dev-C++ IDE.
    Anyway.. can anyone help me?
    Last edited by Wolfmagi; 03-05-2008 at 11:15 AM. Reason: Change in source.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could certainly use a loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write to files ?
    By Akilla in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2002, 01:01 PM
  2. The problem read and write in files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 01:54 PM
  3. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM
  4. Can I not call fstream to write to two files sequentially?
    By tigeress in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 01:26 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21