- 论坛徽章:
- 0
|
我有一个问题,请指教
代码如下
- #include <stdio.h>;
- #include <string.h>;
- //#include <io.h>;
- #include <stdlib.h>;
- #include <unistd.h>;
- #define OK 0
- #define NG -1
- #define F_OK 0
- #define LEN sizeof(struct sList)
- struct sList{
- char * sName;
- struct sList * next;
- };
- void ConfigList(FILE *fp);
- int main()
- {
- FILE *fpread;
- if(!access("eg.cfg",F_OK))
- {
- if((fpread=fopen("eg.cfg","r"))==NULL)
- {
- printf("open fail\n");
- }
- else{}
- ConfigList(fpread);
- fclose(fpread);
- }
- else
- {
- printf("NO SUCH FILE\n");
- return NG;
- }
- return OK;
- }
- void ConfigList(FILE *fp)
- {
- char buf[1024];
- char *p;
- struct sList *head,*q1,*q2;
- int n = 0;
- q1 = q2 = (struct sList *)malloc(LEN);
- head = NULL;
- while ( !feof( fp ) ) {
- memset(buf, NULL, 1024);
- fgets( buf, sizeof( buf ), fp );
- if(strlen(buf)==1)
- {}
- else
- {
- if(buf[0]=='[')
- {
- q1 = (struct sList *)malloc(LEN);
- n = n + 1;
- p = strtok(buf,"[]");
- q1->;sName = p;
- if(n==1)head = q1;
- else q2->;next = q1;
- q2 = q1;
- printf("%s\n",q1->;sName); //这里可以打印出来具体内容
- }
- }
- }
- printf("%s\n",p); //这里打印出来是空行
- q2->;next = NULL;
- q1=head;
- if(q1!=NULL)
- do
- {
- printf("%s\n",q1->;sName); //这里打印出来也是空行
- q1 = q1->;next;
- }
- while(q1!=NULL);
- }
复制代码
我是想读取文件,然后将“[]”中的内容保存在链表结构中,可一直不成功,请各位大大帮帮忙。我觉得是strtok()的问题,不过不能确定。请指教。 |
|