GCC 编译器与 Mircrosoft C++ 编译器(MSVC)问题

时间:2021-05-24 08:59:29

标签: gcc visual-c++

TDM GCC 4.9.2 在 Windows 10 64 位上无法正常运行 Microsoft C++ Compiler (MSVS) 运行流畅

我尝试在 C++ 源文件中使用文件处理。 该文件的目的是读取名为 ClientList.txt 的 .txt 文件 并写下内容 到一个名为 ClientListv2.txt 的新文件,同时修改预先存在的数据。

这是 .txt 文件 ClientList.txt 的内容:

George A Harrison     713-555-1234   gaharrison@gmail.com   250 N Main Street,   Baytown, TX       
James K Smith         713-555-1235   jksmith@gmail.com      100 Cactus St,        Baytown, TX  
Alma P Sanchez        713-554-1237   apsanchez@gmail.com    312 Luella Blvd,     Pasadena, TX       
Samantha J Jones      713-554-1238   sjjones@gmail.com      125 Purdue Ln,        Pasadena, TX       
Paula Mary Henry      713-553-1239   pmhenry@gmail.com      412 Colorado Ave,     League City, TX       
Henry B Albertson     713-553-2345   hbalbertson@gmail.com  724 Perkins Ave,      League City, TX           
Samuel * Harrison     713-552-2346   sharrison@gmail.com    200 Wesley Ln,        Deer Park, TX           
Peter N Smith         713-552-2347   pnsmith@gmail.com      157 Briarwood Ct,     Deer Park, TX           
James Edward Bennett  713-551-2348   jebennett@gmail.com    330 S 6th St,         La Porte, TX           
Javier D Rodriquez    713-551-2349   jdrodriquez@gmail.com  245 Parkcrest Dr,     La Porte, TX  

任务是创建一个名为 ClientListv2.txt 的重复文件,其中包含所有预先存在的 信息,带有两条附加记录,以及任何一条线路的电话号码和地址的更改。 (记录)

这是我尝试用 C++ 完成这个过程:

//我从包含头文件开始:

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

using namespace std;

