[英]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) 。
好吧,基本上它就在错误信息中; 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.