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;
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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