skip line

How to print specific txt file line? I want void function to print selected line using switch.
sry for code in different language

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    void print_plane_information (fstream& file)//, unsigned int num)
    {
        string lektuvas;
        string orobendrove;
        string uostas;
        string isvykimas;
        string atvykimas;

        file.open("skrydziai.txt");
       file>>lektuvas>>orobendrove>>uostas>>isvykimas>>atvykimas;

    }

    int main()
    {



        fstream file;
        file.open ("skrydziai.txt");
        file <<"LektuvasA"<<" Finair"<< " VilniausOroUosto"<<" 12:30"<<"24:34"<< endl;
        file <<"LektuvasB"<<" Ryanair"<< " Kaunoorouosto"<<" 13:45"<<" 23:45"<< endl;
        file <<"LektuvasC"<<" Finair"<< " Vilniausorouosto"<<" 12:30"<<" 4:23"<< endl;
        file <<"LektuvasD"<<" Wizzair"<< " Milanoorouosto"<<" 12:30"<<" 5:32"<< endl;
        file <<"LektuvasE"<<" AirBaltic"<< " Vilniausorouosto"<<" 12:30"<<" 00:23"<< endl;
        file.close();


        print_plane_information(file);


        return 0;
    }
Last edited on
This will skip everything up to and including the next newline character (\n).

 
file.ignore(1000, '\n');

The first argument is the maximum number of characters that will be skipped. Above I used 1000 but if you want to handle longer lines than that you can just pass in a larger value. The maximum number that you can pass in is std::numeric_limits<std::streamsize>::max() so this is what you often see people use.

 
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


By calling this in a loop you can skip to the line that you want.
Last edited on
This will skip everything up to and including the next newline character (\n).


file.ignore(1000, '\n');

The first argument is the maximum number of characters that will be skipped. Above I used 1000 but if you want to handle longer lines than that you can just pass in a larger value. The maximum number that you can pass in is std::numeric_limits<std::streamsize>::max() so this is what you often see people use.


file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


By calling this in a loop you can skip to the line that you want.


thank you for the answer but I'm not sure how to implement it into code. At which point code lets me choose which line i want to print.
do i do for loop if i want 3 line to print?
for(int i=0; i<3;i++)?
So, right after you have opened the stream, or after using something like inputFile.seekg(0, ios::beg); to reset the read position to the beginning of the file, you could use a loop that runs the code I just showed n-1 times in order to jump to the n:th line. And then afterwards, you just read the content of that line the way that you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <limits>

void jumpToLine(std::istream& os, int n)
{
	// Clear error flags, just in case.
	os.clear();
	
	// Start reading from the beginning of the file.
	os.seekg(0, std::ios::beg);
	
	// Skip to line n.
	for (int i = 1; i < n; ++i)
	{
		os.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
}

int main()
{
	
	std::cout << "Which file do you want to read from? ";
	
	std::string filename;	
	std::cin >> filename;
	
	std::cout << "Which line number do you want to print? ";
	
	int lineNumber;
	std::cin >> lineNumber;
	
	std::ifstream file(filename);
	jumpToLine(file, lineNumber);
	
	std::string lineContent;
	std::getline(file, lineContent);
	
	std::cout << "The content of line " << lineNumber << " is \"" << lineContent << "\"\n";
}

So, right after you have opened the stream, or after using something like inputFile.seekg(0, ios::beg); to reset the read position to the beginning of the file, you could use a loop that runs the code I just showed n-1 times in order to jump to the n:th line. And then afterwards, you just read the content of that line the way that you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <limits>

void jumpToLine(std::istream& os, int n)
{
// Clear error flags, just in case.
os.clear();

// Start reading from the beginning of the file.
os.seekg(0, std::ios::beg);

// Skip to line n.
for (int i = 1; i < n; ++i)
{
os.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}

int main()
{

std::cout << "Which file do you want to read from? ";

std::string filename;
std::cin >> filename;

std::cout << "Which line number do you want to print? ";

int lineNumber;
std::cin >> lineNumber;

std::ifstream file(filename);
jumpToLine(file, lineNumber);

std::string lineContent;
std::getline(file, lineContent);

std::cout << "The content of line " << lineNumber << " is \"" << lineContent << "\"\n";
}
Edit & Run

I'm getting errors no matching function call to max() and primary expression before >
from reading our question it seems you are trying to print a sentence or line, I would do this then, for each of your string object that you declared in main, use this getline(file, lektuvas);
cout<< lektuvas;
btw what does lektuvas mean lol
@Peter87 is it possible for you to explain what does this line mean or doing
os.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
nearc wrote:
I'm getting errors no matching function call to max() and primary expression before >

I don't know why. Maybe you need to include <ios>. If that doesn't work you can try passing in some arbitrary large number, like I showed first.

llll wrote:
is it possible for you to explain what does this line mean or doing
os.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

The ignore function is used to discard characters from the stream. It stops when it finds a character that is equal to the character that was passed as the second argument. It also stops after discarding the same number of character as the integer value that was passed as the first argument. Whichever occur first.

In this case we want to discard characters until the end of the line so that is why we pass the newline character '\n' as the second argument. We don't want it to stop before this, no matter how long the line is, so that is why we pass a very large value as the first argument.

The ignore function expects a value of type std::streamsize (a typedef of some signed integer type) as first argument. std::numeric_limits<std::streamsize>::max() returns the maximum value that std::streamsize can hold.

https://en.cppreference.com/w/cpp/io/basic_istream/ignore
https://en.cppreference.com/w/cpp/io/streamsize
https://en.cppreference.com/w/cpp/types/numeric_limits/max
Last edited on
Topic archived. No new replies allowed.