文件处理
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
FILE *f;
if((f=fopen("hello.txt","w+"))==NULL){
cout<<"打开失败!"<<endl;
}
char str[30]="hello world!";
char buffer[30];
fputs(str,f); // 写入字符串
fclose(f); // 把f里面的nextc复位
f=fopen("hello.txt","r");
fgets(buffer,13,f);
cout<<buffer<<endl;
fclose(f);
return 0;
}
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
FILE *f;
if((f=fopen("hello.txt","w+"))==NULL){
cout<<"打开失败!"<<endl;
}
char str[30]="hello world!";
char buffer[30];
fputs(str,f); // 写入字符串
fclose(f); // 把f里面的nextc复位
f=fopen("hello.txt","r");
fread(buffer,sizeof(char),13,f); //12会越界/0
buffer[12]='\0'; // 人为加斜杠0 就不会出错的
//fgets(buffer,13,f);
cout<<buffer<<endl;
fclose(f);
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-38407.html