Results 1 to 10 of 10

Thread: Insert text in file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Insert text in file

    I got an problem i cannot make my code after all checks are passed = OK to insert into MyFile.txt the text that is inputed.

    Here is my code of function:
    Code:
    case 444: //Buy VIP
    					{
    						if(s_Vip.VipEnable == FALSE)
    						{
    							GCServerMsgStringSend("[VIP SYSTEM] System is Disabled", lpObj->m_Index, 1);
    							return 0;
    						}
    
    						int Group = -1;
    						int Time = 0;
    						int SecondTime = 0;
    
    						Group = this->GetTokenNumber(); //GetGroup
    						Time = this->GetTokenNumber(); //GetTime
    
    						if ( Group == NULL || Group < 0 || Group > 3)
    						{
    							GCServerMsgStringSend("[VIP SYSTEM] Wrong Group Number", lpObj->m_Index, 1);
    							return 0;
    						}
    
    						if (Time > 0)
    						{
    							SecondTime = Time*60*60;
    						}
    						else
    						{
    							MsgOutput(lpObj->m_Index, lMsgx.Get((384)));
    							return 0;
    						}
    
    						int GetCredits = SQL.GetPlayerCredits(lpObj->AccountID);
    						int GetCsPoints = SQL.GetPlayerCsPoints(lpObj->AccountID);
    
    						if(lpObj->Resets < ReadConfig.Vip_Buy_Resets)
    						{
    							MsgOutput(lpObj->m_Index, lMsgx.Get((385)));
    							return 0;
    						}
    
    						if(GetCredits < ReadConfig.Vip_Buy_Credits)
    						{
    							MsgOutput(lpObj->m_Index, lMsgx.Get((386)));
    							return 0;
    						}
    
    						if(GetCsPoints < ReadConfig.Vip_Buy_CsPoints)
    						{
    							MsgOutput(lpObj->m_Index, lMsgx.Get((387)));
    							return 0;
    						}
    
    						SQL.UpdatePlayerCredits(lpObj->AccountID,GetCredits-ReadConfig.Vip_Buy_Credits);
    						SQL.UpdatePlayerCsPoints(lpObj->AccountID,GetCsPoints-ReadConfig.Vip_Buy_CsPoints);
    						lpObj->VipGroup = Group;
    						lpObj->VipTick = SecondTime;
    						lpObj->Resets -= ReadConfig.Vip_Buy_Resets;
    						MsgOutput(lpObj->m_Index, lMsgx.Get((388)), s_Vip.GetGroupName(Group));
    						MsgOutput(lpObj->m_Index, lMsgx.Get((389)), Time*60);
    						MsgOutput(lpObj->m_Index, lMsgx.Get((390)));
    //From here starts insert into file text call
    //My file name path is "..\\Data\\connectmember.txt"
    //Each time when this function is called after every text that is saved must save it on new line
    // Example:
    // test
    // test123
    // Everytime on new line to save it
    // What it needs to write to file is only this call "lpObj->AccountID" (this calls the name of the account that will insert into the file)
    Here goes the code for save
    						// End of save to file code
    						return 1;
    					}break;
    I had try with:
    Code:
    std::vector<std::string> example;
    						example.push_back(lpObj->AccountID);
    
    						std::ofstream output_file("..\\Data\\connectmember.txt");
    						std::ostream_iterator<std::string> output_iterator(output_file, "\n");
    						std::copy(example.begin(), example.end(), output_iterator);
    but i receive this:
    Code:
    warning C4509: nonstandard extension used: 'CGMMng::ManagementProc' uses SEH and 'example' has destructor
    Because my function has an __try method if there is problem will show error where what line and code fails. So i cannot use std default language code need something different if someone can help me out.

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Insert text in file

    Code:
    warning C4509: nonstandard extension used: 'CGMMng::ManagementProc' uses SEH and 'example' has destructor
    For MS VS, how has 'C++ Exceptions' in Project Properties/Code Generation been set?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Insert text in file

    Quote Originally Posted by 2kaud View Post
    Code:
    warning C4509: nonstandard extension used: 'CGMMng::ManagementProc' uses SEH and 'example' has destructor
    For MS VS, how has 'C++ Exceptions' in Project Properties/Code Generation been set?
    Name:  Untitled.jpg
