473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding the Integers Question

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 :)

Oct 25 '06 #1
22 1710
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 -- tm*@nj.net -- AB2SB

Oct 25 '06 #2
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 -- tm*@nj.net -- AB2SB
Oct 25 '06 #3
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 -- tm*@nj.net -- AB2SB

Oct 25 '06 #4
On 24 Oct 2006 22:30:04 -0700,
jobo <jo*****@gmail.comwrote
in Msg. <11**********************@i42g2000cwa.googlegroups .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
Oct 25 '06 #5
Robert Latest said:
On 24 Oct 2006 22:30:04 -0700,
jobo <jo*****@gmail.comwrote
in Msg. <11**********************@i42g2000cwa.googlegroups .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)
Oct 25 '06 #6
"jobo" <jo*****@gmail.comwrites:
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) ks***@mib.org <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.
Oct 25 '06 #7
#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

Oct 25 '06 #8
On Wed, 25 Oct 2006 07:55:50 +0000,
Richard Heathfield <in*****@invalid.invalidwrote
in Msg. <tc******************************@bt.com>
>>>
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
Oct 25 '06 #9
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)
Oct 25 '06 #10
"hookah_frog" <An**********@gmail.comwrites:
#include<conio.h>
If you use non-standard headers at least say what they are for.
#include<iostream.h>
This looks like an old C++ header. What is *it* here for?
#include<stdio.h>

void main()
main returns int.
{
FILE *fpt;
char c;
This should be int because EOF is an integer value not equal to any
char.
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);
A "while" loop is almost always better than "do" for input because the
input could be empty. c may already be EOF (albeit converted to char)
but you won't test that until you have tried to do something with it.
>
if( (c >='0' ) && (c <='9' ))
OK, but isdigit(c) is the standard idiom.
{
i[count ++] = c-'0';
This can over flow your array. *Always* check. It is not hard:

if (count < 100) i[count++] = c - '0';
}

}
while(c!=EOF);
And now this test may fail to detect end-of-file. Also an valid input
character might have a value that, when promoted to int, is equal to
EOF and the loop might stop early.
fclose(fpt);
}
for(temp = 0;temp<count;temp++)
printf("%d",i[temp]);
}
// USING mem. allocation would be more suitable
--
Ben.
Oct 25 '06 #11
hookah_frog said:
#include<conio.h>
Non-standard header, not provided by all C compilers.
#include<iostream.h>
Non-standard header, not provided by all C compilers.
#include<stdio.h>

void main()
The main() function returns int, not void. The canonical form (when not
accepting command line arguments) is:

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

