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

  • C/C++
  • Thread starter ACLerok
  • Start date
  • Tags
    C++ File
In summary, the code is attempting to open a file containing numbers and extract the first 5 digits, shift over 1 digit, and continue until the end of the file. The first 5 digits are successfully printed, but the second set of 5 digits is not. The user is also experiencing errors when attempting to use the get function and atoi.
  • #1
ACLerok
194
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
  • #2
You are going right, so now, check the error flags in Prime.

cout << str2 << endl;
 
  • #3


As a scientist, there are a few things I would suggest to make this code more efficient and accurate. First, instead of using a fixed size for the string to store the digits, I would recommend using a dynamic array or vector to store the numbers. This will allow for more flexibility in case the file contains numbers with varying lengths.

Next, I would suggest using a loop to iterate through the file and extract the numbers in groups of 5 digits. This can be achieved by using the getline function and specifying the delimiter as a space or comma, depending on how the numbers are separated in the file.

Additionally, instead of using the seekg function to move to the next set of numbers, I would suggest using the tellg function to get the current position in the file and then using the seekg function to move to the next position. This will ensure that the program is always reading the next set of numbers, regardless of their length.

In terms of converting the string to an integer, instead of using atoi, I would recommend using the stoi function. This function allows you to specify the starting position and the number of characters to convert, which will be useful in this case.

Overall, my approach would be to read the file line by line, extract the numbers in groups of 5 digits, convert them to integers, and then store them in a dynamic array or vector. This will allow for easier manipulation of the numbers and printing them out in the desired format.
 

1. How do I open a file in C++ for reading and writing?

To open a file in C++, you can use the fstream library. First, declare an fstream object and specify the file name and mode (read, write, or both). Then, use the open() function to open the file. For example: fstream myFile("example.txt", ios::in | ios::out); myFile.open();

2. How do I read from a file in C++?

To read from a file in C++, you can use the getline() function from the fstream library. This function reads a line from the file and stores it in a string variable. For example: string line; getline(myFile, line);

3. How do I write to a file in C++?

To write to a file in C++, you can use the fstream library's operator<< function. This function allows you to use the stream insertion operator (<<) to write data to the file. For example: myFile << "This is a line of text";

4. How do I close a file in C++?

To close a file in C++, you can use the close() function from the fstream library. This function closes the file and frees up any resources associated with it. For example: myFile.close();

5. How do I check if a file exists in C++?

To check if a file exists in C++, you can use the ifstream library's good() function. This function returns true if the file exists and can be opened, or false if it doesn't exist or can't be opened. For example: ifstream myFile("example.txt"); if(myFile.good()) { // file exists, do something }

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top