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)
相关文档:
网上一道金山的面试题:
http://topic.csdn.net/u/20100524/14/0eff992a-2849-4db6-bdaa-d4a200e79b7c.html
请分别用C++的面向对象和泛型机制,编写实现Template Method模式的示例代码,并比较两种方式各自的优缺点。
用虚函数实现Template Method的方式就不多说了。用泛型的方式实现多态在ATL里面有大量的用到!
泛型� ......
例) 危険なコーディング
1 char cStr[256];
2 ZeroMemory(cStr, sizeof(cStr));
3 &nb ......
《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个字符的指针。但是,当数组名用于表达式中时,他们的值也是一个指针常量 ......
在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的,要在此处引用”。
一.extern修饰变量的声明
举例来说,如果文件a.c需要引用b.c中变量int v,就可以在a.c中声明extern int v,然后就可以引用变量v。这里需要注意的是,被引用的变量v的链接属性必须是外链接(external� ......