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...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

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