全部博文(489)
分类:
2011-12-22 20:55:02
原文地址:C 输入输出编程 作者:luozhiyong131
#include
#define M 3
#define N 4
int main(){
int a[N][M];
int b[M][N];
int i,j;
printf("Please Input %d*%d intergers:\n",M,N);
for(i=0;i
for(j=0;j
scanf("%d",&a[i][j]);
}
}
for(i=0;i
for(j=0;j
b[j][i]=a[i][j];
}
}
for(i=0;i
for(j=0;j
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
#include
int main(){
char str[5] = {'a','b','c','d','e'};
for(int i=0;i<5;i++){
printf("%c\t",str[i]);
}
printf("\n");
}
#include
#include
int main()
{
FILE *fp;
int ch;
fp = fopen("./test", "w+");
if(fp==NULL)
{
perror("Open text failed");
exit(1);
}
while((ch=getchar())!='\n')
{
fputc(ch,fp);
}
rewind(fp);
printf("Input: ");
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
#include
#include
int main()
{
FILE *fp;
int i,ch,n;
fp = fopen("./test","r");
if(fp==NULL)
{
perror("Open test failed");
exit(1);
}
for(i=0;i<=7;i+=2)
{
fseek(fp,i,SEEK_SET);
n = ftell(fp);
printf("Position :%d\n",n+1);
ch =fgetc(fp);
printf("Character: ");
putchar(ch);
printf("\n");
}
fclose(fp);
return 0;
}
#include
#include
#define size 256
int main()
{
char buffer[size];
int c;
FILE *fp;
fp=fopen("test","r");
if(fp==NULL)
{
perror("Open test failed");
exit(1);
}
c=1;
while((fgets(buffer,size,fp))!=NULL)
{
printf("%d: %s\n",c++,buffer);
}
fclose(fp);
return 0;
}
#include
#include
#define size 256
int main(int argc, char **argv)
{
char buffer[size];
FILE *fp1,*fp2;
if(argc!=3)
{
printf("Too fewargument\n");
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
perror("Open file1 failed");
exit(1);
}
fp2=fopen(argv[2],"w");
if(fp2==NULL)
{
perror("Open file2 failed");
exit(1);
}
while(!feof(fp1))
{
fgets(buffer,size,fp1);
fputs(buffer,fp2);
}
fclose(fp1);
fclose(fp2);
printf("Done!\n");
return 0;
}
#include
#include
#define N 3
struct student
{
int id;
int score;
};
int main()
{
struct student array[N];
int i,n;
FILE *fp;
fp = fopen("./test","w");
if(fp==NULL)
{
perror("Open file failed");
exit(1);
}
for(i=0;i
{
printf("ID: ");
scanf("%d",&array[i].id);
printf("Score: ");
scanf("%d",&array[i].score);
}
n = fwrite(array,sizeof(struct student),N,fp);
if(n!=N)
{
printf("Write file error");
}
fclose(fp);
printf("Write Done!\n");
return 0;
}
#include
#include
#define N 3
struct student
{
int id;
int score;
};
int main()
{
struct student array[N];
int i,n;
FILE *fp;
fp = fopen("./test","r");
if(fp==NULL)
{
perror("Open file failed");
exit(1);
}
n = fread(array,sizeof(struct student),N,fp);
if(n!=N)
{
if(feof(fp))
printf("End of file ");
else
printf("read file error");
}
printf("ID\tScore\n");
for(i=0;i
{
printf("%d\t%d\n",&array[i].id,&array[i].score);
}
fclose(fp);
printf("Read Done!\n");
return 0;
}