簡體   English   中英

字符串C ++的問題

[英]Problems with strings C++

我無法弄清楚如何閱讀幾段左右的內容,也無法將每個單詞放入結構中,也無法計算出有多少個唯一的單詞。

我知道如何讀取數據只是不從具有一行的字符串中傳輸一個單詞。 正如我之前讀過的幾段所述

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


struct words
{
    char unique[21];
    int count;
} test;

void inputFile (words essay[100]);
int search (int index, int subscript, int integer,words essay[100]);

int main()
{
    words essay[100];
    inputFile (&test);

    cout << essay[0].unique<<test.count;
    return 0;
}


void inputFile (words essay[100])
{
    char fileName[81];
    char copyName[81];
    cout << "What is the name of the input file? \n";
    cin >> fileName;


    ifstream infile;
    ofstream outfile;
    int count = 0;
    char line [81];
    int ch;
    infile.open(fileName);
    outfile.open(copyName);

    while ((ch = infile.peek()) != EOF)
    {   // while not end of file
        infile.getline (line[81].unique, 81);
        cout << "Copying: " << line << endl;


        count++;
        outfile << essay << endl;
    }

    cout << "There were " << count << " lines copied\n";
    cout << endl;
    // close both files
    infile.close ();
    outfile.close ();
 };


/*int search (int index, int subscript, int integer, struct words essay[100])
{
    string key;
    key = test.unique;
    int n;
    n = test.count;
    int i;
    i = 0;
    while (i < n && essay[i] != key)
    i++;
    if (i == n)
    {
        i = -1;
    }
    return i;
    };*/

這是部分答案,主要側重於清理流讀取邏輯。 我無法弄清楚您在做什么,無法提供更多幫助。 (例如,你為什么輸出essayoutfile ,而不必把任何東西在里面。)

void inputFile (words essay[100])
{
    std::string fileName, copyName;
    cout << "What is the name of the input file?\n";
    cin >> fileName;
    // I assume you get copyName here…
    // Though fileName and copyName really should be parameters
    // passed in from `main()`.

    ifstream infile(fileName);
    ofstream outfile(copyName);
    std::string line;
    int count = 0;
    while (getline(infile, line))
    {
        cout << "Copying: " << line << endl;
        count++;
        outfile << essay << endl; // No idea what you're doing here.
    }

    cout << "There were " << count << " lines copied\n";
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM