Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Finding the Integers Question

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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
opened for reading, and the appropriate header files have been
#included.

--
Thomas M. Sommers -- -- AB2SB

 
Reply With Quote
 
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
 
Reply With Quote
 
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.


Your technique has the advantage of preserving order, but the disadvantage
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)
 
Reply With Quote
 
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.


Please don't top-post. Read these:

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

Your system should have documentation for the fgetc() function. Read
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.
 
Reply With Quote
 
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

 
Reply With Quote
 
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.

>
> Your technique has the advantage of preserving order, but the disadvantage
> 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
 
Reply With Quote
 
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best strategy for finding a pattern in a sequence of integers Slaunger Python 10 11-22-2008 06:16 AM
Python newbie question re Strings and integers rmac Python 4 09-22-2008 07:59 AM
Style question: Use always signed integers or not? Juha Nieminen C++ 37 06-09-2008 09:06 PM
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
finding max and min integers - newbie question john C++ 41 01-22-2004 03:45 PM



Advertisments