My First Try On File I/O

We want to convert the lower case to upper case, and upper case to lower case,
and then store the content we typed as a file named filename.txt
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
int main() {
    using namespace std;
    //we want to convert the lower case to upper case, and upper case to lower case
    //and then store the content we typed as a file named filename.txt
    
    string content;
    char in;
    cout << "Your can type a sentence whatever you want"
            "\n(All the spaces will be ignored)"
            "\n(End with semi-colon symbol ;)\n"
    ;
    
    for(;cin >> in&&in!=';';){
        if(in<=90&&in>64){//convert the lower case to upper case
            in += 32;
            content +=in;
        }
        else if(in<=122&&in>96){//convert the upper case to lower case
            in -= 32;
            content +=in;
        }
        else{
            content +=in;
        }
    }

    //the following 2 blocks are about outporting
    ofstream outFile;
    string filename;
    cout << "Please enter your filename so we can store your input into a file (.txt):\n";
    cin >> filename;
    filename += ".txt";
    const char* tempFilename= filename.c_str();// !!!!! a confusion about converting string class to char array
    outFile.open(tempFilename);//creat a file named filename.txt
    
    //we outport the content into the filename.txt
    outFile << content;
    outFile.close();
    
    return 0;
}

 

posted @ 2020-11-30 23:10  VVEN2014  阅读(59)  评论(0)    收藏  举报