Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #101950
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to check for valid names |
| Date | 2017-01-16 11:40 +0000 |
| Organization | Fix this later |
| Message-ID | <o5ibbs$ube$1@dont-email.me> (permalink) |
| References | <d715c9db-3c6c-437d-aeb4-ba7eaefce4db@googlegroups.com> <87inphn0r9.fsf@bsb.me.uk> <3d4daa02-9d85-4c04-b085-59b0ed8eb58a@googlegroups.com> <87y3ydlcln.fsf@bsb.me.uk> <823f8d4b-9c72-4324-ad7f-17d8fd03504c@googlegroups.com> |
On 16/01/17 10:16, Alla _ wrote:
> On Saturday, January 14, 2017 at 9:29:18 PM UTC+3, Ben Bacarisse wrote:
>> Alla _ <modelling.data@gmail.com> writes:
>>
>>> On Saturday, January 14, 2017 at 6:02:08 PM UTC+3, Ben Bacarisse wrote:
>>>> Alla _ <modelling.data@gmail.com> writes:
>>>>
>>>>> Given the amount of valuable advice I get here, and the amount
>>>>> and quality of knowledge I acquire here, I have decided to run the
>>>>> following piece by you to get your opinion on the structure and the
>>>>> logic. I am afraid to loose those tiny seeds of improvement, if I
>>>>> go on writing my little program completely on my own, only occasionally
>>>>> asking for help with some pieces.
>>>>>
>>>>> Please, take a look at this little part of the program.
>>>>>
>>>>> The goal of this little part is to read all lines from a file, and
>>>>> upon reading each line check if it doesn't start with a number,
>>>>
>>>> Why does that matter to you?
>>
>>> It does.
>>
>> But why? None of the data lines you care about have numbers at the
>> start so looking for one is a waste of time. Remember the plan is not
>> to find things that are wrong but to look for what you need and anything
>> else (a failure to find what you need) is an error.
>>
>>> I will extend that part further, to work with numbers.
>>> strtod turned out to be a great tool.
>>
>> The numbers follow the text in your format. What you will remove this
>> code, expand it and put it somewhere else. At the moment all it does is
>> confuse people like me who have seen the format you are trying to read.
>>
>>>>> and, if it doesn't, then copy each valid character into another
>>>>> buffer by calling function check_input_names; this function should
>>>>> check the buffer for valid characters, and if there are any, copy
>>>>> them into another place, and then call the function, which checks
>>>>> if copied names can be found in the array of valid names; at the end
>>>>> return the address of the first invalid character in the buffer (I need it
>>>>> to be so).
>>>>
>>>> This is not the right way to perform the overall goal of checking for a
>>>> valid name for some fixed list of names.
>>>>
>>>> The key function to look up is strncmp and then you can avoid all the
>>>> copying of data.
>>> Here is what is said about strncmp in the manual:
>>> "The strncmp() function shall compare not more than n bytes "
>>> I am not sure I understand why strncmp is more helpful than strcmp I
>>> use now.
>>
>> I'll try a hint. Here is a program that counts the number of lines that
>> start with "Aha!". Before you read it, think hou you would do that...
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main(void)
>> {
>> unsigned count = 0;
>> char line[100];
>> while (fgets(line, sizeof line, stdin)) {
>> if (strncmp(line, "Aha!", 4) == 0)
>> count++;
>> }
>> printf("%d\n", count);
>> return 0;
>> }
> I am truly sorry, but I don't understand. I have 13 names,
> each has a different length. Each line I read from a file
> has not only names, but also numbers. So I can't just
> use strncmp for each line, because:
> - number of bytes is different for each element in the array
> of names;
Yes, but you know (or can find out) how long they are.
> - even if I use something to determine the length of each
> name in the array and use that in strncmp, also using the
> loop to loop through each name in the array, the comparison to
> 0 will always be > or < and never = because the line contains
> something else than just a name.
Why? You're only looking at the right number of bytes on that line. If
they are the right bytes, they are the right bytes, regardless of what
follows. And if they are the wrong bytes, they are the wrong bytes,
regardless of what follows. I really don't see your difficulty here.
> This is why trying to follow
> the method, I liked so much, of finding only what I need,
> I copy only potentially valid input characters and then check
> those.
Why would you need to /copy/ anything? I don't get it.
Why not just look for what you're looking for, and sing "hooray" if you
find it, and write a diagnostic message if you don't?
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-14 04:35 -0800
Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:02 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-14 07:29 -0800
Re: Program to check for valid names supercat@casperkitty.com - 2017-01-14 10:17 -0800
Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 18:29 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 02:16 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 11:40 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 03:50 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 04:21 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 12:56 +0000
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 12:59 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 05:03 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 13:06 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 06:40 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 15:03 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 08:29 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 16:42 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:03 -0800
Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-16 17:17 +0000
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:20 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:26 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:18 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:23 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:30 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 06:42 -0800
Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-16 16:23 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 08:46 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:33 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:37 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:54 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-17 03:44 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-17 08:16 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 10:21 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 10:32 -0800
Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-18 19:38 +0000
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 12:52 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-18 18:52 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-20 02:00 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-20 12:09 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-20 02:45 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-20 12:24 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-21 09:02 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-18 18:05 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-16 10:23 -0800
Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 23:03 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-17 00:19 -0800
Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-14 18:23 -0800
csiph-web