#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;

int main()
{

    int number_Of_Lines_To_Print_From_End=0;
    char file_name[20];
    streampos length;
    cout<<"enter file name that needs to be opened\n";
    cin>>file_name;
    cout<<"enter number of lines to print from end of file\n";
    cin>>number_Of_Lines_To_Print_From_End;
    fstream file;
    file.open(file_name, ios::out );
    if(file.is_open())
    {
        cout<<"the file opened\n";
    }
    else
    {
        cout<<"the file didn't open\n";
    }

    file.seekg(0,ios::end);
    length=file.tellg();
    file.seekg(0,ios::beg);
    cout<<"the length is"<<length<<"\n";
    return 0;
}
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;

int main()
{

	int number_Of_Lines_To_Print_From_End=0;
	char file_name[20];
	int first_data,data_loc;
	cout<<"enter file name that needs to be opened\n";
	cin>>file_name;
	cout<<"enter number of lines to print from end of file\n";
	cin>>number_Of_Lines_To_Print_From_End;
	fstream file,indexfile;
	file.open(file_name, ios::in );
	indexfile.open("output.txt", ios::out);
	if(file.is_open())
	{
		cout<<"the file opened\n";
	}
	else
	{
		cout<<"the file didn't open\n";
	}
	if(indexfile.is_open())
	{
		cout<<"the file opened\n";
	}
	else
	{
		cout<<"the file didn't open\n";
	}
	
	first_data=file.tellg();
	data_loc=first_data;
	file.peek();
	while(!file.eof())
	{
		indexfile <<data_loc<<"\n";
		data_loc=file.tellg();
		file.peek();
	}
	indexfile.close();
	//clear eof flags
	file.clear();
	file.seekg(first_data);
	return 0;
}

First problem works now, the new problem is how to move the postion forward to find newline.

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.