Thread: files & streams

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question files & streams

    Hey ladies and gents I am having a little problem with my code
    here it is
    Code:
    #include <iostream>	                      // cin, cout
    #include <fstream>	                       // ifstream, ofstream
    #include <string>                     // string, getline()
    #include <cassert>                    // assert()
    #include <cfloat>                     // DBL_MIN and DBL_MAX
    using namespace std;
    
    int main()
    {
    	cout << "This program computes the longest and shortest\n"
               "lines in a file, as well as the average number of\n"
               "characters per line.\n\n";
    	cout << "Enter the name of the input file: ";
    	string inputFileName;
    	getline(cin, inputFileName); 
    	cin.get();
    	ifstream inStream;                        //  to the input file,
        inStream.open(inputFileName.data());      //  establish connection,
                     //  and check for success
    	char ch;
    	unsigned int i=0,line=0, minLine=0, minChars=65535, maxLine=0, maxChars=0, totChars=0;
    	i = 0;
    	ch = cin.get();
    	while (ch != EOF)
    	{ /* Read until EOF is reached */
    		i++;    /* Count the characters*/
    		ch = cin.get();
    	if ( ch=='\n')
    	{
    		line++; /* increment when a newline is found*/
    		totChars += 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 <<", lines\n";
    	cout << "The average number of characters per line is: " << (totChars/(float)line);
    	cout << "Line: " << minLine <<" is the shortest line with: " << minChars << " characters\n";
    	cout << "Line: " << maxLine <<" is the longest line with: " << maxChars << " characters\n";
    	return 0;
    }
    it is supposed to read a txt file count the characters in each line then display the line number and length of the shortest and longest lines in the file na dthe average number of characters per line. but it just displays the first cout statement and then asks for file name and that is it. can anyone help

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    close() file?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So are you reading from console or a file?

    --
    Mats

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question

    I had put the curly braces in the wrong place i fixed it now it asks for the file and after three enters it will display the cout statement however my values are all wrong. Can someone please help me?

    Code:
    #include <iostream>	                      // cin, cout
    #include <fstream>	                       // ifstream, ofstream
    #include <string>                     // string, getline()
    #include <cassert>                    // assert()
    #include <cfloat>
    #include <conio.h>					// DBL_MIN and DBL_MAX
    using namespace std;
    
    int main()
    {
    	cout << "This program computes the longest and shortest\n"
               "lines in a file, as well as the average number of\n"
               "characters per line.\n\n";
    	cout << "Enter the name of the input file: ";
    	string inputFileName;
    	getline(cin, inputFileName); 
    	cin.get();
    	ifstream inStream;                        //  to the input file,
        inStream.open(inputFileName.data());      //  establish connection,
    												//  and check for success
    	char ch=cin.get();;
    	unsigned int i = 0,line = 0, minLine = 0, minChars = 65535, maxLine = 0, maxChars = 0, totChars = 0;
    	
    	while (ch != EOF)
    	{								// Read until EOF is reached 
    		i++;						// Count the characters
    		ch = cin.get();
    	if ( ch == '\n')
    	{
    		line++;						//increment when a newline is found
    		totChars += 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 <<", lines." << endl;
    	cout << "The average number of characters per line is: " << (totChars/(float)line)<< endl;
    	cout << "Line: " << minLine <<" is the shortest line with: " << minChars << " characters." << endl;
    	cout << "Line: " << maxLine <<" is the longest line with: " << maxChars << " characters." << endl;
    	
    	return 0;
    	}
    }

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by matsp View Post
    So are you reading from console or a file?

    --
    Mats
    a file called test1.txt that has like four lines with various amounts of characters basically garbage sentences.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So why is your code inside the loop of "while (ch != EOF)" reading from "cin"?

    --
    Mats

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Your cout statements now seem to be in your while loop. Dont do that. Move them after/outside it. Proper indentation would help other people see whats going on easier.

    You could also calcualte totChars at the end of the prog as i-line.

    [edit] Assuming you dont reset i at each new line[/edit]

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by gunghomiller View Post
    Code:
    inStream.open(inputFileName.data());      //  establish connection,
                                                    //  and check for success
    data() gives a string that is not null terminated. you want c_str().

    but the real problem is that you don't "check for success".

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by matsp View Post
    So why is your code inside the loop of "while (ch != EOF)" reading from "cin"?

    --
    Mats
    i thought that while the file i.e ch is not i.e != the end of file it will continue with the body of the program is that the wrong statement?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me explain AGAIN: You are reading "ch" from "cin" inside your big while-not-eof-loop. That is not the file you've just opened, it is the console. Should you type a lot of stuff on the keyboard, hitting enter a few times now and again and then hit CTRL-Z (on windows, if you are using Linux you should use "Ctrl-D" instead), then I suspect your program will output some sort of meaningfull data... But you probably don't really want it to do that, as you are reading a filename from the console, and it's kind of meaningless to do that when you read the rest of the data from the console. Is that more clear?

    --
    Mats

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    yes, unfortunately i dont know exactly what to do with this program to make it perform like it is supposed to.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gunghomiller View Post
    yes, unfortunately i dont know exactly what to do with this program to make it perform like it is supposed to.
    You probably need to understand a bit better how you use your stream objects then, don't you?

    "cin" is a "instream" that has been opened by the system to the console. You don't want to read from the console, so you will need to use another instream. Would that be a good enough clue without me actually writing the code for you?

    --
    Mats

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you mean istream

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    you mean istream
    Yes, of course I do. It's gone midnight, fingers are tired... :-)

    --
    Mats

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    unfortunately everything that i try doesnt work I dont want the code just tell me the line it is in I have been working on this thing for like 5 days now. just a little help would be great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. streams and files
    By chad101 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 11:54 AM
  5. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM