c_c++_io之File

#include <iostream>
#include <fstream>

using namespace std;
void createFile(){
    fstream fs("cplus.txt",ios::trunc);

}
void writeFile(){
    ofstream of("cplus.txt",ios::app);
    of<<"hello.txt"<<endl;
    of.close();

}
void readFile(){
     fstream fin("cplus.txt",ios::in);
     char *buffer;
     //char buffer[22]?;这样才行?
     //cout<<buffer<<endl;
     fin.getline(buffer,10);
     //cout<<"error"<<endl;
     if(fin.eof()){
        cout<<"error"<<endl;
     }
     fin.close();
    //

}
int main(){
        // createFile();
         //writeFile();
         readFile();


}

 必须用gcc 编译,不能用g++,否则出错
#include <stdio.h>
#include <stdlib.h>

void createFile(char* const fileName){
//fileName="hello";
FILE *file=fopen(fileName,"w+"); //创建一个可读写的文件
fclose(file);
}
char *read(char *fileName,int length){

char *readString;
FILE *file;
char *p;
file=fopen(fileName,"r");
p=fgets(readString,length,file);
printf("str%s\n",p); //
printf("readString%s\n",readString);//跟上一句效果一样
fclose(file);

return "hello";


}
void write(const char* fileName,char * const writeString){
FILE *file;
file=fopen(fileName,"w");
fputs(writeString,file);
fclose(file);

}
char *test2(char *p){
char *a=p;
return a;
}
int *test(){
int *p;
int a=12;
p=&a;
return p;
}
int main(){
// createFile("hello2.txt");
// write("hello2.txt","大家好");


char *p=read("hello2.txt",22);
printf("读取到得数是%s\n",p);

}

 

你可能感兴趣的:(ios,C++,c,C#,gcc)