简体   繁体   English

在C ++中将输入从文本文件读取到数组

[英]Reading input from text file to array in c++

Alright, be gentle, since I'm very much a novice to programming. 好吧,请保持谦虚,因为我是编程的新手。 So far I've only studied C++ and I'm running Visual Studio 2010 as my compiler. 到目前为止,我仅研究过C ++,并且正在将Visual Studio 2010作为编译器运行。 For this program, I'm trying to read from a text input file and write the information to a set of three arrays. 对于此程序,我试图从文本输入文件中读取并将信息写入一组三个数组中。 One array will handle a list of names, and the other two are for hours worked and hourly pay rate, respectively. 一个数组将处理一个名称列表,另外两个数组分别用于工作小时数和小时工资率。 I will use the latter two to calculate a set of earnings and output the whole thing to another text file as a report. 我将使用后两者来计算一组收益,并将整个结果输出到另一个文本文件中作为报告。 My problem, however, is with acquiring input for the first array. 但是,我的问题是获取第一个数组的输入。 The input file I'm using has text arranged like this: 我正在使用的输入文件的文本排列如下:

J. Doe* 35 12.50 J. Dawn* 20 10.00 ......... J.多伊* 35 12.50 J.黎明* 20 10.00 .........

The names are trailed by asterisks since I'm trying to use ifstream getline to acquire the names with the asterisks acting as delimiters, and writing the following two numbers into the other two arrays. 由于我试图使用ifstream getline获取以星号作为定界符的名称,并将以下两个数字写入其他两个数组,因此名称以星号结尾。 The latter two values are separated by whitespaces, so I don't think they'll cause any problems. 后两个值由空格分隔,因此我认为它们不会引起任何问题。 I'm sure there are other errors that need handling but I need to work through the first error before I can start debugging the rest. 我确定还有其他错误需要处理,但是我需要先解决第一个错误,然后才能开始调试其余错误。

My problem occurs with the line where I call inFile.getline, since I get the following error: error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::string' to 'char *'. 我的问题出现在我调用inFile.getline的行中,因为出现以下错误:错误C2664:'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: getline(_Elem *,std :: streamsize,_Elem)':无法将参数1从'std :: string'转换为'char *'。

From what I've read elsewhere, (I think) the problem stems from trying to write a string to a char array, which won't work since they are of different data types. 根据我在其他地方阅读的内容,(我认为)问题出在试图将字符串写入char数组的原因,因为它们具有不同的数据类型,因此无法正常工作。 I'm not sure if other feasible methods exist for acquiring the names since I need the delimiter to separate the names from the numerical values. 我不确定是否存在其他可行的方法来获取名称,因为我需要使用定界符将名称与数值分开。 Any advice on how to resolve this issue would be much appreciated. 任何有关如何解决此问题的建议将不胜感激。

Here is the source I've written: 这是我写的资料:

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

const int EMP_NUM = 5;
const int BASE_HOURS = 40;
const char N_SIZE = 8;

int main()
{
 int i;
 double rEarnings, oEarnings, tEarnings,
 trEarnings, toEarnings, ttEarnings;
 ifstream inFile;
 ofstream outFile;
 inFile.open("records.txt");
 outFile.open("report.txt");

 outFile << setprecision(2) << showpoint << fixed;

 outFile << setw(50) << "Payroll Report" << "\n\n";
 outFile << "EMPLOYEE NAME" << setw(25) << "REGULAR EARNINGS" << setw(25) << "OVERTIME EARNINGS" << setw(25) << "TOTAL EARNINGS" << endl;

 string nameAr[EMP_NUM];
 int hoursAr[EMP_NUM];
 double hrateAr[EMP_NUM];

 for (int i = 0; i < EMP_NUM; i++) // Get input from our input file.
 {
  inFile.getline(nameAr[i], EMP_NUM, "*");
  inFile >> hoursAr[i] >> hrateAr[i];
 }

 for (int i = 0; i < EMP_NUM; i++) // Make the calculations to be sent to our report.
 {
  char nameAr[N_SIZE];
  int hoursAr[N_SIZE];
  double hrateAr[N_SIZE];

  if (hoursAr[i] > 40) // For employees with overtime hours.
  {
  // double rEarnings, double oEarnings, double tEarnings,
  // double trEarnings, double toEarnings, double ttEarnings;
  // rEarnings = 0, oEarnings = 0, tEarnings = 0,
  // trEarnings = 0, toEarnings = 0, ttEarnings = 0;

   rEarnings = BASE_HOURS * hrateAr[i];
   oEarnings = (hoursAr[i] - BASE_HOURS) * hrateAr[i] * 1.5;
   tEarnings = rEarnings + oEarnings;
   trEarnings += rEarnings;
   toEarnings += oEarnings;
   ttEarnings += tEarnings;
   outFile << left << nameAr[i];
   // << setw(25) << right << rEarnings << setw(25) << right << oEarnings << setw(25) << right << tEarnings << endl;

  } 
  else // For employees without overtime hours.
  {
  // double rEarnings, double oEarnings, double tEarnings,
  // double trEarnings, double toEarnings, double ttEarnings;
  // rEarnings = 0, oEarnings = 0, tEarnings = 0,
  // trEarnings = 0, toEarnings = 0, ttEarnings = 0;

   rEarnings = hoursAr[i] * hrateAr[i];
   oEarnings = 0;
   tEarnings = rEarnings + oEarnings;
   trEarnings += rEarnings;
   toEarnings += oEarnings;
   ttEarnings += tEarnings;
   outFile << left << nameAr[i]; 
   // << setw(25) << right << rEarnings << setw(25) << right << oEarnings << setw(25) << right << tEarnings << endl;
  }
 }

 outFile << endl << endl;

 outFile << setw(33) << trEarnings << " *" << setw(23) << toEarnings << " *" << setw(23) << ttEarnings << " *\n\n";

 outFile << left << "TOTAL EMPLOYEES" << " " << (i - 1);

 inFile.close(); outFile.close();

 return 0;
}

I've included the entire program to give you an idea of where I plan to go with the coding. 我已经包含了整个程序,以使您了解我打算在哪里进行编码。 Thanks in advance for the help! 先谢谢您的帮助!

fstream 's getline function has these signatures. fstreamgetline函数具有这些签名。

std::istream& getline (char* s, streamsize n );
std::istream& getline (char* s, streamsize n, char delim );

In your code... 在您的代码中...

string nameAr[EMP_NUM];
// stuff...
inFile.getline(nameAr[i], EMP_NUM, "*");

You are trying to use a std::string where getline wants a char* - you cannot convert from string to a char* like this. 您尝试在getline需要char*地方使用std::string您不能像这样从string转换为char*

One approach is you can use a character buffer to store the contents of getline , like this. 一种方法是您可以使用字符缓冲区来存储getline的内容,就像这样。

const int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
inFile.getline(buffer, BUFFER_SIZE, '*');
// more stuff...

A better solution 更好的解决方案

I'd recommend using std::getline from <string> , as you can use string types instead of C-strings. 我建议使用<string> std::getline ,因为您可以使用string类型而不是C字符串。

#include <string>

// stuff...
std::getline(inFile, nameAr[i], '*');

edit Links for you since you are learning 编辑链接你,因为你正在学习

It appears that you have a pointer problem. 看来您有指针问题。 You are trying to store a pointer to a char array in nameAr[], correct? 您正在尝试将指向char数组的指针存储在nameAr []中,对吗? What you need to do is define nameAr[] as an array of pointers to char arrays so that you can store a pointer to each name in the nameAr[] array. 您需要做的是将nameAr []定义为指向char数组的指针的数组,以便可以将指向每个名称的指针存储在nameAr []数组中。

Try using this for your name array instead of nameAr[]. 尝试使用此名称数组而不是nameAr []。

nameAr = new char*[EMP_NUM];

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

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