GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Aug-2004, 13:50
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Question

Using files for input


My latest problem involves using a file for input, rather than user input. My program below is based on my book, but it doesn't like the input file I tell it.

I'm currently coding it using Anjuta 1.2.2 in Libranet Linux 2.8.1. I don't know if I need to change the code because Linux has a different file structure than Windows; it shouldn't make a difference, right?

I created a sample text file in my /home/cody directory; on execution, the program gave a

Code:
int main(): Assertion `inputStream.is_open ()' failed.

If I make a copy of the file and put it in the directory where the program is, then the program just sits there after I type in the file name. If I tell it expressly where the text file is (e.g. /home/cody/Text_File), it also sits there.

Do I need to modify my code for Linux or is it how I enter the file in the program when asked?

CPP / C++ / C Code:
/*
*Purpose:  Read a text file and count the characters in each line.  Display the
*	line numbers and length of shortest and longest lines, plus average length.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

int main ()
{
	cout << "This program will read a text file and count the characters in\n" 
		<< "each line.  It will then display the line number and length of\n" 
		<< "shortest and longest lines, plus average length.\n\n";

	//---------------Input Section-----------------------------
	cout << "Enter the input file name: ";
	string input_file;
	getline (cin, input_file);
	ifstream inputStream;					//open input stream to file
	inputStream.open (input_file.data ());	//establish connection to file
	assert (inputStream.is_open ());		//ensure input stream open

	char line_char;		//value for line processing
	int line_num = 1,	//values for line counting and line lengths
	  short_line = LONG_MIN, long_line = LONG_MAX, avg_line;
	int indiv_count;

	for (;;)
	{
		indiv_count = 0;			//resets character counter for each line
		inputStream >> line_char;	//read a character
		if (inputStream.eof ())		//quit loop when EOF
			break;
		while (line_char != '\n')	//count characters in line
		{
			indiv_count++;
		}
		if (line_char = '\n')	//track how many lines and min, max lengths
		{
			++line_num;
			if (indiv_count < short_line)
				short_line = indiv_count;
			if (indiv_count > long_line)
				long_line = indiv_count;
		}
		inputStream.close ();
	}

	//--------------------Output Section---------------------------
	cout << "\n\nFile " << input_file << " has " << line_num <<	" lines.\n" 
		<< "The shortest line is " << short_line << ".\n"
		<< "The longest line is " << long_line << ".\n" 
		<< "The average line length is " << ((long_line-short_line)/2) << ".\n" 
		<< endl;
	return 0;
}

Thanks for everyone's help. :-)
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 28-Aug-2004, 14:53
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Problem opening the file? Instead of...
CPP / C++ / C Code:
inputStream.open (input_file.data ());

Try this...

CPP / C++ / C Code:
inputStream.open (input_file.c_str());
__________________
-Aaron
  #3  
Old 28-Aug-2004, 17:49
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
No joy. I made the change and it still doesn't work. Exact same problems as before.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 29-Aug-2004, 08:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Well, look at
CPP / C++ / C Code:
   inputStream >> line_char;  //read a character
    if (inputStream.eof ())    //quit loop when EOF
      break;
    while (line_char != '\n')  //count characters in line
    {
      indiv_count++;
    }

You have read a single character into your char variable line_char. If the first character on the first line is not '\n', it's an infinite loop.

Also note

CPP / C++ / C Code:
    if (line_char = '\n')  //track how many lines and min, max lengths

I'll bet it should be
CPP / C++ / C Code:
    if (line_char == '\n')  //track how many lines and min, max lengths

(A real common typographical error, and sometimes hard to spot in the middle of lots of otherwise good code.)

Good Luck!

Dave
  #5  
Old 29-Aug-2004, 11:29
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Crap. I didn't notice that :-(. I'll try that and see what happens. Thanks.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #6  
Old 29-Aug-2004, 11:45
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Okay. Now it's reading the file (I think). At least it gives me an output. But there's obviously something wrong in my logic. Here's my new code:
CPP / C++ / C Code:
for (;;)
	{
		indiv_count = 0;			//resets character counter for each line
		inputStream >> line_char;	//read a character
		if (inputStream.eof ())		//quit loop when EOF
			break;
		if (line_char != '\n')	//count characters in line
		{
			indiv_count++;
		}
		if (line_char == '\n')	//track how many lines and min, max lengths
		{
			++line_num;
			if (indiv_count < short_line)
				short_line = indiv_count;
			if (indiv_count > long_line)
				long_line = indiv_count;
		}
		inputStream.close ();
	}

Here's my output:
Code:
File test_file has 1 lines. The shortest line is -2147483648. The longest line is 2147483647. The average line length is 0.
This same thing happens no matter what text is in the test file. Now I modified the code from my textbook, which counted real numbers. I haven't had the best of luck working w/ strings so I'm sure my logic is flawed somewhere inside the loop. Can someone see where I screwed up?

Thanks. I really appreciate the help; it's so much faster (and more informative) than waiting for my instructor to email me back.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #7  
Old 29-Aug-2004, 13:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
Okay. Now it's reading the file (I think). At least it gives me an output. But there's obviously something wrong in my logic. Here's my new code:
CPP / C++ / C Code:

			if (indiv_count < short_line)
				short_line = indiv_count;
			if (indiv_count > long_line)
				long_line = indiv_count;
	

Here's my output:
Code:
The shortest line is -2147483648. The longest line is 2147483647.
; it's so much faster (and more informative) than waiting for my instructor to email me back.

Here's something even faster and even more valuable to your productivity: put output statements at various places in your program to see what's happening.

For example in the above comparisons, what are you actually comparing:

What are the current values of short_line and long_line? What is the value of indiv_count? I could tell you, but why don't you try it yourself:

CPP / C++ / C Code:

  cout << "For line " << line_num << ", indiv_count = " << indiv_count
       << ", short_line = " << short_line 
       << ", long_line = " << long_line << endl;

  if (indiv_count < short_line)
    short_line = indiv_count;
  if (indiv_count > long_line)
    long_line = indiv_count;

C++ programmers: cout<< is your friend.

C programmers: printf() is your friend.

Best regards from your other friend,

Dave
Last edited by davekw7x : 29-Aug-2004 at 14:16.
  #8  
Old 29-Aug-2004, 15:39
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Thumbs down

Still confused


Well, I see what I suspected: after initializing short_line and long_line to the min/max values, they stay that way while in the loop. It also appears that trying to identify the newline doesn't seem to work; actually, it doesn't appear to loop at all.

I put in 3 cout>> groups: one prior to the eof check, one right after ++line_num, and one after the relationship if statements. It appears the final one isn't seen, as no output was given.

My thought process is that after the relationship checks, the default values of "short_line" and "long_line" should be changed to whatever "indiv_count" is, but that doesn't appear to be happening. It's almost like the loop gets to the second "\n" if statement and stops. I'm getting very confused.

But, thanks for the help.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #9  
Old 29-Aug-2004, 17:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
Well, I see what I suspected: after initializing short_line and long_line to the min/max values, they stay that way while in the loop. It also appears that trying to identify the newline doesn't seem to work; actually, it doesn't appear to loop at all.

I put in 3 cout>> groups: one prior to the eof check, one right after ++line_num, and one after the relationship if statements. It appears the final one isn't seen, as no output was given.

My thought process is that after the relationship checks, the default values of "short_line" and "long_line" should be changed to whatever "indiv_count" is, but that doesn't appear to be happening. It's almost like the loop gets to the second "\n" if statement and stops. I'm getting very confused.

But, thanks for the help.


How about this:

CPP / C++ / C Code:
cout << "linechar = " << line_char << endl;
right after you read each char.

I think you will find that cin>>line_char eats white space! (Including the newline)

try
CPP / C++ / C Code:
line_char = inputStream.get();

Now, seasoned C++ programmers probably use some other method, but I think you can get yours to work if you keep at it.

There are lots of other errors that you can pick out after you get the program to do something.

(For example you set indiv_count to 0 before you read each character!. You should set it to zero befor entering the loop, and set it to zero after you detect a '\n'.)

There are lots of other hints that will jump out of the page after you get each line read and collect statistics.

Good Luck,

Dave
  #10  
Old 29-Aug-2004, 17:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
Well, I see what I suspected: after initializing short_line and long_line to the min/max values, they stay that way while in the loop.

I wanted to separate this from the other, more serious error (couldn't see the newline, so couldnt count lines).

Now, look at what you do woth short_line and long_line.

If you did the following before each comparison, you should see why short_line stays at its initial value:
CPP / C++ / C Code:
 cout << "short_line = " << short_line << endl;

What did you see: short_line = some verrrrry large negative number.
Now, can your line length ever be less than that? I don't think so.

So initialize short_line to some large positive number, then do your comparison.

Similarly, you initialized long_line to some verrrrry large positive number.
Now, can your line length ever be greater than that? I don't think so.

Are you getting the picture?

Regards,

Dave
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 14-Jul-2004 23:30
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
this is from 6 files now i need fancy reports to write to the files kilgortrout C++ Forum 1 21-May-2004 16:57
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 09:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.