文件输入输出读取文本文件c ++

时间:2015-03-28 12:36:00

标签: c++

我的任务是编写一个读取文本文件的程序,然后将一些有关其内容的统计结果打印到输出文件中。首先要求用户输入输入文件的名称,然后输入输出文件的名称。输入文件可以是包含纯文本的任何文件。所以我想要它做的是读取输入文件的内容,然后在输出文件中输入有关输入文件内容的以下信息。此外,我正在使用MS Visual Studio进行此任务,我不知道我到目前为止是否正确,但我认为我必须为输入文本文件创建一个.cpp,为输出文件创建一个.cpp然后一个执行。我在创建和使用文本文件方面也有点失落。这是我得到的:

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
    char filename[100];     // a string for filenames
    ifstream input;         // a file input stream
    ofstream output;        // a file output stream

    do
    {
        input.clear();      // to clear status flags in the stream
        cout << "Please enter the name of the input file.\n";
        cout << "Filename: ";
        cin >> filename;
        input.open(filename);
        if (!input)
            cout << "Sorry not a valid file. Try again!\n";
    } while (!input);



    do
    {
        output.clear();     // to clear status flags in the stream
        cout << "Please enter the name of the output file.\n";
        cout << "Filename: ";
        cin >> filename;
        output.open(filename);
        if (!output)
            cout << "Sorry not a valid file. Try again!\n";
    } while (!output);


    char ch;
    int characters = 0;
    int letters = 0;
    int white_space = 0;
    int digits = 0;
    int other_characters = 0;
    int uppercase = 0;
    int lowercase = 0;

    while (!input.eof());   // while not end of input file
    {
        input.get(ch);

        if(isalnum(ch))
            characters++;
        output << ch;

        if(isalpha(ch))
            ++letters;
        output << ch;

        if(isspace(ch))
            ++white_space;
        output << ch;

        if(isdigit(ch))
            ++digits;
        output << ch;

        if(ispunct(ch))
            ++other_characters;
        output << ch;

    }
    cout << "Processing complete\n";

    double perc_letters = 0;
    double perc_white_space = 0;
    double perc_digits = 0;
    double perc_other_characters = 0;

    cout << "\nStatistics for file:" << filename << endl;
    cout << "--------------------------------------------------\n";

    cout << "Total # of charcters in file:\t " << characters << endl;
    cout << "\nCategory\tHow many in file\t% of file\n";
    cout << "--------------------------------------------------\n";
    cout << "Letters\t" << letters << "\t" << perc_letters << "%" << endl;
    cout << "White space\t" << white_space << "\t" << perc_white_space << "%" << endl;
    cout << "Digits\t" << digits << "\t" << perc_digits << "%" << endl;
    cout << "Other characters\t" << other_characters << "\t" << perc_other_characters << "%" << endl;
    cout << "\n";

    input.close();
    output.close();

    return 0;

}

1 个答案:

答案 0 :(得分:0)

如果将它们放在不同的cpp文件中,那应该没什么区别,这只是为了建立良好的代码结构。 我不是100%肯定你将如何完成任务,但如果你使用的是C ++,那么getLine()函数非常方便。你可以这样做:

    while (std::getline(File, line)){
        if (line[0] == 'v' && line[1] == ' '){
            sscanf_s(&line[0], "v %f, &x);}
Thats gonna store a float value in x on that line if the line starts with: "v "