0

TDM GCC 4.9.2 not functioning properly on Windows 10 64-bit Microsoft C++ Compiler (MSVS) runs smoothly

I tried to use file handling in a c++ source file. The file's purpose was to read a .txt file called ClientList.txt and write the contents to a new file called ClientListv2.txt while modifying the pre-existing data.

Here's the contents of the .txt file 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  

The task was to create a duplicate file called ClientListv2.txt which contains all the pre-existing information, with two addional records, as well as a change to the phone number and address of any one single line. (record)

Here's my attempt to do the process in C++:

//I started out by included my header files:

#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;
}

Brief Summary

After doing all of this, I've noticed that first, my code only works when I have a ClientListv2.txt manually created in my present working directory. If not, the code doens't generate a new .txt file itself. Secondly, using a TDM GCC 4.9.2 Compiler, I get pretty strange pattern-based sequences of outputs when examing my phone number and address accross multiple lines. It appears, that the code overwrites some of the old text and sometimes it overwrites none of it.

For some reason, when I tried compiling and running it on Visual Studio (Microsoft C++ Compiler MSVC), I got the right results. Why is TDM GCC 4.9.2 acting like that? My machine runs Windows 10 64-bit. I suppose that the '\r\n' might have something responsible for this strange output. Who knows?

I used the following dummy text to input the values for two additional rows:

Sam
Billy
Jhons
299-292-9292
sambilly@gmail.com
39
2nd Street
Ventnor
NJ
n. 'pronouns' m.
  • 95,181
  • 13
  • 111
  • 206
  • First, the tag compiler-construction is for questions about writing a compiler, not using a compiler. Second, "I suppose that the '\r\n' might have something responsible for this strange output" So why not try a file without `\r\n`? Third, instead of talking about the strange output you are getting, just show it. It is text so paste it to the question. – n. 'pronouns' m. May 24 '21 at 09:26
  • Finally, `while (file.good())` is not a good idea, for the same reason `while (file.eof())` [is not a good idea](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – n. 'pronouns' m. May 24 '21 at 09:38

0 Answers0