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

  • Thread starter Thread starter ACLerok
  • Start date Start date
  • Tags Tags
    C++ File
AI Thread Summary
The discussion centers on a coding issue related to reading a text file containing numbers. The user is attempting to extract sequences of five digits from the file, shifting one digit over each time until the end of the file. The provided code successfully opens the file and prints the first five digits but fails to print the subsequent five digits. The user is advised to check the error flags in the file stream after attempting to read, as this may provide insight into why the second number is not printing. The suggestion to use the `get` function instead of `getline` is also mentioned, with a note that using `atoi` has resulted in errors. The focus remains on troubleshooting the file reading process to achieve the desired output.
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.
 
Technology news on Phys.org
You are going right, so now, check the error flags in Prime.

cout << str2 << endl;
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
6K
Replies
16
Views
4K
Replies
6
Views
2K
Replies
8
Views
2K
Replies
10
Views
2K
Replies
13
Views
2K
Back
Top