- 论坛徽章:
- 0
|
最近要从文件中读些内容,在其他地方用,写到链表里。- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct client_t {
- char *client;
- struct client_t *next;
- };
- int
- main (int argc,char **argv)
- {
- FILE *fp;
- char *str1 = NULL;
- char *str2 = NULL;
- char *str3 = NULL;
- char *str4 = NULL;
- char *str5 = NULL;
- char *tmp = NULL;
- char *string = "helloworld!";
- struct client_t *head,*p;
- char buff[100] = {0,};
- int i = 0;
- int j = 0;
- j = atoi(argv[1]);
- str5 = argv[2];
- if ((fp = fopen (str5, "r")) == NULL) {
- //if ((fp = fopen ("./cc", "r")) == NULL) {
- printf("error!");
- return -1;
- }
- for(i;i<j;i++) {
- fgets(buff,100,fp);
- }
- tmp = buff;
- tmp = strtok_r(tmp, "\n",&str4);
- str3 = strtok_r(tmp," ", &str4);
- head = malloc(sizeof (struct client_t));
- head->client = str3;
- head->next = NULL;
- for(;str2 = strtok_r (NULL, " ", &str4);) {
- p = malloc(sizeof (struct client_t));
- p->client = str2;
- if (head == NULL) {
- head = p;
- head->next = NULL;
- } else {
- p->next = head->next;
- head->next = p;
- }
- }
- while (head) {
- printf("%s ",head->client);
- head = head->next;
- }
-
- pclose (fp);
- return 0;
- }
复制代码 |
|