Hi!

I am quite new at programing. I want to write a program that reads a textfile and then prints the content on the screen 25 rows at the time. I have read quite much about ifstream and ofstream. I wrote this program to count the lines in a file. And I was thinking that if I count the number of lines in a file I would be able to print the lines to the screen until there are no more lines in the file.

#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
  ifstream in; // Detta är en inström som kopplas till en fil.
  char rad[200]; // Buffer för en rad med tecken
  char filnamn[50]; // Filensnamn.
  int antalrader=0;  

  cout << "Ange filens namn ? ";
  cin.getline(filnamn,50);

  // Öppnar filen med det namn som användaren angivit.  
  in.open(filnamn);

  // Testar om det inte gick att öppna filen.
  if(!in.good())
  {
      cout << "Det gick ej att oppna filen " << filnamn << endl;
      return 0;
  }

  // Så länge inte filslut hittats
  while(!in.eof())
  {
     in.getline(rad,200); 
     antalrader++
  }
  in.close();
  cout << "Filen " << filnamn << " innehåller " << antalrader << " rader" << endl;

  return 0;
}

Another idea was to put some kind of counter in a while-loop.

while(!file.eof())
  {
    std::string str;
    getline(file,str);
    std::cout << str << std::flush;
    std::cin.get();
  }

The thing is that I am not that great learning from reading. Any help would great. It is one thing to know how to print a whole file to the screen but another to print some lines at the time.

I also wonder is I can use the manipulator noskipws to make sure all the spaces get copied.

Best Regards

jin83

Its ifstream not instream. And if you have read a lot about it you need to try to write the program yourself then post what you have done if you have problems.

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.