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];
    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;
}

Edited by mike_2000_17: Fixed formatting

1
Contributor
2
Replies
5
Views
8 Years
Discussion Span
Last Post by lotrsimp12345
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;
}

Edited by lotrsimp12345: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.