Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++ Reading Files


  • Please log in to reply

#1
Kevin39

Kevin39

    Member

  • Member
  • PipPip
  • 18 posts
To whom it may concern;

I a writing a program for my CS Course that reads a text file and calculates the number of characters per line, the longest and shortest lines and the average characters per line. I have created a text file and have the program attemting to read the file but it fails to find it. HELP.
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Why don't you post your code where you open the file so we can fave a look at what might be going wrong.

-Ricci
  • 0

#3
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Here is what i have so far:

/*This program read a text file and calculates the charaters
* per line, length of shortest and longest lines, and average
* number of charaters per line.
*
*Input: Text file to read
*Output: Number of characters per line, Average, and longest and
* shortest line.
*****************************************************************/

#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <cassert> //assert()
#include <string> //string, getline()

using namespace std;

int main()
{
string Characters;

cout << "This program reads a text file and calculates the\n"
"total count of characters per line, line # of longest\n"
"and shortest lines and Average characters per line.\n\n";

cout << "Open the Lines.txt file: \n";

ifstream inStream;

inStream.open("Lines.txt");

int charCount = 0;
int lineNum = 0;
double reading,
average,
longest,
shortest;
{
if (inStream.fail())
cerr << "Error! Couldn't open " << "Lines.txt" << " file for reading ";
for

inStream >> Characters;
characters.get(theNewLine);
getline(inStream, Characters);
Characters = charCount;


}
cout << "\n--> There are " << charCount << " characters per line. ";
inStream.close();
}
  • 0

#4
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hey Kevin,

I modified your code so it would compile (looks like you were in the middle of something when you posted it), and it works fine on my machine. You probably need to make sure your Lines.txt file is in the right directory. I'm using Visual C++ 2003 - I know, not a favorite here, but it's what I use at work - and I noticed that if I wanted to run from the debugger, my text file had to be in the directory that contains my source code. However, if I wanted to run from the command line, my text file had to be in the same directory as the executable - in my case the "debug" directory off of my source directory. You might check to see if you have a similiar situation with whatever compiler you're using.

-Ricci
  • 0

#5
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Thank you,

Now the program opens the file, but it will not read the number of characters in the Text file.

It only displays "0 characters per line", based on the cout statement. Any ideas?

Thank you for you assistance.
  • 0

#6
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
I think we need to see the up-to-date version of your code again. As I mentioned before, the code you posted doesn't quite compile. :tazz:

-Ricci
  • 0

#7
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Here is the updated code, i have added somethings to the code and now it reads the file, but will only display either the Name in the line or a blank where the charCount should display.

#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <cassert> //assert()
#include <string> //string, getline()

using namespace std;

