C++ 如何使此代码使用char而不是string?

C++ 如何使此代码使用char而不是string?,c++,C++,我正在尝试完成一项任务,但我必须在不使用字符串的情况下完成它。我的目标是读取一个文本文件,找到包含相同CD名称但不同操作(a或B)的行,然后将输出写入另一个文件。例如: 输入文件: 10 B 01010112345 ABBA 15 A 02020213456 U2 16 A 03030314567 4you 17 B 02020213456 ABBA 20 B 04040415678 4you 127 B 04040415678 HipHop2014 1234 A 05050516789 ABB

我正在尝试完成一项任务,但我必须在不使用字符串的情况下完成它。我的目标是读取一个文本文件,找到包含相同CD名称但不同操作(a或B)的行,然后将输出写入另一个文件。例如:

输入文件:

10 B 01010112345 ABBA
15 A 02020213456 U2
16 A 03030314567 4you
17 B 02020213456 ABBA
20 B 04040415678 4you
127 B 04040415678 HipHop2014
1234 A 05050516789 ABBA
输出文件:

20 03030314567 04040415678
1234 05050516789 01010112345
这是我的代码:

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

const int SIZE = 250;
struct CD {
    int time;
    char action;
//    char person_code[12];
//    char cd[50];
    string person_code;
    string cd;
    bool removed = false;
};

int main(){
    fstream f("exchange.in");
    ofstream fout("exchange.out");
    CD m[SIZE];
    CD x;
    int it = 0, counter = 0;

    while(f>>x.time>>x.action>>x.person_code>>x.cd && it<SIZE){
        cout<<"Time: "<<x.time<<" Action: "<<x.action<<" Person code: "<<x.person_code<<" CD: "<<x.cd<<endl;
        m[it].time = x.time;
        m[it].action = x.action;
//        memcpy(&m[it].person_code, &x.person_code, sizeof(CD));
//        memcpy(&m[it].cd, &x.cd, sizeof(CD));
        m[it].person_code = x.person_code;
        m[it].cd = x.cd;
        it++;
    }
    for(int i=0; i<it; i++){
        for(int j=0; j<it; j++){
            if(!m[i].removed && !m[j].removed && m[i].cd == m[j].cd && m[i].action == 'A' && m[j].action == 'B'){
                int temp;
                if(m[i].time > m[j].time) {
                    temp = m[i].time;
                }
                else{
                    temp = m[j].time;
                }
                cout<<"Time: "<<temp<<" PC1: "<<m[i].person_code<<" PC2: "<<m[j].person_code<<endl;
                fout<<temp<<' '<<m[i].person_code<<' '<<m[j].person_code<<endl;
                counter++;
                m[i].removed = true;
                m[j].removed = true;
            }
        }
    }

    if(counter == 0) fout<<0<<"\n";

    f.close();
    fout.close();
}
#包括
#包括
#包括
使用名称空间std;
常数int SIZE=250;
结构光盘{
整数时间;
炭作用;
//字符person_代码[12];
//char-cd[50];
字符串person_码;
字符串cd;
bool-removed=false;
};
int main(){
FSF(“交易所”);
流式输出(“交换输出”);
cdm[尺寸];
cdx;
int it=0,计数器=0;
而(f>>x.time>>x.action>>x.person\u code>>x.cd&&it此代码:

 m[i].cd == m[j].cd
cd
std::string

cd
是数组时,将检查值是否具有相同的地址。您正在比较指针


查看您是否不确定如何使用C样式字符串。

什么是a?说明什么是错误的?编译过程中的输出或错误方面,您得到了什么?请将和作为一般参考阅读。您应该检查代码审阅,您将从中得到答案谢谢!这非常有用!