Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: c++

  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    c++

    Hi
    i want to make a small program that read and write to a file, so i want to know the commands that used to read and write to a file.
    with many thanks

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    #include <iostream.h>
    #include <fstream.h>

    int main() {
    ifstream myInput("somefile.txt");
    char inp;
    while (myInput >> inp) {
    cout << inp;
    }

    return 0;
    }

    For example. ifstream objects also have a few other methods, such as readline().

    Writing to a file is easy:

    ofstream output("myoutput.txt");
    output << "hello world!";

    writes "hello world!" to myoutput.txt

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •