Hey everyone, I'm new to posting in these forums so sorry if I didn't post my code correctly? I think i did it...hopefully.
Anyways,
I've been working on this all day but I can't figure it out. (i think i will be having problems with the rest of my assignments in this chapter =( ...sigh)

What I need to do:
- Ask user what file to open
- Display contents of file
----display 24 lines, user will strike any key to continue with the next 24 lines


So my code compiles, but it shows me character by character
Please help me with how to display every 24 lines!!
I can't for the life of me figure it out and i've seriously tried all day.
I've read my book and still can't figure it out.

Hints given to me, but i still don't know where to put it / what to put/ how to do it:
- Use an if statement to check if the character is '\n'. If so, that is a new line and can add 1 to the line counter.
- To pause after 24 lines, you can use an integer constant for # of lines per page and use cin.getline to get enter key.
example code for this:

char buff[160];
     
    cout << "\n"
         << "(more) ";
    cin.getline( buff, sizeof buff );

So here's what I have right now =/

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	const int SIZE = 51;	// Array size for file name
	char fileName[SIZE];	// Hold file name
	char ch;				// Hold a character
	fstream file;			// File stream 

	// Get file name from user
	cout << "   Enter the file name you wish to open and read: ";
	cin.getline(fileName, SIZE);

	// Open the file
	file.open(fileName, ios::in);
	if (!file)
	{
		cout << fileName << " could not be opened.\n";
		return 0;
	}

	// Get each character from file and display
	char buff[160];
	file.get(ch);
	while ( !file.eof() )
	{
		cout << ch;
		file.get(ch);
		cout << "\n"
			 << "(more) ";
		cin.getline( buff, sizeof buff );
	}

	// Close the file.
	file.close();

	return 0;
}

Mm..I'm not sure how to edit my post? I figured out how to read line by line...But I don't know if I did it correctly. I just changed from get (ch) to the getline.....And changed the "const int SIZE" to a higher number.

So now, when i open the file, it reads one line, I hit enter, the next line, and so on.

I still can't figure out how to read 24 lines at a time :( Or where I should put the if statement and what that would do and how to link it all together?

***
Oh, I also need to figure out how to number each line of the text file.. with a number and a colon
like:
1: blah blah line 1
2: blah blah blah line 2
****

So frustrated =/

Here's my updated code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	const int SIZE = 160;	// Array size for file name
	char fileName[SIZE];	// Hold file name
	char ch;				// Hold a character
	fstream file;			// File stream 

	// Get file name from user
	cout << "   Enter the file name you wish to open and read: ";
	cin.getline(fileName, SIZE);

	// Open the file
	file.open(fileName, ios::in);
	if (!file)
	{
		cout << fileName << " could not be opened.\n";
		return 0;
	}

	// Get each character from file and display
	char buff[160];
	file.getline(fileName, SIZE);
	while ( !file.eof() )
	{
		cout << fileName;
		file.getline(fileName, SIZE);
		cout << "\n"
			 << "(more) ";
		cin.getline( buff, sizeof buff );
	}

	// Close the file.
	file.close();

	return 0;
}

>>Mm..I'm not sure how to edit my post?
There is a time limit. You have 30 minutes to edit a post.

>>I still can't figure out how to read 24 lines at a time
Just put getline() in a loop. Note that testing end-of-file with eof() is not necessary nor even desirable because eof() will let your program loop once too many times.

for(int i = 0; i < 24 && file.getline(fileName, SIZE); i++)
   ;

>>Mm..I'm not sure how to edit my post?
There is a time limit. You have 30 minutes to edit a post.

>>I still can't figure out how to read 24 lines at a time
Just put getline() in a loop. Note that testing end-of-file with eof() is not necessary nor even desirable because eof() will let your program loop once too many times.

for(int i = 0; i < 24 && file.getline(fileName, SIZE); i++)
   ;

Where am I supposed to put this? And I don't get it...what do I put inside the loop?

I still can't figure out how to read 24 lines at a time :( Or where I should put the if statement and what that would do and how to link it all together?

***
Oh, I also need to figure out how to number each line of the text file.. with a number and a colon

Something like this, perhaps:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;

/**
 * Mimic the 'more' utility and pause every 24 lines.
 */
void more(const char *filename)
{
   const int LINES = 24;
   fstream file(filename);
   if ( file )
   {
      int lineno = 0;
      string line;
      while ( getline(file, line) )
      {
         cout << ++lineno << ": " << line << '\n';
         if ( lineno % LINES == 0 )
         {
            cout << "Press [Enter] to continue..." << flush;
            int ch;
            while ( (ch = cin.get()) != '\n' && ch != EOF )
            {
               // wait for [Enter] keypress
            }
         }
      }
   }
   else
   {
      perror(filename);
   }
}

int main()
{
   more("file.txt");
   return 0;
}

[edit]Used on this source code itself, for example changing to more("main.cpp"); , displayed the following:

1: #include <iostream>
2: #include <fstream>
3: #include <string>
4: #include <cstdio>
5: using namespace std;
6: 
7: /**
8:  * Mimic the 'more' utility and pause every 24 lines.
9:  */
10: void more(const char *filename)
11: {
12:    const int LINES = 24;
13:    fstream file(filename);
14:    if ( file )
15:    {
16:       int lineno = 0;
17:       string line;
18:       while ( getline(file, line) )
19:       {
20:          cout << ++lineno << ": " << line << '\n';
21:          if ( lineno % LINES == 0 )
22:          {
23:             cout << "Press [Enter] to continue..." << flush;
24:             int ch;
Press [Enter] to continue...

After pressing [Enter] it continued...

25:             while ( (ch = cin.get()) != '\n' && ch != EOF )
26:             {
27:                // wait for [Enter] keypress
28:             }
29:          }
30:       }
31:    }
32:    else
33:    {
34:       perror(filename);
35:    }
36: }
37: 
38: int main()
39: {
40:    more("main.cpp");
41:    return 0;
42: }

yezz yezz...this would work too

#include <iostream>
#include <fstream>

using namespace std;
class MyMethods
		{
			public:
			void ReadingFileMethod(int,int);

		};

void MyMethods::ReadingFileMethod(int i,int k)
		{
		ifstream MyData("somefile.txt");
		string data;
		string myDataArray[k];
		string tempArray[k];
		int counter = 0;
		for(int x=0;x<k;x++)
		     { 
			 			
			  getline(MyData,data);
  			  myDataArray[counter] = data;
			  counter++;
			
		     }
		MyData.close();
		for(int j=i;j<k;j++)
		     {
			cout<<j<<" "<<myDataArray[j]<<endl;
		     }
			

		}

int main()
	 {
		
		MyMethods MyMethodCallingVariable;
		int startAt=0;
		int endAt=24;
		begin:
		MyMethodCallingVariable.ReadingFileMethod(startAt,endAt);
		char answer;
		
		cout<<"wanna get the next 24 lines? press y for yes and n for no: ";
		cin>>answer;
		if(answer == 'y' || answer == 'Y')
		  {
			startAt = endAt;
			endAt = endAt*2;
			goto begin;
		  }

		return 0;

	 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.