int main()
{
    //reading the file
    fstream file;
    file.open("ClientList.txt", ios::in | ios::out);
    if (!file.is_open())
    {
        cout << "File does not exist" << endl;
        exit(0);
    }

    char FirstName[10];
    char MiddleName[10];
    char LastName[15];
    char Phone[15];  //one extra char for '\0' (string terminal char)
    char Email[31];
    char Address_num[8];
    char Address_street[25];
    char Town[25];
    char State[8];

    while (file.good())
    {
        file.getline(FirstName, 9, ' ');
        file.getline(MiddleName, 9, ' ');
        file.getline(LastName, 14, ' ');
        file >> ws;
        file.getline(Phone, 14, ' ');
        file >> ws;
        file.getline(Email, 30, ' ');
        file >> ws;
        file.getline(Address_num, 7, ' ');
        file >> ws;
        file.getline(Address_street, 24, ',');
        file >> ws;
        file.getline(Town, 24, ',');
        file >> ws;
        file.getline(State, 7);
        file.clear();
        file >> ws;
        cout << FirstName << " " << MiddleName << " " << LastName << " " << Phone << " " << Email << " " << Address_num << " " << Address_street << " " << Town << " " << State << endl;

    }

    //To modify one phone and one street address of any single record(line), I used this approach...

    fstream new_file;
    new_file.open("ClientListv2.txt", ios::out | ios::in);

    //file.clear();
    file.seekg(0, ios::beg);
    file.seekp(0, ios::beg);
    char c;
    while (file.good())
    {
        file.get(c);
        new_file << c;
    }

    //declaring a string to take a whole line
    string line;
    int line_num;
    //go to the beginning of the new_file
    new_file.seekg(0, ios::beg);
    new_file.seekp(0, ios::beg);

    cout << "Enter Line No to Edit Phone Number (First Line is 1): " << endl;
    cin >> line_num;

    for (int i = 1; i < line_num; i++)
    {
        getline(new_file, line);
    }
    new_file.seekp(22, ios::cur);
    new_file << "888-888-8888";
    
    cout << new_file.tellp() << endl;


    cout << new_file.tellg() << endl;  //34
    cout << new_file.tellp() << endl;

    new_file.seekg(0, ios::beg);  //moving the flag pointers to the beginning of the file
    new_file.seekp(0, ios::beg);

    cout << "Enter Line No to Edit Street Address (First Line is 1): " << endl;
    cin >> line_num;

    for (int i = 1; i < line_num; i++)
    {
        getline(new_file, line);
    }

    //65 street address
    new_file.seekp(64, ios::cur);
    

    new_file << "B Tane Stripe";   //any address you want to add 

    cout << new_file.tellg() << endl; //78
    cout << new_file.tellp() << endl;

    //move the file markers to the end of the file
    new_file.seekg(0, ios::end);
    new_file.seekp(0, ios::end);

    cout << "\t\t\tUpdating Client List Text File\t\t\t" << endl;

    //two add two addional clients
    cout << "\nEnter the first addional record:\n";
    cout << "Enter the FirstName" << endl;
    cout << "Enter the MiddleName" << endl;
    cout << "Enter the LastName" << endl;
    cout << "Enter the Phone" << endl;
    cout << "Enter the Email" << endl;
    cout << "Enter the Address_num" << endl;
    cout << "Enter the Address_street" << endl;
    cout << "Enter the Town" << endl;
    cout << "Enter the State" << endl;
    cin >> FirstName;
    cin >> MiddleName;
    cin >> LastName;
    cin >> Phone;
    cin >> Email;
    cin >> Address_num;
    cin.ignore();
    cin.getline(Address_street, 24);
    cin.getline(Town, 24);
    cin >> State;

    new_file.clear();
    new_file << "\n" << FirstName << " " << MiddleName << " " << LastName << " " << Phone << " " << Email << " " << Address_num << " " << Address_street << " " << Town << " " << State << endl;

    cout << "\nEnter the second addional record:\n";
    cout << "Enter the FirstName" << endl;
    cout << "Enter the MiddleName" << endl;
    cout << "Enter the LastName" << endl;
    cout << "Enter the Phone" << endl;
    cout << "Enter the Email" << endl;
    cout << "Enter the Address" << endl;
    cout << "Enter the Town" << endl;
    cout << "Enter the State" << endl;
    cin >> FirstName;
    cin >> MiddleName;
    cin >> LastName;
    cin >> Phone;
    cin >> Email;
    cin >> Address_num;
    cin.ignore();
    cin.getline(Address_street, 24);
    cin.getline(Town, 24);
    cin >> State;

    new_file << FirstName << " " << MiddleName << " " << LastName << " " << Phone << " " << Email << " " << Address_num << " " << Address_street << " " << Town << " " << State << endl;

    file.close();
    new_file.close();
    return 0;
}

简要总结

在完成所有这些之后,我首先注意到,我的代码仅在我手动创建 ClientListv2.txt 时才有效。 当前工作目录。如果没有,代码本身不会生成新的 .txt 文件。其次,使用 TDM GCC 4.9.2 编译器,我得到了非常奇怪的基于模式的 跨多行检查我的电话号码和地址时的输出序列。 看起来,代码覆盖了一些旧文本,有时它没有覆盖。

出于某种原因,当我尝试在 Visual Studio(Microsoft C++ 编译器 MSVC)上编译和运行它时,我得到了正确的结果。 为什么 TDM GCC 4.9.2 会这样? 我的机器运行 Windows 10 64 位。我想 '\r\n' 可能对这个奇怪的输出负责。谁知道?

我使用以下虚拟文本来输入另外两行的值:

Sam
Billy
Jhons
299-292-9292
sambilly@gmail.com
39
2nd Street
Ventnor
NJ

0 个答案:

没有答案