从txt文件中分离并行数组

时间:2015-04-19 23:15:21

标签: c++ arrays file-io

我是一名非常新手的程序员,我正在尝试制作一个程序,该程序读取包含5名学生(仅限名字)姓名的txt文件以及每位学生的4个考试成绩。我尝试将名称读入名为students的数组中,然后将分数读入4个名为test1,test2,test3,test4的独立数组中,然后从监视器中显示。该文件如下所示:

  史蒂夫78 65 82 73

     

Shawn 87 90 79 82

     

Annie 92 90 89 96

     

Carol 72 65 65 60

     

凯西34 50 45 20

我很难分解阵列并组织它们。有人能帮我吗?请记住我非常新手,所以我不太了解编程。

到目前为止,这是我的代码:

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>
#define over2 "\t\t"
#define over3 "\t\t\t"
#define over4 "\t\t\t\t"
#define down5 "\n\n\n\n\n"


using namespace std;



int main(int argc, char *argv[])
{


    ifstream inputFile;


    //Constant for max string size
    const int SIZE = 5;


    //Constant for the scores
    string names[SIZE], fileName, line, test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];


    //input section, user enters their file name
    cout << down5 << down5 << over2 << "Please enter your file name: ";
    cin >> fileName;
    system("CLS");




    //open the file containing the responses
    inputFile.open(fileName.c_str());
     cout << endl;

    //kicks you out if file isn't found
    if (inputFile)
    {
        for(int i = 0; i < SIZE; i++)
        {  
           getline(inputFile, line);
            names[i] = line;
            getline(inputFile, line);
            test1[i] = line;
            getline(inputFile, line);
            test2[i] = line;
            getline(inputFile, line);
            test3[i] = line;
            getline(inputFile, line);
            test4[i] = line;
        }   
            inputFile.close();  
    }
    cout << down5 << over3 << "Student\tTest1\tTest2\tTest3\tTest4\n";
    cout << over3 << "-------\t-----\t-----\t-----\t-----\n";   
    for(int i = 0; i < SIZE; i++)
    {
        cout << over3 << names[i] << endl;
        cout << over3 << test1[i] << endl;
        cout << over3 << test2[i] << endl;
        cout << over3 << test3[i] << endl;
        cout << over3 << test4[i] << endl;

    }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

让我们看看你想要阅读的文件的结构:

Steve 78 65 82 73
Shawn 87 90 79 82
Annie 92 90 89 96
Carol 72 65 65 60
Kathy 34 50 45 20

数据的格式可以描述如下:

  • 每一行代表一条“记录”。
  • 每个“记录”包含多个列。
  • 列由空格分隔。

您目前正在为每个使用getline()

for(int i = 0; i < SIZE; i++)
{  
  getline(inputFile, line);
  names[i] = line;
  getline(inputFile, line);
  test1[i] = line;
  getline(inputFile, line);
  test2[i] = line;
  getline(inputFile, line);
  test3[i] = line;
  getline(inputFile, line);
  test4[i] = line;
}   

...而您实际上想要为每个记录单行阅读并将其拆分:

for (int i = 0; i < SIZE; i++)
{
  string line;
  size_t start = 0;
  // For each line, attempt to read 5 columns:
  getline(inputFile, line);
  names[i] = get_column(line, start);
  test1[i] = get_column(line, start);
  test2[i] = get_column(line, start);
  test3[i] = get_column(line, start);
  test4[i] = get_column(line, start);
}

以下是原始代码的修改版本,如上所述将每行分开:

#include <cctype>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

static string get_column(string line, size_t &pos)
{
  size_t len = 0;
  // Skip any leading whitespace characters.
  while (isspace(line.c_str()[pos])) { ++pos; }
  // Count the number of non-whitespace characters that follow.
  while (!isspace(line.c_str()[pos+len]) && line.c_str()[pos+len]) { ++len; }
  // Extract those characters as a new string.
  string result = line.substr(pos, len);
  // Update the "start" position (for the next time this function is called).
  pos += len;
  // Return the string.
  return result;
}

int main()
{

  const int SIZE = 5;
  string names[SIZE], test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];

  // Ask the user to enter a file name.
  cout << "Please enter your file name: ";
  string fileName;
  cin >> fileName;

  // Open the file and read the data.
  ifstream inputFile(fileName.c_str());
  if (!inputFile.is_open()) { return 0; }

  for (int i = 0; i < SIZE; i++)
  {
    string line;
    size_t start = 0;
    // For each line, attempt to read 5 columns:
    getline(inputFile, line);
    names[i] = get_column(line, start);
    test1[i] = get_column(line, start);
    test2[i] = get_column(line, start);
    test3[i] = get_column(line, start);
    test4[i] = get_column(line, start);
  }

  inputFile.close();

  // Display the data.
  cout << "Student\tTest1\tTest2\tTest3\tTest4" << endl;
  cout << "-------\t-----\t-----\t-----\t-----" << endl;

  for(int i = 0; i < SIZE; i++)
  {
    cout << names[i] << "\t";
    cout << test1[i] << "\t";
    cout << test2[i] << "\t";
    cout << test3[i] << "\t";
    cout << test4[i] << endl;
  }

}

运行程序会产生以下输出:

Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve   78    65    82    73
Shawn   87    90    79    82
Annie   92    90    89    96
Carol   72    65    65    60
Kathy   34    50    45    20

请注意,get_column()函数处理多个空格或太短的行,以便此文件:

Steve 78     65 82 73
Shawn   87 90 
Annie 92 90 89 96
Carol 72 
Kathy 34 50   45 20

...产生以下输出:

Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve   78    65    82    73
Shawn   87    90
Annie   92    90    89    96
Carol   72
Kathy   34    50    45    20

答案 1 :(得分:0)

之前的回答是过度思考问题。

您可以使用输入文件流来检索“格式化输入”(也就是说,您知道输入的格式为'string',然后是'int','int','int','int')使用&gt;&gt;操作

string name;

int score[4];

ifstream mf;

mf.open("score.txt");

// Get name string.
mf >> name;

// Get four score values into an array.
mf >> score[0] >> score[1] >> score[2] >> score[3];

然后:

cout << name;

cout << score[0];
cout << score[1];
cout << score[2];
cout << score[3];