将信息读入结构数组 c++

Reading information into array of structs c++

本文关键字:数组 c++ 结构 信息      更新时间:2023-10-16

我正在为我的 c++ 类制作一个程序。 最终,我希望我的程序对以下格式的联系人文本文件执行快速排序:

名字 第二名 编号

每个联系人由新线分隔。 我首先计算行数并使用动态内存分配来创建与行数大小相同的结构数组。

但是,当我尝试从文本文件中读取信息并将其输出到屏幕上时,我得到的只是胡言乱语。 我在互联网上四处寻找解决方案,但我发现的所有内容似乎都使用了与我不同的语法。

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

#include <iostream>
#include <fstream>
#include <istream>
char in[20];
char out[20];
using namespace std;
struct contact
{
    char firstName[14];
    char surName[14];
    char number[9];
};
//structure definition
int main(void){
    cout << "Please enter the input filename: " << endl;
    cin >> in;
    ifstream input(in);
    if(!input){
        cerr << "failed to open input file " << in << endl;
        exit(1);
    }
    cout << "Please enter tne output filename: " << endl;
    cin >> out;
    // read in the input and output filenames
    char a;
    int b=0;
    while (input.good ())
    {
        a=input.get ();
        if (a=='n')
        {
            b++;
        }
    }
    // count the number of lines in the input file
    input.seekg (0, ios::beg);
    //rewind to beginning of file
    contact* list = new contact[b];
    //dynamically create memory space for array of contacts
    int i = 0.;
      while(input){
            if(i >= b) break;
            if(input >> *list[i].firstName >> *list[i].surName >> *list[i].number) i++;  
            else break;
      }
    input.close();
    //read information from input file into array of contacts
    for(int N = 0; N < b; N++){
        cout << list[N].firstName << list[N].surName << list[N].number << endl;
    }
    ofstream output(out);
    int k = 0;
    for(int k = 0; k<b; k++){
        output << list[k].firstName << "        " << list[k].surName << "       " << list[k].number << endl;
    }
    //print out the unsorted list to screen and write to output file
    //i've done both here just to check, won't print to screen in final version
    output.close();
    delete []list;
}       // end of main()

您将文件位置重置为开头,但从您第一次读取行数时起,文件 eofbit 仍标记为 true。解决此问题的快速方法是在读取行后重新打开文件,可能会使行计数成为清理代码的函数。

int lines(const string path)
{
    ifstream tmp(path.c_str());
    string temp;
    int count = 0;
    getline(inFile,temp);
    while(inFile)
    {
       count++;
       getline(inFile,temp);
    }
    tmp.close();
    return count;
}

好的,我使用更新的C++结构将一个快速而肮脏的方法放在一起,让您大部分时间都在那里。 您可以自己写入文件(琐碎)和快速排序,尽管我已经为您将结构放入向量中,因此对向量进行排序就像编写自定义函数来比较一个结构与另一个结构一样简单。 如果某些代码低于规范C++,我提前道歉。 我已经过了睡觉时间,而且很累,但这很有趣,我想试一试。 祝您编码愉快!

#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <sstream>
using namespace std;
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}
struct contact
{
    std::string firstName;
    std::string surName;
    std::string number;
    contact(std::string& fName, std::string& lName, std::string& num) : firstName(fName), surName(lName), number(num) {}
};
//structure definition
char in[20];
char out[20];
int main()
{
    std::vector<contact> contacts;
    cout << "Please enter the input filename: " << endl;
    cin >> in;
    ifstream input(in);
    if(!input){
        cerr << "failed to open input file " << in << endl;
        exit(1);
    }
    cout << "Please enter tne output filename: " << endl;
    cin >> out;
    std::string sinput;
    // read in the input and output filenames
    while (input.good ())
    {
        getline(input, sinput);
        vector<string> tokens = split(sinput, ' ');
        if (tokens.size() == 3)
        {
            contact c(tokens[0], tokens[1], tokens[2]);
            contacts.push_back(c);
        }
    }
    input.close();
    //read information from input file into array of contacts
    std::cout << "Outputting from vector..." << std::endl;
    for_each(contacts.begin(), contacts.end(), [](contact& c) {
        cout << c.firstName << " " << c.surName << " " << c.number << endl;
    });
    return 0;
}

另外,只是想赞扬拆分方法来自这个网站上的这个答案。 干杯!