2 Replies - 2741 Views - Last Post: 17 April 2011 - 06:46 AM Rate Topic: -----

#1 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Advanced File and I/O Operations

Posted 17 April 2011 - 06:12 AM

Write a program that asks the user for the name of a text file. The program should display the last 10 lines of the file on the screen (the “tail” of the file). If the file has less than 10 lines, the entire file is displayed, with a message that the entire file has been displayed. The program should do this by seeking to the end of the file and then backing up to the tenth line from the end.
You can use these files to test your program, right-click to download them: TestFile5Lines.txt and TestFile20Lines.txt

I tried to do this first using TestFile20Lines.txt which looks like this:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty


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



 int main()
 {
	// go to the end of the file and count 10 lines from the bottom
	int LENGTH;
	fstream nameFile;
	char input(LENGTH - 10);

	// open file
	nameFile.open("TestFile20Lines.txt", ios::in);
	if (!nameFile)
	{
		cout << "File open error!" << endl;
		return 0;
	}
	
	// read file one line at a time
	// display the last 10 lines in the file. if the file has less than 10 lines
	// display the entire file and print a message "The Entire File Has Been Displayed".
	nameFile.getline(input,LENGTH);
	while (!nameFile.fail())
	{
		cout << input << endl;
		nameFile.getline(input, LENGTH);
	}
	 
	if (LENGTH < 10)
	{
		cout << "The Entire File Has Been Displayed" << endl;
	}
	
	// close the file
	nameFile.close();


	system("Pause");
	return 0;
 }


The errors I receive and need help resolving:
1>------ Build started: Project: TailOfFile, Configuration: Debug Win32 ------
1> TailOfFile.cpp
1>c:\users\kim\documents\visual studio 2010\projects\tailoffile\tailoffile\tailoffile.cpp(39): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\kim\documents\visual studio 2010\projects\tailoffile\tailoffile\tailoffile.cpp(43): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Is This A Good Question/Topic? 1
  • +

Replies To: Advanced File and I/O Operations

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Advanced File and I/O Operations

Posted 17 April 2011 - 06:39 AM

You need a string or char array in which to store each line of input. That has to be big enough to hold the longest string that might be in the input file. It would make sense to use a C++ string for this purpose since that can hold any length string -- it will automatically adjust itself as required. So change the declaration of input (line 14 above) to:

string input;

Then, you should be using the string version of getline, documented here. So change the getline calls (lines 27 and 31) to:

getline( nameFile, input );
Was This Post Helpful? 2
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Advanced File and I/O Operations

Posted 17 April 2011 - 06:46 AM

However, you have also basically ignored the requirement to count the number of lines in the file. You need a counter variable (an int), and you have to set up a loop in which you read the file, line by line, incrementing the counter for each line and terminating the loop when you reach the end of the file.

Then, you have to clear the input stream and reset it to the beginning of the file (or simply close it and reopen it), and deal with the specific lines that you want (the last 10).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1