【问题标题】:Reading A .txt file into an array [duplicate]将 .txt 文件读入数组 [重复]
【发布时间】:2026-01-28 08:30:01
【问题描述】:

可能重复:
C++ match string in file and get line number

我被分配了一个硬件任务,所以我不是要求一整套代码,但也许是一些关于我应该做什么的提示。任务如下: 包含文本文件 babynames2004.txt。包含美国最受欢迎的 1000 个名字的列表。 这是一个以空格分隔的文件,包含 1000 个条目,其中排名在前,然后是相应的男孩名和女孩名。最受欢迎的名字排在最前面,最不受欢迎的名字排在最后。编写一个允许用户输入名称的程序。然后程序应该从文件中读取并在女孩和男孩中搜索匹配的名字,然后将这些信息存储在一个数组中。如果找到匹配项,它应该输出名称的排名。该程序还应指出是否不匹配。

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
//function prototypes
void input(int search(const int name[][Max_Girl_Name], int, int, int );
void display(


int main()
{
  const int Max_Girl_Name = 1000, Max_Boy_Name = 1000;     //array sizes
  int name[][Max_Girl_Name], count(0);

  ifstream inputfile; //open stored file
  inputFile.open("c:/temp/babynames2004.txt")
  if (inputFile.fail())
  {
     cout<<"Error opening File\n";
  }
  else 
  {
     while(!inputFile.eof())
     inputFile>>name[count];
     count++
  }

  input(
  searchname = (
  display(

  inputFile.close();

  cout << count " is ranked " << Rank <<  

}

system("PAUSE");
return 0;
}

int search(const int name[][Max_Girl_Name], int name_input, int targetname, int size_Dimension_1)
{
  for(int i = 0;i<size_Dimension_1;i++)
  {
    int index = 0;
    bool found = false;
    while ((!found) && (index < number_used))
      if (target == name[index])
         found = true
      else 
           index++;

      if(found)
               return index;
      else -1;
    }
}

我也有几个想法:

  1. 是否可以使用多维数组来解决这个问题,例如names[boy_name][girl_name]
  2. 这个程序的函数是search(value)input(void)output(void)吗?
  3. 您将如何遍历数组的两个索引?

我希望我足够清楚。

【问题讨论】:

  • @irrelephant 不。甚至没有关闭
  • 不,这只是我已经走了多远。我无法确定该程序将使用哪些类型的函数。
  • 语法错误很多。如果您还没有这样做,我建议您使用像 Visual Studio Express 这样的免费 IDE。
  • 你熟悉STL吗?关联容器映射对您有意义吗?文件 i/o 可以通过多种方式完成,因为 C++ 是面向对象的语言,您是否考虑过对象 i/o?(例如,使用 iostream_iterator)。知道您需要通过作业确认哪些知识也很有趣。
  • 如果你看这些家庭作业的问题足够长,你就会记住它们......如果我是一名老师,我至少会在每个学期把练习混在一起。 “1000 个婴儿名字 C++”会在 SO 搜索中找到。 C++ match string in file and get line number

标签: c++ arrays multidimensional-array


【解决方案1】:

根据您的知识水平,您被困在数组中。你应该有 3 个一维数组,就像你已经定义的那样。 (在实际程序中,您将定义一个包含男孩名、女孩名和排名这三个项目的类)

1.应该是这样的:

int maxLines=1000;
int maNameLength=100; // This would go away later coz you would use std::string when you learnt them
char boysName[maxLines][maxNameLength];
char girlsName[maxLines][maxNameLength];
int rank[maxLines];

(这里是你的“多维数组”,那是女孩和男孩的名字)

2.你是对的,3 个这样的功能就可以了。输入功能应该例如逐行读取文件,将其拆分为三个字段并将它们存储在这些数组中。这可以通过多种方式完成,使用 std:: 库中的高级构造,或者作为初学者,您可能想手动完成。 (注意文件的行数超过数组大小,或者名称长于 maxNameLength)。搜索功能应该通过您的数组并检查名称是否相等,如果找到则返回数组索引,否则例如-1

然后输出函数获取这个数组索引,首先检查其是否有效(>=0 和 lt maxLines),然后输出排名或未找到)。

不要沮丧,作为初学者,要启动并运行这个程序,您还有很多工作要做......

【讨论】:

  • 感谢您的反馈帮助很大!
【解决方案2】:

在一些提示之前,只是对如何处理事情的建议。在编码成为您的第二天性之前,您应该花时间在伪代码中写出您希望最终代码执行的操作。然后将这些行更改为 cmets 并开始充实您需要满足要求的变量和循环等。通过这样做 (a) 你会变得更有条理,并且 (b) 它可以让你记录你的方法。

//The text file babynames2004.txt is included. 

//Contains a list of the 1000 most popular names in the US. It is a space-delimited file of //1000 entries in which the rank is listed first, followed by the corresponding boy name //and girl name. 
const int Max_Name = 1000

//The most popular names are listed first and the least popular names are listed last. 
int rankArray[Max_Name];//a given index in all three corespondss to the row in the file
char boyArray[Max_Name][SomeConstantMaxLengthValue];
char girlArray[Max_Name][SomeConstantMaxLengthValue];

//write a program that allows the user to input a name. 
int main(unsigned int argc, const char** argv )
{
    std::cin >> searchName;

    //The program should then read from the file and search for a matching name among the   
    //girls and boys then store this information in an array. If a match is found, it 
    //should output the rank of the name. The program should also indicate if there is no 
    //match.
    if ( readNameFromFile( searchName , rank, girlName, boyName ) )
    {
        Store( rank,girlName,boyName );
        std::cout << .... //details retireved...
    }
    else
    {
         //Not found - do something helpful
    }
}

readFromFileStore 是完成这项工作的部分,请考虑通过引用传递参数(google/c++ 教科书)以便您可以操纵它们在你的功能中。 请参阅有关读取文件的 Ians 提示。

您的问题似乎只希望您存储用户选择的那些,否则它希望您搜索文件并选择正确的行。确实,阅读所有内容然后存储更有效,但这是您的决定。如果您确实阅读了所有内容,那么您只需要遍历 2 个名称数组,直到名称匹配。然后从 rank、girl 和 boy 数组中提取详细信息... hint strncmp(...) 或者如果您可以使用 std::string 作为名称,例如

std::string boyNames[Max_Name];
std::string girlNames[Max_Name];

这可能更容易,但我猜你需要使用char

需要谨慎选择实际的数组,因为名称是任意长度,因此您可能需要考虑如何记录详细信息 - 如果它将是 char 数组,那么您需要在存储等时确保长度.... 看看你的进展如何,如果有任何问题,请回来。

【讨论】:

    【解决方案3】:

    提示

    ifstream 有一个名为 getline 的方法,可让您指定所需的分隔符。

    由于男孩名和女孩名彼此之间没有任何特定关系,您可以将它们存储在完全独立的列表中,但是如果一个名字出现在两个列表中会怎样? p>

    每个名称都有一个等级,因此信息确实需要以这样一种方式存储,即 2 条数据可以相互关联。

    【讨论】:

    • 在这种情况下,您将如何计算名称的排名?
    • 从问题“排名第一,然后是对应的男孩名和女孩名”。如果您将名称与排名一起存储,或者以表明排名的方式存储名称(我尽量不泄露它),那么您可以相当容易地获得它