Views: 313
Size:  30.4 KB

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Insert text in file

    As the code uses SEH, shouldn't that option be set to Yes With SEH exceptions??
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Insert text in file

    std::vector<std::string> example;
    example.push_back(lpObj->AccountID);

    std:fstream output_file("..\\Data\\connectmember.txt");
    std:stream_iterator<std::string> output_iterator(output_file, "\n");
    std::copy(example.begin(), example.end(), output_iterator);

    how to make this code to insert the new text on new line, because it doesnt add on each new line it just replace on first line all time the text

    need to be:
    - test
    - test2
    - test3
    everytime on new line without to delete the previous.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Insert text in file

    also i made an simply check but it does not read the name from the lines correct
    example:
    - test
    - test2

    if i use the command and my name is "test2" it shows me your account already exists but still continued with the procedure, but it should it should stop if the account name already exists

    Code:
    bool CheckVip(int aIndex)
    {
    	LPOBJ lpObj = &gObj[aIndex];
    	string name = lpObj->AccountID;
    	string line = "";
    	fstream f;
    	f.open("..\\Data\\connectmember.txt");
    
    	if (f.is_open())
    	{
    		while (f.good() )
    		{
    			getline(f,line);
    			if(line==name)
    			{
    				MsgOutput(lpObj->m_Index, "Your Account already exists");
    				return 0;
    			}
    		}
    		f.close();
    	}
    	else 
    	{
    		MsgBox("Unable to open file connectmember.txt");
    		return 0;
    	}
    
    	getch();
    	return 1;
    }

  7. #7
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Insert text in file

    This writes the contents of the vector to the file - one string per line.
    Code:
    #include <vector>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	vector<string> example {"test", "test2", "test3"};
    
    	ofstream output_file("test3.txt");
    	ostream_iterator<string> output_iterator(output_file, "\n");
    	copy(example.begin(), example.end(), output_iterator);
    }
    Any data in the file will first be deleted as using ofstream. Do you mean that you want to append to existing data in the file? Then consider
    Code:
    #include <vector>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	vector<string> example {"test", "test2", "test3"};
    
    	fstream of("test3.txt");
    
    	if (!of.is_open()) {
    		of.open("test3.txt", ios_base::out);
    	}
    
    	of.seekp(0, ios::end);
    	ostream_iterator<string> oi(of, "\n");
    	copy(example.begin(), example.end(), oi);
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Insert text in file

    To test if a name exists as a line in a file, consider
    Code:
    #include <vector>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const string name = "test2";
    
    	ifstream ifs("test3.txt");
    
    	if (!ifs.is_open()) {
    		cout << "Cannot open file test3.txt" << endl;
    		return 0;
    	}
    
    	string line;
    
    	while (getline(ifs, line)) {
    		if (line == name) {
    			cout << "The name " << name << " already exists" << endl;
    			return 0;
    		}
    	}
    
    	return 1;
    }
    Note that in the example posted in post #6, if the line exists then the file is not closed.
    Code:
    MsgOutput(lpObj->m_Index, "Your Account already exists");
    f.close();
    return 0;
    Last edited by 2kaud; May 20th, 2017 at 08:05 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Insert text in file

    Also consider
    Code:
    #include <vector>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    const string tnam = "test3.txt";
    
    bool notexists(const string& name)
    {
    	ifstream ifs(tnam);
    
    	if (!ifs.is_open()) {
    		cout << "Cannot open file " << tnam << endl;
    		return true;
    	}
    
    	string line;
    
    	while (getline(ifs, line)) {
    		if (line == name) {
    			cout << "The name " << name << " already exists" << endl;
    			ifs.close();
    			return false;
    		}
    	}
    
    	ifs.close();
    
    	return true;
    }
    
    bool add(const string& name)
    {
    	if (notexists(name)) {
    		fstream of(tnam);
    
    		if (!of.is_open()) {
    			of.open(tnam, ios_base::out);
    		}
    
    		of.seekp(0, ios::end);
    		of << name << endl;
    		of.close();
    		return true;
    	}
    
    	return false;
    }
    
    int main()
    {
    	vector<string> example {"test2", "test22", "test23"};
    	
    	for (const auto& v : example) {
    		if (add(v))
    			cout << v << " added OK" << endl;
    	}
    }
    Which only adds lines to the file if they don't already exist. Note that this is not efficient as the file is opened and closed multiple times.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Insert text in file

    i had maded it currectly now the way i wanted it:
    Code:
    if(CheckVip(aIndex) == FALSE)
    						{
    							
    							return 0;
    							
    						}
    
    std::vector<std::string> example;
    						example.push_back(lpObj->AccountID);
    
    						std::fstream of("..\\Data\\connectmember.txt");
    
    						if (!of.is_open()) 
    						{
    							of.open("..\\Data\\connectmember.txt", std::ios_base::out);
    						}
    
    						of.seekp(0, std::ios::end);
    						std::ostream_iterator<std::string> oi(of, "\n");
    						copy(example.begin(), example.end(), oi);
    works fine, if username exists on file connectmember.txt return 0; and shows message already exist - perfect
    if not username exists it add it and continue with function - perfect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width