473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

file read problem

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;
}
Dec 3 '07 #1
2 1217
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
Dec 3 '07 #2
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;
}
Dec 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ohaya | last post by:
Hi, I'm a real newbie, but have been asked to try to fix a problem in one of our JSP pages that is suppose to read in a text file and display it. From my testing thus far, it appears this page...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.