简体   繁体   English

尝试从文本文件中逐行读取文本并输出到重新格式化的新文本文件

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

So I am trying to read the text in from file a. 所以我试图从文件中读取文本。 The text is in groups of three 该文本分为三组

Name Country Score( 1 2 3 4 5) which can be any random number and in any order 姓名国家分数(1 2 3 4 5),可以是任何随机数和任何顺序

At this point I am having issues reading in the text into separate arrays 在这一点上,我在将文本读入单独的数组时遇到问题

So far I have this 到目前为止,我有这个

#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()
{

}

Which gives me this error. 这给了我这个错误。 Any ideas would be appreciate it. 任何想法都会很感激。

Error 1 error C3867: 'std::basic_istream<_Elem,_Traits>::getline' : function call missing argument list; 错误1错误C3867: 'std::basic_istream<_Elem,_Traits>::getline' :函数调用缺少参数列表; use '&std::basic_istream<_Elem,_Traits>::getline' to create a pointer to member c:\\users\\justin\\desktop\\lab 7\\lab 7\\lab7source.cpp 71 使用'&std::basic_istream<_Elem,_Traits>::getline'创建指向成员c:\\users\\justin\\desktop\\lab 7\\lab 7\\lab7source.cpp 71

That one's pretty self explanatory when you read it. 当你阅读它时,那个是非常自我解释的。 You have no argument list, getline(args) , for getline. 对于getline,你没有参数列表, getline(args)

Also, please get used to searching for errors like C3867 yourself, it will actually save you some time as there are generally exhaustive examples of every possible cause for them. 此外,请习惯于自己搜索C3867这样的错误,它实际上会节省一些时间,因为通常会有详尽的例子说明每种可能的原因。

Well, basically it's right there in th e error message; 好吧,基本上它就在错误信息中; getline is a function, not a stream object. getline是一个函数,而不是流对象。 Thus replace 从而取代

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

with

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

and you should be one step closer. 你应该更近一步。 But what you really want to do is probably more like letting thePlayer, theCountry and theScore be of type std::string and 但你真正想做的可能更像是让thePlayer,theCountry和theScore属于std :: string类型

myInputFile >> thePlayer >> theCountry >> theScore;

Read about getline function here http://www.cplusplus.com/reference/istream/istream/getline/ 在这里阅读getline函数http://www.cplusplus.com/reference/istream/istream/getline/

You have an array of character pointers but space is not allocated for storing the strings themselves. 您有一个字符指针数组,但没有分配空间来存储字符串本身。 Even if you manage to compile your code, you will end up in crashing your application. 即使您设法编译代码,最终也会导致应用程序崩溃。 You will be better off using string class. 你最好使用字符串类。

Also you are not incrementing i,j,k which will overwrite the same locations. 此外,您没有递增i,j,k将覆盖相同的位置。 The second argument in getline is supposed to be the max size to read from input stream so instead of theCount it should be a bigger number. getline中的第二个参数应该是从输入流中读取的最大大小,因此它应该是一个更大的数字而不是theCount。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM