Thread: text files

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    text files

    text files



    is there anyway i can use textfiles i created in a program


    because writing cout a bunch of times can get tiring...

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    ty so so much and why did you double everything ?one for c and one for c++?

    where did your post go i found it helpfull

    well heres what
    Well, I would sure hope so lol.

    This is the method I learned, so far.

    Use #include <fstream>

    Declare variables like

    ifstream fin;
    ofstream fout;

    You connect them to files with the "open" member function.

    fin.open("file name");
    fout.open("file name");

    Close them when your done.

    fin.close();
    fout.close();

    And you can do things like

    fin >> variable; ect

    A little code:
    Code:
    
    Code:
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    static int spaces = 0;
    using namespace std;
    void edit_file(ifstream & fin, ofstream & fout);
    int check_word(ifstream & fin, ofstream & fout);
    int main()
    {
        ifstream fin;
        ofstream fout;
        fin.open("C:\\Documents and Settings\\Stephen\\My Documents\\New Folder\\letter.txt");
        if(fin.fail())
        exit(1);
        fout.open("C:\\Documents and Settings\\Stephen\\My Documents\\New Folder\\letter2.txt");
        if (fout.fail())
        exit(1);
        cout << "Editing..." << endl;
        edit_file(fin, fout);
        cout << "Done." << endl;
        fin.close();
        fout.close();
        return 0;
    }
    I dunno though. That method might be bad...
    Last edited by lilhawk2892; 07-04-2006 at 10:54 PM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    I thought maybe I read your post wrong.

    Anyway...


    This is the method I learned, so far.

    Use #include <fstream>

    Declare variables like

    ifstream fin;
    ofstream fout;

    You connect them to files with the "open" member function.

    fin.open("file name");
    fout.open("file name");

    Close them when your done.

    fin.close();
    fout.close();

    And you can do things like

    fin >> variable; ect

    A little code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    static int spaces = 0;
    using namespace std;
    void edit_file(ifstream & fin, ofstream & fout);
    int check_word(ifstream & fin, ofstream & fout);
    int main()
    {
        ifstream fin;
        ofstream fout;
        fin.open("C:\\Documents and Settings\\Stephen\\My Documents\\New Folder\\letter.txt");
        if(fin.fail())
        exit(1);
        fout.open("C:\\Documents and Settings\\Stephen\\My Documents\\New Folder\\letter2.txt");
        if (fout.fail())
        exit(1);
        cout << "Editing..." << endl;
        edit_file(fin, fout);
        cout << "Done." << endl;
        fin.close();
        fout.close();
        return 0;
    }
    I dunno though. That method might be bad...

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    lol i just posted your post in my edited post thanks by the way

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    :P

    Quote Originally Posted by lilhawk2892
    ty so so much and why did you double everything ?one for c and one for c++?
    ?

    You mean my headers?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    no like
    fin.open("file name");
    fout.open("file name");

    its almost doubled.

    it looks like its fin = printf
    fout = cout
    c c++

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Oh...fin and fout are just variable names. Use whatever you want. Its just what my book uses. And fin is of type ifstream, which you use to get input, and fout is of type ofstream, which you use for output.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    which book my birthdays on the 28th and i cant get an ipod anymore so i wanna get some c++ books(among other things)

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    A book from the 80's i think lol. You better get something recent.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    lol im thinking of c++ for dummies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM