LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2005, 03:42 PM   #1
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Rep: Reputation: 0
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 |
 
Old 01-28-2005, 03:58 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-28-2005, 04:11 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 01-28-2005 at 04:15 PM.
 
Old 01-28-2005, 07:55 PM   #4
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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.........
 
Old 01-28-2005, 08:05 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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) {
...
}
 
Old 01-28-2005, 08:31 PM   #6
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
ALRIGHT!!!!!!!!

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!!!
 
Old 01-28-2005, 08:37 PM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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;
}
 
Old 01-28-2005, 08:46 PM   #8
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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");
}
 
Old 01-28-2005, 08:51 PM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya, I think that should do it. That will find the '\n' and replace it with a string terminator (0 is a string terninator).
 
Old 01-28-2005, 09:06 PM   #10
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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.......
 
Old 01-28-2005, 09:40 PM   #11
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
ISO C++ forbids comparison between pointer and integer? pimaster Programming 1 11-06-2003 01:45 PM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM
pointer to bog standard integer acid_kewpie Programming 4 02-02-2002 03:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration