How to find out a type id

Z

zhi lin

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
 
B

Ben Pfaff

zhi lin said:
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.
 
B

Barry Schwarz

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>>
 
P

pete

Ben said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top