int main()
{
string Characters;
char theNewLine;

cout << "This program reads a text file and calculates the\n"
"total count of characters per line, line # of longest\n"
"and shortest lines and Average characters per line.\n\n";

cout << "Open the Lines.txt file: \n";

ifstream inStream;

inStream.open("Lines.txt");

int charCount = 0;
int lineNum = 0;
double reading,
average,
longest,
shortest;

if (inStream.fail())
cerr << "Error! Couldn't open " << "Lines.txt" << " file for reading ";

for (;:tazz:

{
inStream >> Characters;
inStream.get(theNewLine);
getline(inStream, Characters);

if (inStream.eof()) break;

Characters = charCount;
}


cout << "\n--> There are " << Characters << " characters for the first line. ""\n";
inStream.close();
}
  • 0

#8
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
i changed the Characters in the cout statement back to charCount and it still displays 0 number of characters.

Thanks
  • 0

#9
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Ok, for starters, you're never assigning anything to charCount, so you're never going to have any valid output. I'd take a look there first.

-Ricci
  • 0

#10
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
it finally reads the file and displays the number of characters in the first line, now how would i get the program to read the next line in the text file?
  • 0

Advertisements


#11
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Lets see your new code.
  • 0

#12
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Here is the new code.

#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <cassert> //assert()
#include <string> //string, getline()

using namespace std;

int main()
{
string Characters;
char theNewLine;

cout << "This program reads a text file and calculates the\n"
"total count of characters per line, line # of longest\n"
"and shortest lines and Average characters per line.\n\n";

cout << "Open the Lines.txt file: \n";

ifstream inStream;

inStream.open("Lines.txt");

if (inStream.fail())
cerr << "Error! Couldn't open " << "Lines.txt" << " file for reading ";

int charCount = 0;
int lineNumber = 0;
double reading,
average,
longest,
shortest;

for (;:tazz:
{
inStream.get(theNewLine);
getline(inStream, Characters);

if (inStream.eof()) break;

charCount = Characters.size() + 1;

}
cout << "\n--> There are " << charCount << " characters in line number "<< lineNumber <<".\n";
inStream.close();


}
  • 0

#13
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts

it finally reads the file and displays the number of characters in the first line, now how would i get the program to read the next line in the text file?

Actually, it is displaying the number of characters in the second-to-last line (sort of).

You have a "for" loop on line 33 of your code that loops through the contents of the file. Each time it gets a value roughly relating to the length of the line and stores it in charCount. The problem is that you don't do anything with that value before you loop again and put another value in its place. When the program gets to the end of the file, only the value for the last line is stored, and that's what you are printing to the screen.

If you want to display that value for each line, try moving your last cout statement up to the last line inside the loop, like so:
for (;;)
	{ 
		inStream.get(theNewLine);
		getline(inStream, Characters);

		if (inStream.eof()) break;

		charCount = Characters.size() + 1;

		cout << "\n--> There are " << charCount << " characters in line number "<< lineNumber <<".\n";
	} 

	inStream.close();
}
Another problem you're going to run into is that you test for the end of your file and break out of the loop before you compute the number of characters and display your output. For starters with this problem, try moving the statement "if (inStream.eof()) break;" to the end of the loop. It's not the perfect fix to the problem, but it will give you something to work with for the moment.

I'm going to mention one more problem that you'll want to take care of eventually. In your loop, your first line is "inStream.get(theNewLine);" followed by "getline(inStream, Characters);". This is essentially stripping off the first character of the line before reading the rest of it. Is that really what you want to do? You'll need to think about this some more I believe.

So start with those things, and there will be more problems to solve after that. Hopefully you have a sufficient understanding of your own code that you will be able to tackle some of them on your own. If you get stumped, post again and we'll take another look.

-Ricci
  • 0

#14
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
I am able to determine number of characters per line.

Now i need to calculate which line has the most characters and which line has the least number of characters, and the average number of characters per line.

Here is the code so far:

#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <cassert> //assert()
#include <string> //string, getline()

using namespace std;

int main()
{
string Characters;
char theNewLine;

cout << "This program reads a text file and calculates the\n"
"total count of characters per line, line # of longest\n"
"and shortest lines and Average characters per line.\n\n";

cout << "Open the Lines.txt file: \n";

ifstream inStream;

inStream.open("Lines.txt"); //Opens the Text file.

if (inStream.fail()) //If opening the file fails
cerr << "Error! Couldn't open " << "Lines.txt" << " file for reading ";

int charCount = 0;
int lineNumber = 0;
double average, longest, shortest;

for (;:tazz: //Loop to read the text file data.
{
getline(inStream, Characters);



charCount = Characters.size() -1; //Convert characters to number of characters
lineNumber++;

cout << "\n--> There are " << charCount << " characters in line number "<< lineNumber <<".\n";
if (inStream.eof()) break;
}


//cout << "\n--> Line Number "<< mostCharacters <<" has the most characters in it.\n";
//cout << "\n--> Line Number "<< leastCharacters <<" has the least characters in it.\n";

//cout << "\n--> The average number of characters per line is << Average <<".\n";
inStream.close();

}
  • 0

#15
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Kevin,

If someone just tells you how to do it, you're not going to benefit at all. You need to try to come up with something, and if you run into problems, then you can explain what you're trying to do and ask for ideas.

To get you started, try thinking about the problems one at a time. Start with which line has the most characters. How would you figure that out manually? Seems like you would have to look at each line and remember which one is biggest.....

There are a couple of things you will still need to examine in your code in its current state. First, is your code really getting the length of each line? You might want to double check your math. Second, what will happen with your program if the text file exists but doesn't actually contain anything? And third, (yes I know I only said a couple) what about if the file doesn't exist at all? Oh, and one last thing, what if there is a blank line in your file?

Good luck,
Ricci
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP