簡體   English   中英

嘗試從文本文件中逐行讀取文本並輸出到重新格式化的新文本文件

[英]Trying to read in text line by line from a text file and output to a new text file reformatted

所以我試圖從文件中讀取文本。 該文本分為三組

姓名國家分數(1 2 3 4 5),可以是任何隨機數和任何順序

在這一點上,我在將文本讀入單獨的數組時遇到問題

到目前為止,我有這個

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

int main()
{
    char* thePlayer[20];
    char* theCountry[20];
    char* theScore[100];

    fstream myInputFile("playerData.txt");
    fstream myOutputFile;

    // int highestRank = computeHighestRank();

    // myInputFile.open("playerData",    ios::in);
    // myOutputFile.open("playerReport", ios::out);

    myInputFile.open("playerData.txt"); //, ios::in);

    int theCount = 1;
    int i = 0;
    int j = 0;
    int k = 0;

while (! myInputFile.eof()) {


        myInputFile.getline << (thePlayer[i], theCount, '\n');
        theCount++;
        myInputFile.getline << (theCountry[j], theCount, '\n');
        theCount++;
        myInputFile.getline << (theScore[k], theCount, '\n');
        theCount + 2;
        cout << thePlayer[i] << endl;
        cout << theCountry[j] << endl;
        cout << theScore[k] << endl;
      }

    myOutputFile << "         1         2         3         4         5         6" << "\n\n" << "123456789012345678901234567890123456789012345678901234567890" << "\n\n" << "Player             Country             Highest Rank         " << "\n\n" << "------------------------------------------------------------" << "\n\n";

int computeHighestRank()
{

}

這給了我這個錯誤。 任何想法都會很感激。

錯誤1錯誤C3867: 'std::basic_istream<_Elem,_Traits>::getline' :函數調用缺少參數列表; 使用'&std::basic_istream<_Elem,_Traits>::getline'創建指向成員c:\\users\\justin\\desktop\\lab 7\\lab 7\\lab7source.cpp 71

當你閱讀它時,那個是非常自我解釋的。 對於getline,你沒有參數列表, getline(args)

此外,請習慣於自己搜索C3867這樣的錯誤,它實際上會節省一些時間,因為通常會有詳盡的例子說明每種可能的原因。

好吧,基本上它就在錯誤信息中; getline是一個函數,而不是流對象。 從而取代

myInputFile.getline << (thePlayer[i], theCount, '\n');

myInputFile.getline(thePlayer[i], theCount, '\n');

你應該更近一步。 但你真正想做的可能更像是讓thePlayer,theCountry和theScore屬於std :: string類型

myInputFile >> thePlayer >> theCountry >> theScore;

在這里閱讀getline函數http://www.cplusplus.com/reference/istream/istream/getline/

您有一個字符指針數組,但沒有分配空間來存儲字符串本身。 即使您設法編譯代碼,最終也會導致應用程序崩潰。 你最好使用字符串類。

此外,您沒有遞增i,j,k將覆蓋相同的位置。 getline中的第二個參數應該是從輸入流中讀取的最大大小,因此它應該是一個更大的數字而不是theCount。

暫無
暫無

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

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