File handling:

This is a discussion on File handling: within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <cctype> #include <array> using namespace std; void counting(char ch, int list[]); void copyText(ifstream& in,ofstream& ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465

    File handling:

    Code:
     #include <iostream>
    #include <fstream>
    #include <cctype>
    #include <array>
    
    using namespace std;
    
    void counting(char ch, int list[]);
    void copyText(ifstream& in,ofstream& out,char& ch,int list[]);
    void initialize(int&,int[]);
    void writeTotal(ofstream& out, int loc, int list[]);
    
    void initialize(int&loc,int list[])
    {
        loc=0;
        for (int i=0; i<26; i++) {
            list[i]=0;
        }
    
    }
    void copyText(ifstream& in,ofstream& out,char& ch,int list[])
    {
        while (ch != '\n') {
            out<<ch;
            counting(ch,list);
            in.get(ch);
        }
        
        out << ch;
    }
    void counting(char ch, int list[])
    {
        ch = toupper(ch);
        int index = static_cast<int>(ch) - static_cast<int>('A');
        if (0 < index && index < 26) {
            list[index]++;
        }
        
    }
    void writeTotal(ofstream& out, int loc, int list[])
    {
        out<<endl<<endl;
        out<<"The number of line: "<< loc <<endl;
        for (int i = 0; i<26; ++i) {
            out<<static_cast<char>(i + static_cast<int>('A')) << " count = "<< list[i]<<endl;
        }
    
    }
    
    int main()
    {
        int letterCount[26];
        int lineCount;
        char ch;
        
        ofstream outfile;
        outfile.open("/Users/peterjocham/Desktop/textoutput.txt",ios::out);
        ifstream infile("/Users/peterjocham/Desktop/textinput.txt",ios::in);
        
        
        initialize(lineCount, letterCount);
        infile.get(ch);
        
        while (infile) {
            copyText(infile, outfile, ch, letterCount);  //counts each line
            lineCount++;
            infile.get(ch);
        }
        writeTotal(outfile, lineCount, letterCount);
        
        infile.close();
        outfile.close();
        
        return 0;
    }
    This what it is reading and below it is giving these numbers....

    A is given me 0 and clearly this is not true. Line 40-47 is the error I think...

    "I think it's fair to say that personal computers have become the most empowering tool we've ever created.They're tools of communication, they're tools of creativity, and they can be shaped by their user.


    Bill Gates"




    The number of line: 4
    A count = 0
    B count = 4
    C count = 7
    D count = 3
    E count = 25
    F count = 3
    G count = 2
    H count = 9
    I count = 11
    J count = 0
    K count = 1
    L count = 6
    M count = 6
    N count = 7
    O count = 16
    P count = 4
    Q count = 0
    R count = 11
    S count = 10
    T count = 20
    U count = 3
    V count = 4
    W count = 2
    X count = 0
    Y count = 6
    Z count = 0

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, look at your counting function. In which index do you intend to store any appearances of the letter A? What happens when you try to use that index?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file handling
    By ashaikh432 in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2010, 10:01 AM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  3. file handling
    By vaibhav in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 11:12 AM
  4. file handling ....!
    By Prasad kulkarni in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-12-2002, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21