about using input file (File I/O stream)

Discussion in 'C++' started by 01wlau, Jan 19, 2007.

  1. 01wlau

    01wlau New Member

    Joined:
    Jan 19, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I got a problem about File I/O stream. I have created a text file which is called input.txt which contains 80 characters in each line. The follwoing codes I did typed, but, I don't know how to use the input file to calculate the numbers of upper cases and the numbers of lines. Please help me!
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    #include <fstream>
    int main()
    {
    	ifstream fin1 ("input.txt");
    	if (!fin1)
    	{
    		cout << "Cannot open infile: input.txt\n";
    	}
    	else
    	{
    		cout << "Infile: input.txt opened.\n";
    		cout << "Number of spaces: ";
    		char ch[1000];
    		fin1.read(ch,1000);
    		int numchar = strlen(ch);
    		int count1 = 0;
    		for (int i=0;i<numchar;i++)
    		{
    				if (ch[i] == ' ')
    					count1++;
    		}
    		cout << count1;
    		cout << endl;
    		cout << "Number of upper cases: ";
    		int sum1 = 0;
    		for (int i=0; i<numchar;i++)
    		{
    			if (isupper(*ch))
    				sum1++;
    		}
    		cout << sum1;
    		cout << endl;
    		cout << "Number of lines: ";
    		cout << endl;
    	}
    	return 0;
    }
    Thanks a lot!!!
     
    Last edited by a moderator: Jan 19, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice