090928日c语言学习日记(文件I/O)
#include<stdio.h>
#include<stdlib.h>
#define MAX 41
static int i=0;
int main(void)
{
FILE *fp;
char words[MAX];
if((fp=fopen("words","a+"))==NULL)
{
fprintf(stdout,"Can't open \" word\" file\n");
exit(1);
}
puts("Enter words to add to the file,press the enter.");
puts("Key at the begining of a line to terminate.");
while(gets(words)!=NULL&&words[0]!='\0')
{
fprintf(fp,"%s",words);
i++;
}
rewind(fp);
while(fscanf(fp,"%s",words)==1)
{
puts(words);
}
if(fclose(fp)!=0)
{
fprintf(stderr,"Error closing file.\n");
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define MAX 2000
int main(void)
{
FILE *fp;
char words[MAX];
int wordct=0;
if((fp=fopen("words","a+"))==NULL)
{
fprintf(stderr,"Can't open \" word\" file\n");
exit(1);
}
rewind(fp);
while (fgets(words, MAX - 1, fp) != NULL)
wordct++;
rewind(fp);
puts("Enter words to add to the file,press the enter.");
puts("Key at the begining of a line to terminate.");
while(gets(words)!=NULL&&words[0]!='\0')
{
fprintf(fp,"%d:%s",++wordct,words);
}
puts("File contents:");
rewind(fp);
while(fgets(words,MAX-1,fp)!=NULL)
{
fputs(words,stdout);
}
if(fclose(fp)!=0)
{
fprintf(stderr,"Error closing file.\n");
}
return 0;
}
/*文件名由用户输入,建立一个循环,让用户输入文件位置,
则打印位置到下一个换行符之间的字符,当输入非数字字符时退出*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 41
int main(void)
{
FILE *fp;
char ch;
char file[MAX];
long address;
puts("请输入文件名");
gets(file);
if((fp=fopen(file,"rb"))==NULL)
{
fprintf(stderr,"Can't open the %s\n",file);
exit(1);
}
printf("请输入一个文件位置\n");
while(1)
{
scanf("%ld",&address);
if(isdigit(address))
{
break;
}
fseek(fp,address,SEEK_SET);
while((ch=getc(fp))!='\n' && (ch=getc(fp)
相关文档:
《C专家编程》学习总结2
转自 : http://blog.chinaunix.net/u2/87570/showart_2120069.html
编译器做了些什么
图1:编译器通常分割成几个更小的程序
静态链接与动态链接
  ......
void mystery(int n)
{
n += 5;
n /= 10;
printf(" :%s\n","***********" + 10 -n);
}
当一个字符串常量位于一个表达式中时,它的值是一个指针常量。编译器把这些指定字符的一份拷贝存储在内存的某个位置,并存储一个指向第1个字符的指针。但是,当数组名用于表达式中时,他们的值也是一个指针常量 ......
linux常用头文件如下:
POSIX标准定义的头文件
<dirent.h>
目录项
<fcntl.h> 文件控制
<fnmatch.h> 文件名匹配类型
<glob.h>
路径名模式匹配类型
< ......
原著:Andrew Koenig - AT&T Bell Laboratories Murray Hill, New Jersey 07094
翻译:lover_P
0 简介
C语言及其典型实现被设计为能被专家们容易地使用。这门语言简洁并附有表达力。但有一些限制可以保护那些浮躁的人。一个浮躁的人可以从这些条款中获得一些帮助。
&nbs ......
C语言程序可以看成由一系列外部对象构成,这些外部对象可能是变量或函数。而内部变量是指定义在函数内部的函数参数及变量。外部变量定义在函数之外,因此可以在许多函数中使用。由于C语言不允许在一个函数中定义其它函数,因此函数本身只能是“外部的”。
由于 ......