Thread: C++ extract only numbers from a .txt file

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Question C++ extract only numbers from a .txt file

    Hi,

    This code works fine:

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    
    void main()
    {
    FILE *daten;
    
    int i;
    float x[5], y[5];
    
    clrscr();
    
    daten=fopen("a:dat.txt","r");
    
     for (i=0;i<=4;i++)
      fscanf(daten,"%f%f",&x[i],&y[i]);
    
       for (i=0;i<=4;i++)
         cout<<x[i]<<y[i];
    
      fclose(daten);
     getch();
    }
    Only the file dat.txt has to contain numbers only(including a sign), and must not contain any other characters.

    I`d like to process a hpgl plotter file, which
    looks somewhat like this:

    PU-123 234;
    PD345 567;
    PD346 568;

    IŽd like to get rid of everything but the numbers.
    I did experiment with fseek, but wasn`t very succsessful.

    Has anyone a routine that I could use ?

    appreciate,

    Johannes

  2. #2
    tegwin7531
    Guest

    Interesting..

    whenever i want to process a string and only gather the numbers i just root out the characters.. if you know how many characters to skip for each line then the code written by the guy above me works just dandy... incase this is not the case..

    why not just read in the first group of letters/numbers

    Code:
    string s1, temp;
    
    inFile >> s1;  // Store first set of characters in here..
    
    for (int i = 0; i < string.length(); i++)
    {
      if (string[i] > 0 && string[i] < 10)  // IF the current letter at
                                                          // index i is a number.
        temp[i] = string[i];  // Store it in temp string.
    }
    replace the 0 && 10 with the ascii values for the numbers 0-10... i can't remember what they are off the top of my head.. and at the end just convert the temp string to an int and store in an int variable if u need to do mathematical computations on it.. just repeat this in a loop for the entire file.. you can read in strings containing letters/symbols/numbers.. take out the numbers and store in temp and ignore all others...
    just another way to do things..

    B.T.W. i believe the function that converts a c-string to an int is in the file cctype

    hope this was of some assistance...

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Also

    Also if you want another way to skip the first 2 characters of every line..


    Code:
    #include <fstream>
    
    int main()
    {
      int num1[50], num2[50]; // 2 integer arrays of size 50.
    
      int index = 0;  
    
      std::ifstream inFile;
      inFile.open("dat.txt");  // Open the file dat.txt
    
      do
      {
        inFile.ignore(2, ' ');   // Ignore the first two characters.
        inFile >> num1[index];  // Store the next set of #'s in num1.
        inFile >> num2[index];  // Store the next set of #'s in num2.
        inFile.ignore(20, '\n');  // Go to the next line.
        index++;  // Increment index.
      }while (!inFile.eof());  // While we are not at end of the file.
      
      inFile.close();  // Close file
    
      return 0;
    }
    i believe this set of code would work if for every line the first 2 characters you didnt want.. then followed by a positive or negative number... then some spaces then followed by a second number (positive or negative)... would store in a nice array incase you wanted to use it as coordinates and such..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM