Thread: counting lines , chars from an input file need help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    counting lines , chars from an input file need help

    I got this program to finnally compile but cant seem to get it to read a single bit of data from the file. I have a input.txt file in the same directory. It crashes as soon as i put in the file name.

    Please assist. I am pulling my hair out on this one. Thanks in advance for your help.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.data());
    	assert(inFile.is_open());
    
    
    	
    	while (!inFile.eof())
            {
    			int ch;
                int i=0;
                int line;
                int totalChars;
                int minChars;
                int maxChars;
                int minLine;
                int maxLine;
                i++;    /* Count the characters*/
                if ( ch=='\n')
                    {
                        line++; /* increment when a newline is found*/
                        totalChars += i-1; /* Correction for \n (newline) character*/
    				}
                    if (i<minChars)
                        {
                            minChars = i;
                            minLine = line;
    					}
                        else if (i>maxChars || minChars>maxChars)
                            {
                                maxChars = i;
                                maxLine = line;
    						}
                            i = 0;
                            cout << "The input file has   " << line << "\n";
                            cout << "The average number of characters per line is    " << totalChars << "\n";
                            cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
                            cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n";
                            return 0;
    		}
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> fileName.data()
    Try c_str(). The ifstream constructor expects a null terminated character array, and data isn't guaranteed to be null terminated.

    Once you change that, I don't see where you ever actually read from the file.

    >> while (!inFile.eof())
    Generally, it is a bad idea to use eof() to control your while loop. I would use the return value of your read call (operator>>, getline, etc) instead.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    This compiles. It should work:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main() {
    	std::string file_name;
    	std::getline(std::cin, file_name);
    	std::ifstream file(file_name.c_str());
    
    	if(file.is_open()) {
    		int nb_lines = 0;
    		int nb_chars = 0;
    		int min_char = ~0;
    		int max_char = 0;
    		int min_line = 0;
    		int max_line = 0;
    
    		std::string tmp_line;
    		while(std::getline(file, tmp_line)) {
    			int tmp_size = tmp_line.size();
    
    			nb_lines++;
    			nb_chars += tmp_size;
    			if(min_char >= tmp_size) {
    				min_char = tmp_size;
    				min_line = nb_lines;
    			}
    			if(max_char <= tmp_size) {
    				max_char = tmp_size;
    				max_line = nb_lines;
    			}
    		}
    
    		/* Output data here... */
    	}
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    well the code you posted doenst work.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Still have the same problem. It doesnt even make it past the enter file name part. It does the gong,, error thing. I am entering input.txt as the file and it says this debug error. it refers first to "ch not being defined". and then on to the rest of my varibles. So i put ch=0; etc... program runs and puts 0's in for the answers.

    Please advise.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Post your latest code. Also, try putting in a full path to the file, just in case the program is looking in a different directory than you expect.

    >> So i put ch=0; etc... program runs and puts 0's in for the answers.
    Did you fix the part where I said you never read from the file?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    here is the latest:

    Now by defining the varibles all my output does is display what ever i initialize them too.

    Code:
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.c_str());
    	assert(inFile.is_open());
    
    	while (!inFile.eof())
            {
    			int ch=0;
                int i=0;
                int line=0;
                int totalChars=0;
                int minChars=0;
                int maxChars=0;
                int minLine=0;
                int maxLine=0;
                i++;    /* Count the characters*/
                if ( ch=='\n')
                    {
                        line++; /* increment when a newline is found*/
                        totalChars += i-1; /* Correction for \n (newline) character*/
    				}
                    if (i<minChars)
                        {
                            minChars = i;
                            minLine = line;
    					}
                        else if (i>maxChars || minChars>maxChars)
                            {
                                maxChars = i;
                                maxLine = line;
    						}
                            i = 0;
                            cout << "The input file has   " << line << "\n";
                            cout << "The average number of characters per line is    " << totalChars << "\n";
                            cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
                            cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n";
                            return 0;
    		}
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You never read from the file. Use operator>>, getline, get, or some other method to read data from the file into ch (hint, one of those is a better choice than the others).

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    I have went over the examples in the book. It has a command like inFile >> ch; I tried this and it had no effect. I compared two of the these types of programs in the book and dont see how i can use Getline from the file to ch as you state. Please give me a little more hints. I already have enough fraustration over this program as it is. Thanks for trying to help a dummy.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should try lining up your braces better. You will see that the last 5 lines of actual code, which look like they should be outside the while loop, are in fact in the while loop. That causes your program to return after the first character is read in.

    Using inFile>>ch will skip over whitespace characters like space, tab, and newline. Using getline will read an entire line into a string, so that is probably not what you want since the code goes one character at a time. The get() function gets a single character no matter what it is, including whitespace characters.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    i looked over the code made some adjustments. copiled it got an error with it not recognizing i as a valid declaration. moved some of my declarations out of the loop to see it would complile.

    It compliles fine. Still doesnt work. After putting the input.txt file in when asked. It just sits there; nothing happens.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    
    	int ch=0;
    	int i=0;
        int line=0;
        int totalChars=0;
        int minChars=0;
        int maxChars=0;
        int minLine=0;
        int maxLine=0;
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.c_str());
    	assert(inFile.is_open());
    
    	while (!inFile.eof())
            {
                int ch=0;
    			int i=0;
    			int line=0;
    			int totalChars=0;
    			int minChars=0;
    			int maxChars=0;
    			int minLine=0;
    			int maxLine=0;
    
    			
                i++;    /* Count the characters*/
                if ( ch=='\n')
                    {
                        line++; /* increment when a newline is found*/
                        totalChars += i-1; /* Correction for \n (newline) character*/
    				}
                    if (i<minChars)
                        {
                            minChars = i;
                            minLine = line;
    					}
                        else if (i>maxChars || minChars>maxChars)
                            {
                                maxChars = i;
                                maxLine = line;
    						}
    		}
            cout << "The input file has   " << line << "\n";
            cout << "The average number of characters per line is    " << totalChars << "\n";
            cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
            cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n";
            return 0;
    
    }

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Sorry about the code not having the change to the inFile >> ch; statement was put in after the while statement. still just sits there with nothing happening. man i am fraustrated. Never again will i take a distance learning course.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The inFile >> ch didn't work. The reason is that ch is declared as an int, so it expects to find an int as the first thing in the file. Most likely, the file has a character that is no a number, so it fails. Try something other than inFile >> ch to read in the character.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    Didn't I post working code earlier ? Oh well I guess everybody skipped it.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Sorry for asking again. But first the code you metion that should work doesnt compile. My file compiles but doesnt seem to count any of the data in the file. I am thinking it doesnt even use the file at this point. Please look over my code and tell me what is wrong.

    Please be specific and not cryptic. The suggestions i appreciate , but it really doesnt matter if ch is assigned char or int. It is something else that is not working causing the problem.

    Need help! This one is driving me crazy.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    
    	int ch;
    	int i=0;
        int line=0;
        int totalChars=0;
        int minChars=0;
        int maxChars=0;
        int minLine=0;
        int maxLine=0;
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.c_str());
    	assert(inFile.is_open());
        inFile >> ch;
    	while (!inFile.eof())
            {
    			
                
    			int i=0;
    			int line=0;
    			int totalChars=0;
    			int minChars=0;
    			int maxChars=0;
    			int minLine=0;
    			int maxLine=0;
    
    			
                i++;    /* Count the characters*/
                inFile >> ch;
                if ( ch == '\n')
                    {
                        line++; /* increment when a newline is found*/
                        totalChars += i-1; /* Correction for \n (newline) character*/
    				}
                    if (i<minChars)
                        {
                            minChars = i;
                            minLine = line;
    					}
                        else if (i>maxChars || minChars>maxChars)
                            {
                                maxChars = i;
                                maxLine = line;
    						}
    		}
    		cout << "The input file has   " << line << "\n";
    		cout << "The average number of characters per line is    " << totalChars << "\n";
    		cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
    		cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n";
    		return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 06:47 PM
  5. Replies: 3
    Last Post: 06-25-2003, 04:29 PM