Thread: Reading Files

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Reading Files

    I am stuck on the following question:

    8. Write a program that opens a text file, reads it character-by-character to the end of
    the file, and reports the number of characters in the file.

    9.Do Programming Exercise 6 but modify it to get information from a file.The first
    item in the file should be the number of contributors, and the rest of the file should
    consist of pairs of lines, with the first line of each pair being a contributor’s name
    and the second line being a contribution.That is, the file should look like this:
    4
    Sam Stone
    2000
    Freida Flass
    100500
    Tammy Tubbs
    5000
    Rich Raptor
    55000

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct donors
    {
        string name;
        double price = 0;
    };
    
    int main()
    {
        int no_donors = 0;
    
        ifstream openFile;
        openFile.open("FileInput.txt");
    
        if (!openFile.is_open())
        {
            exit(EXIT_FAILURE);
        }
    
        openFile >> no_donors;
        donors* b = new donors[no_donors];
        while (openFile.good())
        {
            cout << "File opened sucessfully: ";
            for (int i = 0; i < no_donors; i++)
            {
                getline(openFile, b[i].name);
                (openFile >> b[i].price).get();
            }
        }
    
        if (openFile.eof())
            cout << "End of file has been reached.\n\n";
        else if (openFile.fail())
            cout << "Input terminated by data mismatch.\n\n";
        else
        {
            cout << "Input terminated for unknown reason.\n";
            openFile.clear();
            cin.clear();
            cin.get();
            exit(EXIT_FAILURE);
        }
    
        for (int j = 0; j < no_donors; j++){
            cout << b[j].name;
            cout << b[j].price << endl;
        }
    
        delete[] b;
        cin.get();
        cin.get();
        return 0;
    }
    My program seems to have read the information in from the file however the problems here as this piece of code is not working.

    Code:
        for (int j = 0; j < no_donors; j++){
            cout << b[j].name;
            cout << b[j].price << endl;
        }
    Does anyone know why?
    Last edited by immy; 07-31-2014 at 03:08 PM.
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  2. #2
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    I solved the problem, it was because I didnt make the program read the first integer as a line. E.g.
    Code:
     (openFile >> no_donors).get();
    Disregard this thread lol.
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  2. Reading xls files
    By dlwlsdn in forum C Programming
    Replies: 20
    Last Post: 12-04-2008, 07:38 AM
  3. Reading files
    By Quantrizi in forum C Programming
    Replies: 4
    Last Post: 04-14-2004, 11:15 AM
  4. reading files
    By MrJCake in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2003, 06:42 AM
  5. reading from files????????
    By devour89 in forum C++ Programming
    Replies: 10
    Last Post: 11-15-2002, 08:22 AM