Velocity Reviews > Finding the Integers Question

# Finding the Integers Question

jobo
Guest
Posts: n/a

 10-25-2006
Hello,

I'm very new to C so please forgive my ineptitude.

If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"

I want to capture each occurence of an integer 0-9 into an array.

So in the array I would have {3,8,1,2,8,1,9,0,4}

How would I go about doing this? scanf? fgets?

Thank you

T.M. Sommers
Guest
Posts: n/a

 10-25-2006
jobo wrote:
> Hello,
>
> I'm very new to C so please forgive my ineptitude.
>
> If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
>
> I want to capture each occurence of an integer 0-9 into an array.
>
> So in the array I would have {3,8,1,2,8,1,9,0,4}
>
> How would I go about doing this? scanf? fgets?

Take a look at fgetc and isdigit.

--
Thomas M. Sommers -- -- AB2SB

jobo
Guest
Posts: n/a

 10-25-2006
Here's what I have in my code:
arr[i] = fgetc("%d ");

I'm gettting a "passing argument 1 of 'fgetc' from incompatible pointer
type" error.

T.M. Sommers wrote:
> jobo wrote:
> > Hello,
> >
> > I'm very new to C so please forgive my ineptitude.
> >
> > If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
> >
> > I want to capture each occurence of an integer 0-9 into an array.
> >
> > So in the array I would have {3,8,1,2,8,1,9,0,4}
> >
> > How would I go about doing this? scanf? fgets?

>
> Take a look at fgetc and isdigit.
>
> --
> Thomas M. Sommers -- -- AB2SB

T.M. Sommers
Guest
Posts: n/a

 10-25-2006
jobo wrote:
> T.M. Sommers wrote:
>>jobo wrote:
>>
>>>I'm very new to C so please forgive my ineptitude.
>>>
>>>If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
>>>
>>>I want to capture each occurence of an integer 0-9 into an array.
>>>
>>>So in the array I would have {3,8,1,2,8,1,9,0,4}
>>>
>>>How would I go about doing this? scanf? fgets?

>>
>>Take a look at fgetc and isdigit.

>
> Here's what I have in my code:
> arr[i] = fgetc("%d ");
>
> I'm gettting a "passing argument 1 of 'fgetc' from incompatible pointer
> type" error.

Read the man page for fgetc. It takes a FILE * as an argument,
and returns an int. You want something like this:

int ch;
while ( (ch = fgetc(file_pointer)) != EOF ) {
if ( isdigit(ch) ) {
/* stuff ch in your array */
}
}

Assuming file_pointer is a FILE * and has been successfully
#included.

--
Thomas M. Sommers -- -- AB2SB

Robert Latest
Guest
Posts: n/a

 10-25-2006
On 24 Oct 2006 22:30:04 -0700,
jobo <> wrote
in Msg. < .com>

> If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
>
> I want to capture each occurence of an integer 0-9 into an array.
>
> So in the array I would have {3,8,1,2,8,1,9,0,4}
>
> How would I go about doing this? scanf? fgets?

Do two passes. First go through the file counting the digits,
use the result to allocate a suitable array, and then go
through the data again to populate the array.

robert

Richard Heathfield
Guest
Posts: n/a

 10-25-2006
Robert Latest said:

> On 24 Oct 2006 22:30:04 -0700,
> jobo <> wrote
> in Msg. < .com>
>
>> If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
>>
>> I want to capture each occurence of an integer 0-9 into an array.
>>
>> So in the array I would have {3,8,1,2,8,1,9,0,4}
>>
>> How would I go about doing this? scanf? fgets?

>
> Do two passes. First go through the file counting the digits,
> use the result to allocate a suitable array, and then go
> through the data again to populate the array.

of requiring the stream to be read twice. (If it's stdin, which the OP
suggests it isn't but never mind that!, then the stream might not even be
available a second time.)

If you expand the buffer as you go, using realloc when required, then you
can do this in a single pass.

(If order is not significant, an array of ten unsigned longs will be fine,
with each element being a counter for a particular digit.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Keith Thompson
Guest
Posts: n/a

 10-25-2006
"jobo" <> writes:
> Here's what I have in my code:
> arr[i] = fgetc("%d ");
>
> I'm gettting a "passing argument 1 of 'fgetc' from incompatible pointer
> type" error.

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

it. If not, a Google search should tell you how to use it. Don't
just guess (fgetc() doesn't take printf-style or scanf-style format
strings).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

hookah_frog
Guest
Posts: n/a

 10-25-2006
#include<conio.h>
#include<iostream.h>
#include<stdio.h>

void main()
{
FILE *fpt;
char c;
int temp,i[100];
int count = 0;

fpt = fopen("D:/ceg.txt","r");

if(NULL == fpt)
{
printf("\n Cannot open file");
}
else
{
do
{
c = getc(fpt);

if( (c >='0' ) && (c <='9' ))
{
i[count ++] = c-'0';
}

}
while(c!=EOF);
fclose(fpt);
}
for(temp = 0;temp<count;temp++)
printf("%d",i[temp]);
}
// USING mem. allocation would be more suitable

Robert Latest
Guest
Posts: n/a

 10-25-2006
On Wed, 25 Oct 2006 07:55:50 +0000,
Richard Heathfield <> wrote
in Msg. <>
>>>
>>> So in the array I would have {3,8,1,2,8,1,9,0,4}

>> Do two passes. First go through the file counting the digits,
>> use the result to allocate a suitable array, and then go
>> through the data again to populate the array.

>
> of requiring the stream to be read twice. (If it's stdin, which the OP
> suggests it isn't but never mind that!, then the stream might not even be
> available a second time.)
>
> If you expand the buffer as you go, using realloc when required, then you
> can do this in a single pass.

Of course. I tried to keep my answer on the level of suspected cluefulness
on the OP's part

> (If order is not significant, an array of ten unsigned longs will be fine,
> with each element being a counter for a particular digit.)

The OP's example array content shows that this is not what he wants.

robert

Richard Heathfield
Guest
Posts: n/a

 10-25-2006
Robert Latest said:

<snip>

> I tried to keep my answer on the level of suspected cluefulness
> on the OP's part

I see. I just hope you didn't overshoot!

>> (If order is not significant, an array of ten unsigned longs will be
>> fine, with each element being a counter for a particular digit.)

>
> The OP's example array content shows that this is not what he wants.

Oops, so it does.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)