473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding the Integers Question

Hello,

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

If I am given a file with "jfewuuj3uefi8j kw128jdmnsdf\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 1729
jobo wrote:
Hello,

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

If I am given a file with "jfewuuj3uefi8j kw128jdmnsdf\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 "jfewuuj3uefi8j kw128jdmnsdf\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 "jfewuuj3uefi8j kw128jdmnsdf\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_poin ter)) != 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************ **********@i42g 2000cwa.googleg roups.com>
If I am given a file with "jfewuuj3uefi8j kw128jdmnsdf\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************ **********@i42g 2000cwa.googleg roups.com>
>If I am given a file with "jfewuuj3uefi8j kw128jdmnsdf\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_Keit h) 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<iostre am.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;te mp++)
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*****@invali d.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

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

Similar topics

3
2884
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 a string IDNUM = "12345"; I need to do simple arithmetic with the integers in this string. The way I've been trying and failing is by doing...
13
2524
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 these, and am interested to know whether the storage efficiency of long integers is in danger of breaking my code if I use too many. Would I do better...
6
2462
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 searching the newsgroup and web and by reading the docs, is : how do I find the MSB of mpz_t? I hope I didn't miss it if it was there.
32
5087
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 * 10^12) it's pretty efficient, it took 15 minutes to compute the first 1,000,000 primes.
20
10829
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 included a code snippet from Chuck Blake 'ls' app in python. It seems to do the job just fine on my comp, but regrettably, I'm not sassy enough to...
1
3168
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 than the number of integers in S2. Neither S1, not S2 can be an empty set. You have to find out whether the sequence of integers of S1 is there in...
2
2170
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 integers, but I still get 2 on the output. Here is the code: void IntegerNumber() { int NumberOfIntegers = 0; string SubString; int flag;
6
2910
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, val2 = 0.2; if ( (val1 - val2) limit )
275
12170
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
2756
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 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.