ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 |
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.
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.
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
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.
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!!!
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");
}
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.......
#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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.