C语言本身是不能输入输出的。C语言程序库内包含了printf和scanf这两个函数当用户需要输入输出时就要输入这两个函数编译时C语言程序库则调用这两个函数。C语言采用方式使得语言功能的扩充十分方便。如果需要增加新的功能只需要在函数库中添加相应的函数即可;
而如果一个函数的功能需要进行调整也只需要修改函数本身的代码但不需要修改调用了该函数的其他程序。
C语言中本身具有的函数称为系统函数用户可以直接调用这些函数完成相应的功能。例如printf、fabs等都是系统函数。系统函数被保存在称为“C函数库“的系统文件中当需要使用一个函数时应当通知系统该函数所在的函数库这是通过包含头文件的方式来实现的。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
int main()
{
char buf[MAX_LINE] /*缓冲区*/
FILE *fp /*文件指针*/
int len/*行字符个数*/
if((fp = fopen("test.txt","r")) == NULL) /*如果读取失败*/
{
perror("fail to read")
exit (1)
}
while(fgets(buf,MAX_LINE,fp) != NULL) /*如果指针指到的行还有内容*/
{
len = strlen(buf)
buf[len-1] = '\0' /*去掉换行符*/
printf("%s %d \n",buf,len - 1)/*输出读取的内容*/
}
return 0
}
希望能帮到你
char *strcpy(char *strDes, const char *strSrc){
assert((strDes != NULL) &&(strSrc != NULL))
char *address = strDes
while ((*strDes ++ = *strSrc ++) != '\0')
NULL
return address
}
char *strchr_(char *str, int c)
{
assert(str != NULL)
while ((*str != (char) c) &&(*str != '\0'))
str ++
if (*str != '\0')
return str
return NULL
}
char *strchr(const char *str, int c)
{
assert(str != NULL)
for (*str != (char) c++ str)
if (*str == '\0')
return NULL
return (char *) str
}
int strcmp(const char *s, const char *t)
{
assert(s != NULL &&t != NULL)
while (*s &&*t &&*s == *t)
{
++ s
++ t
}
return (*s - *t)
}
char *strcat(char *strDes, const char *strSrc)
{
assert((strDes != NULL) &&(strSrc != NULL))
char *address = strDes
while (*strDes != '\0')
++ strDes
while ((*strDes ++ = *strSrc ++) != '\0')
NULL
return address
}
int strlen(const char *str)
{
assert(str != NULL)
int len = 0
while (*str ++ != '\0')
++ len
return len
}
char *strdup(const char *strSrc)
{
assert(strSrc != NULL)
int len = 0
while (*strSrc ++ != '\0')
++ len
char *strDes = (char *) malloc (len + 1)
while ((*strDes ++ = *strSrc ++) != '\0')
NULL
return strDes
}
char *strstr(const char *strSrc, const char *str)
{
assert(strSrc != NULL &&str != NULL)
const char *s = strSrc
const char *t = str
for (*t != '\0'++ strSrc)
{
for (s = strSrc, t = str*t != '\0' &&*s == *t++s, ++t)
NULL
if (*t == '\0')
return (char *) strSrc
}
return NULL
}
char *strncpy(char *strDes, const char *strSrc, int count)
{
assert(strDes != NULL &&strSrc != NULL)
char *address = strDes
while (count -- &&*strSrc != '\0')
*strDes ++ = *strSrc ++
return address
}
char *strncat(char *strDes, const char *strSrc, int count)
{
assert((strDes != NULL) &&(strSrc != NULL))
char *address = strDes
while (*strDes != '\0')
++ strDes
while (count -- &&*strSrc != '\0' )
*strDes ++ = *strSrc ++
*strDes = '\0'
return address
}
int strncmp(const char *s, const char *t, int count)
{
assert((s != NULL) &&(t != NULL))
while (*s &&*t &&*s == *t &&count --)
{
++ s
++ t
}
return (*s - *t)
}