LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Comparison between pointer and integer ---> WHY?? (https://www.linuxquestions.org/questions/programming-9/comparison-between-pointer-and-integer-why-283448/)

its_godzilla 01-28-2005 03:42 PM

Comparison between pointer and integer ---> WHY??
 
I'm still new to C and I have a problem with this while loop:

while (gets(string) != EOF)
{
// I do stuff here
}

It gives me a warning and segmentation fault. "Comparison between pointer and integer". I can't use NULL because what is getting inputed is a list of names ex Joe|Smith|
James|Doe|
Ben|Dover| etc......

any help would be greatly appreciated......AND I am using strtok for the |

jtshaw 01-28-2005 03:58 PM

Ok... first suggestion. Don't use gets, use fgets instead (see the man page on gets for the reasoning behind this).

The warning you are getting is beccause gets (and fgets) return pointers to char's and EOF is a number.

Also, gets and fgets will never return EOF. They will return NULL if you attempt to read after you hit the EOF so change your while condition to != NULL.

Hko 01-28-2005 04:11 PM

Quote:

I can't use NULL because what is getting inputed is a list of names ex
Joe|Smith|
James|Doe|
Ben|Dover| etc......
Of course you can use NULL. Becasue that what gets() returns when it tries to read past the end of the file.

Lines in a file (or stdin in this case) do not end with NULL or '\0' like strings in C. Lines end in a newline character ('\n'). gets() reads a single line into a string, not including the newline character itself, and it it will put a '\0' in the string to mark the end.

gets() returns NULL only when it tries to read beyond the End-Of-File. Even if there would be a NULL inside the file. In that case gets() returns a non-NULL pointer to your string. And your string would contain NULL.

So just check for NULL, and you would be fine. Except... as jtshaw and the man page already said: "Never use gets().". Use fgets() instead.

its_godzilla 01-28-2005 07:55 PM

That still doesn't work properly.........
I am infact reading a file with about 250 lines that all look thik this ( Bob|Smith|) each line is followed by a newline. On the command propt I use myprog<in>out. which will read the file do what I need it to do and out-put to another file. All my program needs to do is this in stdin and stdout. Here is all of my code....maybe I'm doing something wrong somewhere else but I'm almost sure that it is this while loop:

#include <stdio.h>
#include <string.h>

int main()
{
char string[512];
char *token[16];
int a=0;

printf("Obj-Class, First Name, Last Name, Display Name, Alias Name, Directory Name,
Primary Windows NT Account, Home-Server, E-mail Address, E-mail Adresses, Members,
Obj-Container, Hide from AB");

while (fgets(string) != NULL)
{
token[a++] = strtok(string, " | "); // gets the first name
while (token[a++] = strtok(NULL, " | ")) // gets the last name
;

a--;

printf("Remote");
printf("%s", token[0]); // token[0] is the first name
printf("%s", token[1]); // token[1] is the last name
printf("%s%s", token[0], token[1]);
printf("%s.%s@######.com", token[0], token[1]);
printf('\n");
}

}

Ok now I have not completed the printf statements because I was working on this while loop with the fgets......

What hapens now is the compiler gives a warning that fgets has too few args, but I don't know how that appies to my program.

Also if I run this in stdin/out it will work for the first name I enter but not the second or third or any after. It will only read part of the name.
example:
> Bob|Smith
Remote, Bob, Smith, Bob Smith, Bob.Smith@#####.com
> Jane|Doe
Remote,o,e,oe.oe@#####.com

THIS HAS GOT ME I HAVE NO IDEA.........

jtshaw 01-28-2005 08:05 PM

I am real confused as to why your second strtok is in a while loop. If you know the file is just fistname|lastname then grab the first token and set it to first name and grab the second token and set it to the last name.

fgets takes in three arguements.

char * fgets(char * restrict str, int size, FILE * restrict stream);

so id declare a buffer and then fget into that buffer.

Something like this:

char buffer[100];
....
while (fgets(buffer,100,stdin)!=NULL) {
...
}

its_godzilla 01-28-2005 08:31 PM

ALRIGHT!!!!!!!!:D :D :D

It seems to work now......
I took out that second while loop and set thoes token[a++] to token[0] and token[1].
Then I used your advise on the buffer and while(fgets(buffer,100,stdin)!=NULL)

There is only one thing..........
My printf statements don't print on one line like I need them to. Do you know why they have all of a sudden changed. They look very sporadic, doesn't look like a certain char length or anything. I guess I could just make on long print statement, but thats harder to keep track of. If you have any info on why the changes I made would do this the the printf statements that would be great!!!

jtshaw 01-28-2005 08:37 PM

Ok... the problem is your lastname token has a \n on it.

Create a temp char *.

char *tmp;
...
while(...) {
firstname = ...
lastname = ...
tmp = strchr(lastname,'\n');
tmp = 0;
}

its_godzilla 01-28-2005 08:46 PM

I don't follow???

while (fgets(buffer,100,stin) != NULL)
{
token[0] = strtok(string, " | "); // gets the first name
token[1] = strtok(NULL, " | "); // gets the last name

printf("Remote,");
printf("%s,", token[0]); // token[0] is the first name
printf("%s,", token[1]); // token[1] is the last name
printf("%s%s,", token[0], token[1]);
printf("%s.%s@######.com,", token[0], token[1]);
printf('\n");
}

---------------------------------------------------
Something like this??

#include <stdio.h>
#include <string.h>

int main()
{
char string[512];
char *token[16];
char *tmp;

printf("Obj-Class, First Name, Last Name, Display Name, Alias Name, Directory Name,
Primary Windows NT Account, Home-Server, E-mail Address, E-mail Adresses, Members,
Obj-Container, Hide from AB");

while (fgets(buffer,100,stin) != NULL)
{
token[0] = strtok(string, " | "); // gets the first name
token[1] = strtok(NULL, " | "); // gets the last name

tmp = strchr(token[1],'\n');
tmp = 0;

printf("Remote,");
printf("%s,", token[0]); // token[0] is the first name
printf("%s,", token[1]); // token[1] is the last name
printf("%s%s,", token[0], token[1]);
printf("%s.%s@######.com,", token[0], token[1]);
printf('\n");
}

jtshaw 01-28-2005 08:51 PM

Ya, I think that should do it. That will find the '\n' and replace it with a string terminator (0 is a string terninator).

its_godzilla 01-28-2005 09:06 PM

NO that didn't do it......

I'm going to leave it for now.... I'm going home....

I want to thank you for all your help.
I'll probably work More on it tomorrow. If you have any more suggestions I will try them tomorrow, if not I may post another thread.......

thanks again.......:D :D :D

jtshaw 01-28-2005 09:40 PM

My bad... that should be *tmp = 0; Much different. Try this, it works for me.

namefile:
Code:

john|shaw
bob|ryan
jack|smith
larry|ellis
first|last

c code:
Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char string[512];
        char *token[16];
        char *tmp;

        while (fgets(string, 512, stdin) != NULL) {
                token[0] = strtok(string, "|");
                //gets the first name
                token[1] = strtok(NULL, "|");
                //gets the last name

                tmp = strchr(token[1], '\n');
                *tmp = 0;

                printf("Remote,");
                printf("%s,", token[0]);
                //token[0] is the first name
                printf("%s,", token[1]);
                //token[1] is the last name
                printf("%s %s,", token[0], token[1]);
                printf("%s.%s@######.com,", token[0], token[1]);
                printf("\n");
        }
        return 0;
}

output:
Code:

./temp < namefile
Remote,john,shaw,john shaw,john.shaw@######.com,
Remote,bob,ryan,bob ryan,bob.ryan@######.com,
Remote,jack,smith,jack smith,jack.smith@######.com,
Remote,larry,ellis,larry ellis,larry.ellis@######.com,
Remote,first,last,first last,first.last@######.com,

I hope that helps....


All times are GMT -5. The time now is 11:59 PM.