fpt = fopen("D:/ceg.txt","r");
Hard-coded path information makes the program less useful than it could be.
if(NULL == fpt)
{
printf("\n Cannot open file");
}
else
{
do
{
c = getc(fpt);
getc returns int, not char.
>
if( (c >='0' ) && (c <='9' ))
Neater: if(isdigit(c))
{
i[count ++] = c-'0';
Buffer overrun if count is (or exceeds) 100.

--
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)
Oct 25 '06 #12
On Tue, 2006-10-24 at 23:02 -0700, jobo wrote:
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.
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.
Don't toppost, and snip signatures.

fgetc() wants a file pointer as an argument. You passed it a string
literal. Find a good book on C and learn the details of the language.
This group recommends "The C Programming Language" by Kernighan and
Ritchie.
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #13
On Wed, 2006-10-25 at 02:02 -0700, hookah_frog wrote:
#include<conio.h>
Non-standard header.
#include<iostream.h>
Wrong-standard header.
#include<stdio.h>

void main()
main() returns int. main() returns int. main() returns int.

Figure out what language you're using, what group you need to post to,
and how to write a proper "Hello, world!" program. After that, post
again if you still have trouble.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #14
On Wed, 2006-10-25 at 13:02 +0000, Robert Latest wrote:
On Wed, 25 Oct 2006 07:55:50 +0000,
Richard Heathfield <in*****@invalid.invalidwrote
in Msg. <tc******************************@bt.com>
>>
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.
If order is irrelevant, it would be simple to convert Richard's
10-element array to the OP's example array.
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #15
There is no fgetc() in the code !
Andrew Poelstra wrote:
On Tue, 2006-10-24 at 23:02 -0700, jobo wrote:
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.
>
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.

Don't toppost, and snip signatures.

fgetc() wants a file pointer as an argument. You passed it a string
literal. Find a good book on C and learn the details of the language.
This group recommends "The C Programming Language" by Kernighan and
Ritchie.
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
Oct 25 '06 #16
[Top-posting fixed, and some snippage for contrast!]

hookah_frog said:
Andrew Poelstra wrote:
>On Tue, 2006-10-24 at 23:02 -0700, jobo wrote:
>
Here's what I have in my code:
arr[i] = fgetc("%d ");
<snip>
>fgetc() wants a file pointer as an argument. You passed it a string
literal.
<snip>
There is no fgetc() in the code !
Look again!

--
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)
Oct 25 '06 #17
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?
Use the fgetc function to get one character at a time from a file. Below
is the normal idiom:

int ch;
FILE *fp = fopen("filename", "r");

if(!fp) exit(EXIT_FAILURE);

while((ch = fgetc(fp)) != EOF)
{
/* process character */
}

Here's a function that will construct your required array, mallocing and
reallocing to handle any number of digits in the input:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int *get_digits(FILE *fp, size_t *i)
{
size_t n = 10; /* initial size */
int *arr = malloc(n * sizeof *arr);
int ch;
*i = 0;
if(!arr) return NULL;
while((ch = fgetc(fp)) != EOF)
{
if(isdigit((unsigned char)ch))
{
/* Store the digit converted to 0 to 9 into the
next available spot in arr, and increment *i
so it reflects the new number of items */
arr[*i] = ch - '0';
++*i;

if(*i == n) /* it's full, allocate more */
{
int *tmp;
n *= 2;
tmp = realloc(arr, n * sizeof *arr);
if(!tmp) break;
arr = tmp;
}
}
}
return arr;
}

It will return a pointer to int, being the array of digits, and it will
store the length of the data in that array into *i. You pass it a
pointer to a size_t variable like this:

size_t num_digits;

int *arr = get_digits(fp, &num_digits);

You should check whether the arr returned was a null pointer (means the
initial malloc failed). Also check whether feof(fp) (good, read
everything) or ferror(fp) (bad, encountered an input error) or neither
(bad, means it didn't get all the way through because of a realloc error).

Then to iterate through the array:

size_t i;
for(i = 0; i < num_digits; i++)
{
/* use arr[i] */
}

--
Simon.
Oct 25 '06 #18
Richard Heathfield wrote:
hookah_frog said:
.... snip ...
>
>#include<stdio.h>

void main()

The main() function returns int, not void. The canonical form (when
not accepting command line arguments) is:

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

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

Hard-coded path information makes the program less useful than it
could be.
Better in general is to take arguments with:

int main(int argc, char **argv) {

and to handle the file access with:

if (argc < 2) puts("Usage: foo filename);
else {
fpt = fopen(argv[1], "r");
... and the rest of your code ...
}

later you can gussy up the Usage branch (and others) to return
EXIT_FAILURE iff you #include <stdlib.h>.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 25 '06 #19
Andrew Poelstra wrote:
On Wed, 2006-10-25 at 02:02 -0700, hookah_frog wrote:
#include<conio.h>

Non-standard header.
#include<iostream.h>

Wrong-standard header.
No, non-standard header.

Brian
Oct 25 '06 #20
"hookah_frog" <An**********@gmail.comwrites:
There is no fgetc() in the code !
Andrew Poelstra wrote:
>On Tue, 2006-10-24 at 23:02 -0700, jobo wrote:
[snip]
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.

Don't toppost, and snip signatures.

fgetc() wants a file pointer as an argument. You passed it a string
literal. Find a good book on C and learn the details of the language.
This group recommends "The C Programming Language" by Kernighan and
Ritchie.
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
Please pay attention this time:

Don't top-post, and do snip signatures.

For more information on top-posting, see:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

When you post a followup, you should quote enough context so your
followup makes sense to someone who hasn't seen the previous article
(which may or may not be available), but you should delete any quoted
text that isn't relevant to your followup. In particular, signatures
(the stuff at the end of an article starting with a "-- " line) should
be snipped unless you're actually commenting on something in the
signature.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Oct 25 '06 #21
hookah_frog wrote:
>
There is no fgetc() in the code !
Don't top-post. Your answer belongs after, or possibly intermixed
with, the _snipped_ material to which you reply. The snipping
removes anything not germane to your answer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 25 '06 #22
Ben Bacarisse wrote:
"hookah_frog" <An**********@gmail.comwrites:
> char c;

This should be int because EOF is an integer value not equal to any
char.
Not quite. EOF is an integer value not equal to any unsigned char.
That's pretty obvious since EOF is also a negative value. It may also be
not equal to any char value, but often it is equal to a char value.

The reason we say not to use char for the result of getc / getchar /
fgetc is precisely that EOF may be an integer value equal to a (signed)
char value.

getc returns a signed int value between 0 and UCHAR_MAX inclusive, or
else EOF. If that result is directly assigned to a signed char,
typically the value 255 becomes -1 and so will be detected as EOF.

--
Simon.
Oct 26 '06 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Vallely | last post by:
If anyone could help me with my problem I'd greatly appreciate it, this question probably has a quick easy answer but I've been wanting to punch the wall for the last hour because of it. I have...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
6
by: Ernst Berg | last post by:
I am learning about the GMP library and it is installed correctly here. My question, which I found no match (so far) in the reading of the past few weeks of posting to this group,the FAQ, by...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
1
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
2
by: jewel87 | last post by:
Hello, I've got a problem and would appreciate any help. I have to count the number of integers in a string like this "1.1 some text. 1.2 some text", where it should return 0, as there are no...
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
8
by: Slaunger | last post by:
Hi all, I am a Python novice, and I have run into a problem in a project I am working on, which boils down to identifying the patterns in a sequence of integers, for example ..... 1 6 6 1 6 6...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.