Thread: files

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Question files

    I need code to open up a file
    Thank you for your help

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65

    Re: files

    include <fstream>

    main()
    {
    ifstream inputfile("inp.txt"); // for input
    ofstream outputfile("out.txt"); // for output
    fstream both("both.txt", ios::in | ios:ut) // for input output.
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    Thank You
    But i was also looking for the code to put each line into a data array once opened

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    // just a simple class
    class sth {
    public:
    int id;
    int year;
    }

    main()
    {
    ifstream file("file1.txt", ios::in);
    if (file.fail()) {
    cout << "file cannot be opened";
    return 0;
    }
    sth *array = new sth[10];

    // input ten sth from the file. I assume there are ten records
    // in file
    for (i = 0; i < 10; i++) {
    file >> array[i].id;
    file >> array[i].year;
    }
    file.close();

    ofstream file2("file2.txt", ios:ut);
    if (file2.fail()) {
    cout << "file cannot be opened";
    return 0;
    }

    // writes the array back to another file.
    for (i = 0; i < 10; i++) {
    file2 << array[i].id;
    file2 << array[i].year;
    }
    file2.close();
    delete [] array;
    return 0;
    }

    Another ex:

    main()
    {
    sth *array = new sth[40];
    ifstream file("file");

    while (!file.eof()) {
    file >> array[i].id;
    if (!file.eof())
    file >> array[i].year;
    i++;
    }
    file.close();
    delete [] array;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. 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
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM