I'm just starting to learn C++ and I need some help. I need to be able to read in a text file and pull out numbers from the file (like a news story, etc). I will need to be able to do other checks with the numbers as well, I'm just looking for a starting point right now. Just want to make sure that I have an idea of how to proceed.

I will need to open the file
Then read the file line by line with getLine
But I am not sure how to find just the digits and extract them...

I know this is probably a very simple question, but thank you for any help!

Recommended Answers

All 9 Replies

//Nov 19,2010
//This lab does some leftover string stuff and introduces the concept of writing to and reading from data files.
#include <string>
using namespace std;
int main (void)
	//Brif intro to string class
	string name;
	name = "Jrussell";
	cout << name << endl;
	cout << "The length of name is " << name.length() <<".\n";

	//reading from a data file
	ifstream inFile; //infile = cin

		cout<<"OMG-it didn't open! WTF?" << endl;
	int numNames = 0;
	while (!(inFile.eof())){
	inFile >> name;
	cout << name << endl;

	int index;
	string* namePtr = new string[numNames];
	for(index = 0; index < numNames; index++)
		inFile >> namePtr[index];

	ofstream outFile("C:\\Users\\Desktop\\newnames.txt");
	if (outFile.fail()){
		cout << "Error opening" << endl;
	for (index = 0; index < numNames; index ++){
		outFile << "Name# " << index+1 << "is: ";
		outFile << namePtr[index] << ".\n";

so here this program reads the name from file on desktop! u need to include the file path as u see here! and u can do bunch of stuff with it! hope this helps!

and if u copy paste this prog and run it without file on specified location it will show the error message...

That is great, thank you! How do I scan through and only pull of the strings that contain numbers? I need to get rid of everything else, and just store the numbers? Should I read them as tokens and use like isDigit? Or how would be the best way to go about that? Thank you again for your help!

start from beginning and assign each digit to an array. then it will be easy to scan the digits.

Use the appropriate version of getline() to read the file line-by-line, reading each line into either a std::string object or a char array.

Then, after you've read a line, use a loop to review each char in the read line. If the char is a digit either store it some place else or write it to the file; otherwise ignore it and move on to the next.

What about something like this, and then I can parse through the array? Does that make sense, or am I going about this completely wrong??

int main ()
	string LineList[100000];
	ifstream infile; 
   cout << "Reading from the file" << endl; 
		cout<<"File not found" << endl;
	while (!inFile.eos())
         if (getch=='\r') continue;
         if (getch == '\n' || inFile.eos() )



Your formatting isn't that great so it's difficult to see your intent. I can see a few braces that aren't paired correctly such as the if statement you're using for error-checking on Lines 7-9. Where does that statement block end? As written, it ends on Line 22 and you have no closing brace for your main().

Ignoring that, another thing that I see is that you aren't using "getline()" as required by the assignment (as I understand the assignment). The way you've written it, (again, ignoring the improper structure of your if statement) you are attempting to read the file char-by-char instead of line-by-line.

Here is the basic structure you're looking for:

#include <iostream>
#include <fstream>
#include <string>

const string fileName = "myFile.txt";  //declare a constant for the file name

int main()
  //declare and initialize the file stream
  std::ifstream dataFile(fileName.c_str(), std::ios::in);

  //verify the file stream is valid
  if (!dataFile.is_open()) {
    std::cout << "Error opening file!";

  //declare the input string
  std::string readString;

  while (std::getline(dataFile, readString)) {
    for(int i = 0; i < (readString.length()); ++i) {

      //if char is a digit, store the char\


    //finish analyzing detected digits and react accordingly

  } //end while

  dataFile.clear();   //reset the stream's error/EOF condition

  //additional code

  dataFile.close();   //close the file stream

  return EXIT_SUCCESS;

Notice the nested loops starting on Line 21...

I also noticed that you're using inFile.eos() . As far as I know, there is no such thing. Did you mean inFile.eof() ? It's really not the best choice because of the conditions that are required for that status to be set. You're better off basing your condition on whether you've had a successful read or not.

That is great! Thank you. My next question is I need to take the digit, say -123.4567 and turn it into -1234567. Basically I just need to remove the decimal place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.