数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz

C语言的函数!

  [复制链接]
 楼主| 发表于 2004-5-10 16:57:03 | 显示全部楼层
<>函数名: ungetch
功  能: 把一个字符退回到键盘缓冲区中
用  法: int ungetch(int c);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;ctype.h&gt;
#include &lt;conio.h&gt; <>int main( void )
{
   int i=0;
   char ch; <P>   puts("Input an integer followed by a char:"); <P>   /* read chars until non digit or EOF */
   while((ch = getche()) != EOF &amp;&amp; isdigit(ch))
      i = 10 * i + ch - 48; /* convert ASCII into int value */ <P>   /* if non digit char was read, push it back into input buffer */
   if (ch != EOF)
      ungetch(ch); <P>   printf("\n\ni = %d, next char in buffer = %c\n", i, getch());
   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:57:38 | 显示全部楼层
<>函数名: unixtodos
功  能: 把日期和时间转换成DOS格式
用  法: void unixtodos(long utime, struct date *dateptr,
   struct time *timeptr);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;dos.h&gt; <>char *month[] = {"---", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; <P>#define SECONDS_PER_DAY 86400L  /* the number of seconds in one day */ <P>struct date dt;
struct time tm; <P>int main(void)
{
   unsigned long val; <P>/* get today's date and time */
   getdate(&amp;dt);
   gettime(&amp;tm);
   printf("today is %d %s %d\n", dt.da_day, month[dt.da_mon], dt.da_year); <P>/* convert date and time to unix format (number of seconds since Jan 1, 1970 */
   val = dostounix(&amp;dt, &amp;tm);
/* subtract 42 days worth of seconds */
   val -= (SECONDS_PER_DAY * 42); <P>/* convert back to dos time and date */
   unixtodos(val, &amp;dt, &amp;tm);
   printf("42 days ago it was %d %s %d\n",
        dt.da_day, month[dt.da_mon], dt.da_year);
   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:57:52 | 显示全部楼层
<>函数名: unlink
功  能: 删掉一个文件
用  法: int unlink(char *filename);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;io.h&gt; <>int main(void)
{
   FILE *fp = fopen("junk.jnk","w");
   int status; <P>   fprintf(fp,"junk"); <P>   status = access("junk.jnk",0);
   if (status == 0)
      printf("File exists\n");
   else
      printf("File doesn't exist\n"); <P>   fclose(fp);
   unlink("junk.jnk");
   status = access("junk.jnk",0);
   if (status == 0)
      printf("File exists\n");
   else
      printf("File doesn't exist\n");
  <P>   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:58:03 | 显示全部楼层
<>函数名: unlock
功  能: 解除文件共享锁
用  法: int unlock(int handle, long offset, long length);
程序例: <>#include &lt;io.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;sys\stat.h&gt;
#include &lt;process.h&gt;
#include &lt;share.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   int handle, status;
   long length; <P>   handle = sopen("c:\\autoexec.bat",O_RDONLY,SH_DENYNO,S_IREAD); <P>   if (handle &lt; 0)
   {
       printf("sopen failed\n");
       exit(1);
   } <P>   length = filelength(handle);
   status = lock(handle,0L,length/2); <P>   if (status == 0)
      printf("lock succeeded\n");
   else
      printf("lock failed\n"); <P>   status = unlock(handle,0L,length/2); <P>   if (status == 0)
      printf("unlock succeeded\n");
   else
      printf("unlock failed\n"); <P>   close(handle);
   return 0;
}
  </P>
 楼主| 发表于 2004-5-10 16:58:32 | 显示全部楼层
  
  <>函数名: vfprintf
功  能: 送格式化输出到一流中
用  法: int vfprintf(FILE *stream, char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdarg.h&gt; <>FILE *fp; <P>int vfpf(char *fmt, ...)
{
   va_list argptr;
   int cnt; <P>   va_start(argptr, fmt);
   cnt = vfprintf(fp, fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber = 30;
   float fnumber = 90.0;
   char string[4] = "abc"; <P>   fp = tmpfile();
   if (fp == NULL)
   {
      perror("tmpfile() call");
      exit(1);
   } <P>   vfpf("%d %f %s", inumber, fnumber, string);
   rewind(fp);
   fscanf(fp,"%d %f %s", &amp;inumber, &amp;fnumber, string);
   printf("%d %f %s\n", inumber, fnumber, string);
   fclose(fp); <P>   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:58:48 | 显示全部楼层
<>函数名: vfscanf
功  能: 从流中执行格式化输入
用  法: int vfscanf(FILE *stream, char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdarg.h&gt; <>FILE *fp; <P>int vfsf(char *fmt, ...)
{
   va_list  argptr;
   int cnt; <P>   va_start(argptr, fmt);
   cnt = vfscanf(fp, fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber = 30;
   float fnumber = 90.0;
         char string[4] = "abc"; <P>   fp = tmpfile();
   if (fp == NULL)
   {
      perror("tmpfile() call");
      exit(1);
   }
   fprintf(fp,"%d %f %s\n",inumber,fnumber,string);
   rewind(fp); <P>   vfsf("%d %f %s",&amp;inumber,&amp;fnumber,string);
   printf("%d %f %s\n",inumber,fnumber,string);
   fclose(fp); <P>   return 0;
}
  
  </P>
 楼主| 发表于 2004-5-10 16:59:04 | 显示全部楼层
<>函数名: vprintf
功  能: 送格式化输出到stdout中
用  法: int vprintf(char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;stdarg.h&gt; <>int vpf(char *fmt, ...)
{
   va_list argptr;
   int cnt; <P>   va_start(argptr, format);
   cnt = vprintf(fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber = 30;
   float fnumber = 90.0;
   char *string = "abc"; <P>   vpf("%d %f %s\n",inumber,fnumber,string); <P>   return 0;
}
  
  </P>
 楼主| 发表于 2004-5-10 16:59:28 | 显示全部楼层
<>函数名: vscanf
功  能: 从stdin中执行格式化输入
用  法: int vscanf(char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;stdarg.h&gt; <>int vscnf(char *fmt, ...)
{
   va_list argptr;
   int cnt; <P>   printf("Enter an integer, a float,  and a string (e.g. i,f,s,)\n");
   va_start(argptr, fmt);
   cnt = vscanf(fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber;
   float fnumber;
   char string[80]; <P>   vscnf("%d, %f, %s", &amp;inumber, &amp;fnumber, string);
   printf("%d %f %s\n", inumber, fnumber, string); <P>   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:59:42 | 显示全部楼层
<>函数名: vsprintf
功  能: 送格式化输出到串中
用  法: int vsprintf(char *string, char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;stdarg.h&gt; <>char buffer[80]; <P>int vspf(char *fmt, ...)
{
   va_list argptr;
   int cnt; <P>   va_start(argptr, fmt);
   cnt = vsprintf(buffer, fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber = 30;
   float fnumber = 90.0;
   char string[4] = "abc"; <P>   vspf("%d %f %s", inumber, fnumber, string);
   printf("%s\n", buffer);
   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-10 16:59:50 | 显示全部楼层
<>函数名: vsscanf
功  能: 从流中执行格式化输入
用  法: int vsscanf(char *s, char *format, va_list param);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;stdarg.h&gt; <>char buffer[80] = "30 90.0 abc"; <P>int vssf(char *fmt, ...)
{
   va_list  argptr;
   int cnt; <P>   fflush(stdin); <P>   va_start(argptr, fmt);
   cnt = vsscanf(buffer, fmt, argptr);
   va_end(argptr); <P>   return(cnt);
} <P>int main(void)
{
   int inumber;
   float fnumber;
   char string[80]; <P>   vssf("%d %f %s", &amp;inumber, &amp;fnumber, string);
   printf("%d %f %s\n", inumber, fnumber, string);
   return 0;
}
  <P> </P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-11-27 06:32 , Processed in 0.049661 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表