Thread: C++ question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    C++ question

    How can I open binary file in C++?
    I wanna open a text file.
    thx!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    maybe


    #include <fstream.h>

    ifstream File("MyFile.ext", ios::binary);


    I believe it's something like this. Might be wrong though... I still use the old C file pointer way
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User programmer's Avatar
    Join Date
    Oct 2001
    Posts
    22

    Thumbs up

    Not sure if this is what your after, but in its most basic form, i'd do it like this:

    #include <fstream.h>

    main()
    {
    char ch;
    char filename[20] = "c:test.txt";
    int mode = ios:ut;
    fstream fout(filename, mode );
    cout << "Ready for input, use CTRL+Z to end" << endl;
    while ( cin.get( ch ) )
    {
    fout.put( ch );
    }
    fout.close();
    return(0);
    }

    fout.close(); is important, miss it and you could **** up your whole hd.

    to open the file:

    #include <fstream.h>

    main()
    {
    char ch;
    char filename[20] = "c:test.txt";
    int mode = ios::in;

    fstream fin(filename, mode );
    if (!fin)
    cout << "Unable to open file";
    while ( fin.get(ch) )
    {
    cout << ch;
    }
    fin.close();
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM