在C ++中将输入从文本文件读取到数组 [英] Reading input from text file to array in c++

查看:140
本文介绍了在C ++中将输入从文本文件读取到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,请保持谦虚,因为我是编程的新手.到目前为止,我仅研究过C ++,并且正在将Visual Studio 2010作为编译器运行.对于此程序,我试图从文本输入文件中读取并将信息写入一组三个数组中.一个数组将处理一个名称列表,另外两个数组分别用于工作小时数和小时工资率.我将使用后两者来计算一组收益,并将整个结果输出到另一个文本文件中作为报告.但是,我的问题是获取第一个数组的输入.我正在使用的输入文件的文本排列如下:

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. 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.美国能源部* 35 12.50 J.黎明* 20 10.00 .........

J. Doe* 35 12.50 J. Dawn* 20 10.00 .........

由于我试图使用ifstream getline获取带有星号作为定界符的名称,并将以下两个数字写入其他两个数组中,所以名称以星号结尾.后两个值由空格分隔,因此我认为它们不会引起任何问题.我确定还有其他错误需要处理,但是我需要先解决第一个错误,然后才能开始调试其余的错误.

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. 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.

我在调用inFile.getline的行中发生了问题,因为出现以下错误:错误C2664:'std :: basic_istream< _Elem,_Traits>& std :: basic_istream< _Elem,_Traits> :: getline (_Elem *,std :: streamsize,_Elem)':无法将参数1从'std :: string'转换为'char *'.

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 *'.

根据我在其他地方阅读的内容,(我认为)问题出在试图将字符串写入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. 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.

这是我写的出处:

#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!

推荐答案

fstreamgetline函数具有这些签名.

fstream's getline function has these signatures.

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

在您的代码中...

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

您正在尝试使用std::string,其中getline需要char*-您不能像这样从string转换为char*.

You are trying to use a std::string where getline wants a char* - you cannot convert from string to a char* like this.

一种方法是像这样使用字符缓冲区存储getline的内容.

One approach is you can use a character buffer to store the contents of getline, like this.

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

更好的解决方案

我建议使用<string>中的std::getline,因为您可以使用string类型而不是C字符串.

I'd recommend using std::getline from <string>, as you can use string types instead of C-strings.

#include <string>

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

自学习以来为您编辑链接

  • std::getline function example
  • std::getline function description

这篇关于在C ++中将输入从文本文件读取到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