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.
C++ Reading Files
Started by
Kevin39
, Nov 11 2005 10:42 AM
#1
Posted 11 November 2005 - 10:42 AM
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.
#2
Posted 11 November 2005 - 04:27 PM
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
-Ricci
#3
Posted 14 November 2005 - 07:22 AM
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();
}
/*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();
}
#4
Posted 14 November 2005 - 10:55 AM
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
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
#5
Posted 14 November 2005 - 11:26 AM
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.
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.
#6
Posted 14 November 2005 - 11:35 AM
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.
-Ricci
-Ricci
#7
Posted 14 November 2005 - 11:38 AM
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 (;
{
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();
}
#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 (;
{
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();
}
#8
Posted 14 November 2005 - 11:40 AM
i changed the Characters in the cout statement back to charCount and it still displays 0 number of characters.
Thanks
Thanks
#9
Posted 14 November 2005 - 11:45 AM
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
-Ricci
#10
Posted 14 November 2005 - 12:30 PM
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?
#11
Posted 14 November 2005 - 12:35 PM
Lets see your new code.
#12
Posted 14 November 2005 - 12:39 PM
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 (;
{
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();
}
#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 (;
{
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();
}
#13
Posted 14 November 2005 - 01:16 PM
Actually, it is displaying the number of characters in the second-to-last line (sort of).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?
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
#14
Posted 14 November 2005 - 01:47 PM
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 (; //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();
}
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 (; //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();
}
#15
Posted 14 November 2005 - 03:07 PM
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
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
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users