file read problem

A

Art Cummings

Hello, i'm trying to read a file 24 lines at a time for a beginning c++
problem. The code doesn't stop when it has 24 lines but continues to read.
Can someone assist with why it doesn't stop at 24 lines?

Thanks
Art

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
string input, fileName;

cout << "Enter a file name: ";
getline( cin, fileName );

ifstream nameFile( fileName.c_str() );
if (!nameFile )
{
cerr << fileName << " The file could not be opened.\n";
system("PAUSE");
return 0;
}

int count = 0;
while( nameFile )
{
getline(nameFile, input);
cout << input << endl;
++count;

if( count==24 )
{
_getch();
count=0;
//system("PAUSE");
}
}
nameFile.close();
system("PAUSE");
return 0;
}
 
C

Cupu

Art said:
Hello, i'm trying to read a file 24 lines at a time for a beginning c++
problem. The code doesn't stop when it has 24 lines but continues to read.
Can someone assist with why it doesn't stop at 24 lines?

Thanks
Art

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
string input, fileName;

cout << "Enter a file name: ";
getline( cin, fileName );

ifstream nameFile( fileName.c_str() );
if (!nameFile )
{
cerr << fileName << " The file could not be opened.\n";
system("PAUSE");
return 0;
}

int count = 0;
while( nameFile )
{
getline(nameFile, input);
cout << input << endl;
++count;

if( count==24 )
{
_getch();
count=0;
//system("PAUSE");
}
}
nameFile.close();
system("PAUSE");
return 0;
}

Well your loop condition is while(nameFile) so as long as that returns
true ... you will continue to try to get lines from the file, either
you change (or add to) the condition or simply "break;" the loop when
you hit 24 lines.
 
J

Jim Langston

Art Cummings said:
Hello, i'm trying to read a file 24 lines at a time for a beginning c++
problem. The code doesn't stop when it has 24 lines but continues to
read. Can someone assist with why it doesn't stop at 24 lines?

Thanks
Art

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
string input, fileName;

cout << "Enter a file name: ";
getline( cin, fileName );

ifstream nameFile( fileName.c_str() );
if (!nameFile )
{
cerr << fileName << " The file could not be opened.\n";
system("PAUSE");
return 0;
}

int count = 0;
while( nameFile )
{
getline(nameFile, input);
cout << input << endl;
++count;

if( count==24 )
{
_getch();
count=0;
//system("PAUSE");
}
}
nameFile.close();
system("PAUSE");
return 0;
}

For me when I run this code it asks for filename, I enter it, it displays 25
lines and stops. I press a key, it displays anohter 25 lines and stops. I
press a key, it reads to the end of the file (my file has between 50 and 75
lines). Isn't that what you want it to do? If not, what is it you want?
You want it to stop reading totally after 25 lines? Where you have the
//system("PAUSE"); you can put a break; statement and it would break out of
the while loop.
 
A

Art Cummings

Thanks Jim, it doesn't do that with my file. It reads the entire file. I
wonder if there is something odd with the file i'm using.

Art
 
A

Art Cummings

Jim I changed the program and it's working now with my file, but I wanted to
control for the user just hitting the enter key not any key. I've tried
reading char 13 for the return character but when I use code for this, the
cursor moves but nothing is displayed on the screen.

Thanks
Art



#include <iostream>
#include <fstream>
#include <string>
#include <conio.h> //necessary for the _getch function

using namespace std;

int main()
{
const int SIZE = 81;
char fileName[SIZE];
char ch;
fstream file;

cout << "Enter a file name: ";
cin >> fileName;

//opens the file
file.open(fileName, ios::in);

if (!file)
{
cout << fileName << fileName << " could not be opened.\n";
system("PAUSE");
return 0;
}

int count = 0;
while(!file.eof())
{
cout << ch;
file.get(ch);
++count;

if( count==1920)// this is 24 * 80, to control for number of lines
displayed
{

count=0;
_getch();//causes a pause until the user hits a key
}

}
//closes the file
file.close();
system("PAUSE");
return 0;
}
 
J

James Kanze

Thanks Jim, it doesn't do that with my file. It reads the
entire file. I wonder if there is something odd with the file
i'm using.

What do you mean by "reads the entire file"? As Jim Langston
has pointed out, your code will hit the _getch() function
(whatever that does) every 25th line. On the other hand,
depending on its size, the file may have been physically read
and buffered; most implementations read files in more or less
large blocks (one or two KB seems to be a frequent size).

(A cleaner implementation would probably use nested loops:

while ( inFile ) {
std::vector< std::string > lines ;
std::string line ;
while ( lines.size() < maxLines
&& std::getline( inFile, line) ) {
lines.push_back( line ) ;
}

// process lines, or whatever...
// note that lines may be empty here, which
// may require special handling.
}

.)
 
J

James Kanze

Jim I changed the program and it's working now with my file,
but I wanted to control for the user just hitting the enter
key not any key. I've tried reading char 13 for the return
character but when I use code for this, the cursor moves but
nothing is displayed on the screen.
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h> //necessary for the _getch function

Of course, this means that a lot of us can't test your program,
since the header isn't present on most implementations.

If you're trying to implement some sort of pager, you'll
probably want to resort to some sort of implementation specific
input in the end, in order to read from the keyboard without
echoing. But for test purposes, just doing a std::getline() on
std::cin might be more appropriate when posting here.
using namespace std;
int main()
{
const int SIZE = 81;
char fileName[SIZE];
char ch;
fstream file;
cout << "Enter a file name: ";
cin >> fileName;

This line may cause undefined behavior, if e.g. the user enters
a filename of more than 80 characters. And it will get the
wrong filename if the filename contains a blank (quite frequent
under Windows). Much better would be:

std::string filename ;
std::getline( std::cin, filename ) ;
if ( ! std::cin ) {
// You never know, but if the user has redirected
// input to an empty file, or something.
// For this sort of error, you can probably just
// abort, with an error message.
}
std::ifstream input( filename.c_str() ) ;
if ( ! input.is_open() ) {
// ...
}
//opens the file
file.open(fileName, ios::in);
if (!file)
{
cout << fileName << fileName << " could not be opened.\n";
system("PAUSE");
return 0;
}
int count = 0;
while(!file.eof())

And this is definitely wrong. You should never call ios::eof()
until after an input has failed.
{
cout << ch;

And the first time you get here, ch has never been initialized.

If you're looping copying characters, the correct idiom is:

while ( file.get( ch ) ) {
std::cout.put( ch ) ;
// ...
}

(Obviously, you can also use "std::cout << ch". I just feel put
more natural for output when you're using get for input.)
file.get(ch);
++count;
if( count==1920)// this is 24 * 80, to control for number of lines
displayed

Except that it doesn't control the number of lines displayed.
It controls the number of characters. If the file consists in a
lot of very short lines, you're going to display a lot more than
24 lines. If it has very long lines, you're going to display
less.
{
count=0;
_getch();//causes a pause until the user hits a key
}
}
//closes the file
file.close();
system("PAUSE");
return 0;
}

As I mentionned in another posting, I'd use nested loops here,
reading all 24 lines into memory. If I were really
implementing some sort of pager, I'd also add handling for long
lines, either wrapping them---and counting the extra lines thus
generated---or truncating them. And probably offer several
different commands, one for advancing one line, one for a
complete page, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top