By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

How to find out a type id

P: n/a
hi, guys

I am doing a c program which need me to print out the error message during
the file input, suppose we need to read in a
struct {
int pid;
char *name;
char *skill;
}

I can not guarantee that the input filealways in the oder of pid, name and
then followed by skill in each line, for example,
1
someone
program
somebody
2
gaming
....
so how to I test the readin string's type id (especially the pid int type,
it is differ to other two strings type) in case I can return the error
message.

Thanks guys
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"zhi lin" <zh*****@ntlworld.com> writes:
I can not guarantee that the input filealways in the oder of pid, name and
then followed by skill in each line, for example,
1
someone
program
somebody
2
gaming
...
so how to I test the readin string's type id (especially the pid int type,
it is differ to other two strings type) in case I can return the error
message.


Strings do not have "type id"s in C. A string is simply a
sequence of characters (followed by a null byte). If the string
contains a sequence of digits, then you can consider it to be a
number. So: read in the input a line at a time. For each line,
test whether it looks like a number by examining its characters.
Based on that, give an error or not.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #2

P: n/a
On Sun, 30 Nov 2003 22:54:55 -0000, "zhi lin" <zh*****@ntlworld.com>
wrote:
hi, guys

I am doing a c program which need me to print out the error message during
the file input, suppose we need to read in a
struct {
int pid;
char *name;
char *skill;
}
Unless you wrote the structure during the current execution of the
program, it is probably unlikely that the addresses in the file which
will be read into the two pointers will still point to the data they
did when you wrote the file. It is even questionable whether the
addresses will be part of the current program.

I can not guarantee that the input filealways in the oder of pid, name and
then followed by skill in each line, for example,
1
someone
program
somebody
2
gaming
...
This is inconsistent with you first statement. Are you reading in a
structure or are you reading in data you want to add to a structure?
so how to I test the readin string's type id (especially the pid int type,
it is differ to other two strings type) in case I can return the error
message.

Thanks guys


<<Remove the del for email>>
Nov 13 '05 #3

P: n/a
Ben Pfaff wrote:

"zhi lin" <zh*****@ntlworld.com> writes:
I can not guarantee that the input
filealways in the oder of pid, name and
then followed by skill in each line, for example,
1
someone
program
somebody
2
gaming
...
so how to I test the readin string's type id
(especially the pid int type,
it is differ to other two strings type)
in case I can return the error
message.


Strings do not have "type id"s in C. A string is simply a
sequence of characters (followed by a null byte). If the string
contains a sequence of digits, then you can consider it to be a
number. So: read in the input a line at a time. For each line,
test whether it looks like a number by examining its characters.
Based on that, give an error or not.

I am doing a c program which need me to print out
the error message during the file input,
suppose we need to read in a
struct {
int pid;
char *name;
char *skill;
}


The structure contains pointers to strings, not strings.
In order to know anything about the string,
you would have to dereference the pointer first,
and you're not supposed to do that
until you know whether you have an int, or a pointer.

--
pete
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.