免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: hellolinux
打印 上一主题 下一主题

在linux下编写的shell怎么可以弄到xp下运行. [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-06-01 14:33 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

谢谢了.

gcc 1.c
1.c: In function `main':
1.c:22: syntax error before "exit"
1.c:27: `Please' undeclared (first use in this function)
1.c:27: (Each undeclared identifier is reported only once
1.c:27: for each function it appears in.)
1.c:27: syntax error before "enter"
1.c:27: stray '\' in program
1.c:27:54: warning: multi-line string literals are deprecated
1.c:28:12: warning: multi-line string literals are deprecated
1.c:37:19: missing terminating ' character
1.c:37:19: warning: character constant too long
1.c:39: `new' undeclared (first use in this function)
1.c:40: `pre' undeclared (first use in this function)
1.c:44: `cur' undeclared (first use in this function)
1.c:46: `string' undeclared (first use in this function)
1.c: In function `save2file':
1.c:64: `w' undeclared (first use in this function)

论坛徽章:
0
12 [报告]
发表于 2005-06-01 14:41 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

printf("lease enter the fapiao serial number,exit program with string \"exit\":\n";  这行。
printf("lease enter the file name for save datas:\n";这行
if (fptr=fopen("string","w"==NULL)这行
再实验一下。


  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>

  4. struct fapiao{
  5.     char fp[30];
  6.     struct fapiao *next;
  7. };
  8. typedef struct fapiao FAPIAO;

  9. main()
  10. {
  11.    char fpstr[30];
  12.    FAPIAO *head;

  13.    void insert(FAPIAO **,char *);
  14.    void save2file(FAPIAO **,char *);

  15.    head=NULL;
  16.    while (1)
  17.    {
  18.      printf("Please enter the fapiao serial number,exit program with string \"exit\":\n");     
  19.      scanf("%s",fpstr);
  20.      if (strcmp(fpstr,"exit")==0) break;
  21.      insert(&head,fpstr);
  22.    }
  23.    printf("Please enter the file name for save datas:\n");
  24.    scanf("%s",fpstr);
  25.    save2file(&head,fpstr);
  26. }

  27. void insert(FAPIAO **head,char *string)
  28. {
  29.     FAPIAO *cur,*pre,*new;
  30.    
  31.     if ((new=(FAPIAO *)malloc(sizeof(FAPIAO)))==NULL)
  32.        {printf("Can't creat new node!\n");return;}
  33.     strcpy(new->fp,string);
  34.     new->next=NULL;
  35.     pre=*head;
  36.     if (pre==NULL)
  37.        *head=new;
  38.     else
  39.        {cur=pre->next;
  40.         while(cur!=NULL)
  41.            {if(strcmp(string,cur->fp)==0)
  42.                {printf("Repeated!\n");return;}
  43.             pre=cur;
  44.             cur=cur->next;
  45.            }
  46.         pre->next=new;
  47.         return;
  48.        }   
  49. }

  50. void save2file(FAPIAO **head,char *string)
  51. {
  52.    FAPIAO *cur,*pre;
  53.    FILE *fptr;

  54.    pre=*head;
  55.    if (pre==NULL) return;
  56.    cur=pre->next;
  57.    if ((fptr=fopen("string","w"))==NULL)
  58.       {printf("Can't creat file!\n");return;}
  59.    while (cur!=NULL)
  60.      {fprintf(fptr,"%30s\n",cur->fp);
  61.         *head=cur;
  62.         cur=cur->next;
  63.         free(pre);
  64.         pre=*head;
  65.      }
  66.    return;
  67. }
复制代码

论坛徽章:
0
13 [报告]
发表于 2005-06-01 14:45 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

可以看错误信息找错啊,呵呵。疏忽了几个双引号。嘿嘿。再有错误信息发上来。

论坛徽章:
0
14 [报告]
发表于 2005-06-01 15:16 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

xp还支持bat么,xp还支持.cmd文件,好像和bat一回事

论坛徽章:
0
15 [报告]
发表于 2005-06-01 15:22 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

谢谢这位朋友的好意思
不过我有几点不理解.
应该是比较2次,你比较3次才打出警告信息.
而且最后保存文件,把文件保存到哪了?

cc 1.c
./a.out
Please enter the fapiao serial number,exit program with string "exit":
123
Please enter the fapiao serial number,exit program with string "exit":
123
Please enter the fapiao serial number,exit program with string "exit":
123
Repeated!
Please enter the fapiao serial number,exit program with string "exit":
exit
Please enter the file name for save datas:
aaa

论坛徽章:
0
16 [报告]
发表于 2005-06-01 15:37 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

看insert函数,把cur=pre->next修改为cur=pre就正确了。
另外文件保存在你程序所在目录内。
你应当去看一些c编程的书。
<<c how to program>>
挺不错的。
  1. void insert(FAPIAO **head,char *string)
  2. {
  3.     FAPIAO *cur,*pre,*new;
  4.    
  5.     if ((new=(FAPIAO *)malloc(sizeof(FAPIAO)))==NULL)
  6.        {printf("Can't creat new node!\n");return;}
  7.     strcpy(new->fp,string);
  8.     new->next=NULL;
  9.     pre=*head;
  10.     if (pre==NULL)
  11.        *head=new;
  12.     else
  13. /**********下面这句改为cur=pre;************/
  14.        {cur=pre->next;
  15.         while(cur!=NULl)
  16.            {if(strcmp(string,cur->fp)==0)
  17.                {printf("Repeated!\n");return;}
  18.             pre=cur;
  19.             cur=cur->next;
  20.            }
  21.         pre->next=new;
  22.         return;
  23.        }   
  24. }
复制代码

论坛徽章:
0
17 [报告]
发表于 2005-06-01 15:49 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

谢谢,其他都搞定,就是看保存的文件时,它记录的发票号还是有点问题.有重复的发票号好像没有存入文件,再次感谢.

论坛徽章:
0
18 [报告]
发表于 2005-06-01 15:56 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

save2file()函数忘了关闭打开的文件描述符,加了条语句。看下面。再编译一下看看。不过我不确定是这个问题。呵呵。
你实验一下吧。

  1. void save2file(FAPIAO **head,char *string)
  2. {
  3.    FAPIAO *cur,*pre;
  4.    FILE *fptr;

  5.    pre=*head;
  6.    if (pre==NULL) return;
  7.    cur=pre->next;
  8.    if (fptr=fopen("string","w")==NULL)
  9.       {printf("Can't creat file!\n");return;}
  10.    while (cur!=NULL)
  11.      {fprintf(fptr,"%30s\n",cur->fp);
  12.         *head=cur;
  13.         cur=cur->next;
  14.         free(pre);
  15.         pre=*head;
  16.      }
  17. /**********不知道是不是没有关闭文件描述符造成的错误*******/
  18.    fclose(fptr);/*加这句*/
  19.    return;
  20. }
复制代码

论坛徽章:
0
19 [报告]
发表于 2005-06-01 16:01 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

谢谢,它保存的文件为string,不是自己建立的文件,它记录的发票号还是有点问题.有重复的发票号好像没有存入文件,而且每次输入的第1个发票号也没有被记录,再次感谢.

论坛徽章:
0
20 [报告]
发表于 2005-06-01 16:11 |只看该作者

在linux下编写的shell怎么可以弄到xp下运行.

我自己转了一下,照着下面改一下,肯定没问题了。
呵呵, 细节没考虑周全。现在你输入什么文件名它就保存什么。
其实不是重复的不能保存,而是第一个数据无法保存。现在好了。



  1. void save2file(FAPIAO **head,char *string)
  2. {
  3.   FAPIAO *cur,*pre;
  4.   FILE *fptr;

  5.   pre=*head;
  6.   if (pre==NULL) return;
  7. /******这里********/
  8.   cur=pre;   /***原来为cur=pre->next***/
  9.   if (fptr=fopen(string,"w")==NULL)   /***原来为"string"*****/
  10.      {printf("Can't creat file!\n");return;}
  11.   while (cur!=NULL)
  12.     {fprintf(fptr,"%30s\n",cur->fp);
  13.        *head=cur;
  14.        cur=cur->next;
  15.        free(pre);
  16.        pre=*head;
  17.     }
  18. /**********不知道是不是没有关闭文件描述符造成的错误*******/
  19.   fclose(fptr);/*加这句*/
  20.   return;
  21. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP