How to read a line with specific number of characters from a text file in C++

2 Answers

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;

int main()
{
	const int total_chars = 128;

	char line[total_chars];

	ifstream in("d:\\data.txt");

	while (in.get(line, total_chars)) {
		int count = in.gcount();
		cout << line << endl;
		cout << count << endl;
	}

	return 0;
}


/*
run:

c c++ python
12

*/

 



answered Jul 4, 2018 by avibootz
edited Jul 4, 2018 by avibootz
0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;

int main()
{
	const int total_chars = 4;

	char line[total_chars];

	ifstream in("d:\\data.txt");

	while (in.get(line, total_chars)) {
		int count = in.gcount();
		cout << line << endl;
		cout << count << endl;
	}

	return 0;
}


/*
run:

c c
3
++
3
pyt
3
hon
3

*/

 



answered Jul 4, 2018 by avibootz
edited Jul 4, 2018 by avibootz

Related questions

...