Characters written to the file, and then read from the file and display

// when the value is written to the hard disk file in ASCII characters between 1-127, file read these characters show up

#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE *fp;
	char ch;
	if((fp=fopen("demo.bin","wb"))==NULL)//二进制写方式打开文件 
	{
	printf("Failure to open demo.bin!\n");
	exit(0);
	}
	for(int i=0;i<128;i++)
	{
		fputc(i,fp);
	 } 
	 fclose(fp);
	 if((fp=fopen("demo.bin","rb"))==NULL)//二进制读方式打开文件
	 {
	printf("Failure to open demo.bin!\n"); 
	exit(0);
	 }
	 //相同替换
	 //ch=fgetc(fp);  函数feof()判断是否读到文件末尾 +若fp已正确定义并指向某个文件,当未遇到该文件结束标志时函数feof(fp)的值为0,否则为非0值
	 //while(!feof(fp))
	 //{
	 //	putchar(ch);
	 //	ch=fgetc(fp);
	//  } 
	while((ch=fgetc(fp))!=EOF) //文件中读取字符 
	{
		putchar(ch);//显示字符 
	}
	fclose(fp);
	return 0;
 } 

Here Insert Picture Description
// value of the ASCII character when written to disk files between 1-127, the characters read from the file, determines whether the printable

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
	FILE *fp;
	char ch;
	if((fp=fopen("demo.bin","wb"))==NULL)//二进制写方式打开文件 
	{
	printf("Failure to open demo.bin!\n");
	exit(0);
	}
	for(int i=0;i<128;i++)
	{
		fputc(i,fp);//将字符ASCII值写入文件 
	 } 
	 fclose(fp);
	 if((fp=fopen("demo.bin","rb"))==NULL)//二进制读方式打开文件
	 {
	printf("Failure to open demo.bin!\n"); 
	exit(0);
	 }
	 //相同替换
	 //ch=fgetc(fp);  函数feof()判断是否读到文件末尾 
	 //while(!feof(fp))
	 //{
	 //	putchar(ch);
	 //	ch=fgetc(fp);
	//  } 
	while((ch=fgetc(fp))!=EOF) //文件中读取字符 
	{
		if(isprint(ch)) //判断是否为可打印字符 
		printf("%c\t",ch);  
		else
		printf("%d\t",ch);//可打印,显示ASCII值 
		//putchar(ch);//显示字符 
	}
	fclose(fp);
	return 0;
 } 

Here Insert Picture DescriptionThe data related to the case when the file is read out //

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch;
	FILE *fp;
	long pos;//显示文件数据输出时,“位于文件中的位置” 
	if((fp=fopen("demo.1.txt","r"))==NULL)
	{
		printf("Failure to open demo.1.txt!\n");
		exit(0);
	}
	//ch=fgetc(fp);
	pos=ftell(fp);
	ch=fgetc(fp);
       while(!feof(fp))  
        //使用 while(!feof(fp))效果更好,EOF在文件读取结束和读取出错时都将返回相同的结果 
        //判断读取是否出错,使用ferror判断
	{
		printf("%c,%ld\n",ch,pos);
		pos=ftell(fp);
		ch=fgetc(fp); 
		//putchar(ch);
     
	}
	fclose(fp);
	return 0;
 } 

Here Insert Picture Description

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch;
	FILE *fp;
	long pos;//显示文件数据输出时,“位于文件中的位置” 
	if((fp=fopen("demo.1.txt","r"))==NULL)
	{
		printf("Failure to open demo.1.txt!\n");
		exit(0);
	}
	//ch=fgetc(fp);
	pos=ftell(fp);
	ch=fgetc(fp);
       while(!feof(fp))
          //使用 while(!feof(fp))效果更好,EOF在文件读取结束和读取出错时都将返回相同的结果 
          //判断读取是否出错,使用ferror判断
	{
		printf("%c,%ld\n",ch,pos);
		pos=ftell(fp);
		ch=fgetc(fp); 
		//putchar(ch);
     
	}
	fclose(fp);
	return 0;
 } 

Here Insert Picture Description
hello will cover the previous
Open w that if the file already exists it will cover
you want to add to the end is to use the "a"

#include <stdio.h> 
#include<stdlib.h>
#include <string.h>
void Fun(char *fname,char *st)
{ 
        FILE *myf;
		int i;
        myf=fopen(fname,"w");
        if (myf == NULL)
        {
            printf("cannot open the file.\n");
            exit(0);
        }
       for(i=0;i<strlen(st); i++)
       {
            fputc(st[i],myf);
       }
       fclose(myf);
}
int main()
    { 
        Fun("test","new world"); 
        Fun("test","hello");
        return 0;
    }

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/CSDN447447LJH/article/details/91476245