Jump to content

Find number's in text file ( C )

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Omar Ali

Omar Ali

    Newbie

  • Members
  • Pip
  • 4 posts
Hello friends, i can't solve this
i have a text file with text and numbers for example : " Hello world 123456 "

how can i find all number's from the the text file and show it ?

i opened a file ready for read what' the next step ?

int findnumber() { // find all numbers

    FILE *fp;

    fp=fopen("Text.txt","r");

    // now what ? 

    }  


#2
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Well! First declare some variables to store the values in them from the file while we are reading. Then use the fscanf() to read from file.

Example:

char str[9];  /* One extra for nul char. */

int val;


...


while (fscanf(fp, "%s %d", str, &val) != EOF)        // till the End-Of-File.


...



Hope this helps!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#3
Omar Ali

Omar Ali

    Newbie

  • Members
  • Pip
  • 4 posts
So now how can i print only the number's ? sorry im new

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
while( there is a char that is not EOF)
   if (  isdigit(char_read) ) print(char_read)
or something like that

---------- Post added at 09:55 AM ---------- Previous post was at 09:41 AM ----------

Have a look: cctype (ctype.h) - C++ Reference
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
Omar Ali

Omar Ali

    Newbie

  • Members
  • Pip
  • 4 posts
The problem is i have a string it the tring i have char's and integer's i want to print out only integer ? how

Where am i wrong

int findnumber() { // find all numbers

   FILE *f;

   f = fopen("Text.txt","r");

   char buf[1000];

   fgets(buf,sizeof(buf),f);

   int integer;

   char ch[100];

   sscanf(buf,"%s%d",&ch,&integer);

   printf("%*s%d",ch,integer);

    }  


#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
There are possibly countless ways to get this done. But in the end you need to test each read value for char or int properties.
Try this:

We declare the necessary variables:
const char * filename = "input.txt";
FILE * fp = fopen(filename,"r");
int c;

Why declare c as an int? So we can distinguish between the end of input value and other chars.

We can use a simple read function like getchar which returns one character from the standard input(usually the stuff you type in your terminal or command prompt window). For our purposes we will use the getc function. Which is more or less the same as getchar in that it reads one char, but instead it takes one argument. A File pointer. Now we can read from a file like this:
c = getc(fp);

The pseudo code i gave in the earlier post was:
while( there is a char that is not EOF)    
     if (  isdigit(char_read) ) print(char_read)

What the while loop will do is read a char then test the char for the EOF value. Once it is not this value we continue.
while ( (c = getc(fp)) != EOF)

Variable c now contains a character read from the input file. Now test for int property, if satisfied we print the character:
if ( isdigit(c) != 0 )  putchar(c);
putchar is used to print one char to standard input.

Details of getchar, putchar, getc , etc, can be found here

How to differentiate between alpha chars, int chars, other chars? Easy. Use functions from the ctype.h library. isdigit is what we will need for the job at hand. isdigit will return zero if it finds a value other than a decimal and any other value for a decimal. So we basically just need to test for a value other than zero.

So lets put all this together.

    const char * filename = "input.txt";
    FILE * fp = fopen(filename,"r");
    int c;
    
    if(fp == NULL) { 
         printf("Error opening %s\n",filename); 
         exit(1); 
    }
    
    while ( (c = getc(fp)) != EOF)
    {
          if ( isdigit(c) != 0 )  putchar(c);
    }
    fclose(fp);

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users