How can I extract and print every 5 digits from a txt file using C++ file i/o?

  • Context: C/C++ 
  • Thread starter Thread starter ACLerok
  • Start date Start date
  • Tags Tags
    C++ File
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
ACLerok
Messages
194
Reaction score
0
i have a txt file full of numbers and i just want to take the first 5 digits, save it has one number, shift over 1 digit, take another 5 digits, save it, and so on until the end of the file

here is the code
Code:
	fstream Prime;
	Prime.open("digitsofe.txt");
	
	if (!Prime.is_open()) { cout << "unable to open"; }
	else if (Prime.is_open()) {
		cout << "File opened successfully\n";
		Prime.getline(str2,6);
		cout << str2 << endl;
		Prime.seekg(1);
		Prime.getline(str2,6);
		cout << str2;
		
		Prime.close();
}
right now I'm just trying to print the first 5 digits and then the next 5 digits but the second number won't print. what am i doing wrong?

I tried using the get function but using atoi kept giving errors.
 
Physics news on Phys.org
You are going right, so now, check the error flags in Prime.

count << str2 << endl;