writing in C++ using stdio.h

0x32789

Expert
Joined
May 26, 2014
Messages
849
Reaction score
51
Location
LongForgotten <-> 0x32789
Hi guys I am trying to make a reading and writing function for my hack so it can read values which were set old time and there is no need to set it again and again
But I have a problem when I try to write something in a file it freezes my game.
and also when my script tries to read, it dosent read
Help me I am not good in File I/O functions. (or probably cpp)

this is one of the commands and all of them crash:
Code:
        if(fexists(WIDTH_FILE))
	{
		DeleteFile((LPCSTR)*WIDTH_FILE);
	}
	CreateFile((LPCSTR)*WIDTH_FILE,GENERIC_READ,FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,NULL);
	FILE *file;
	file = fopen(WIDTH_FILE, "w");
	if (!file) return;
	fprintf(file,"%d", felix);
	fclose(file);
and my fexists(its my own)
Code:
bool fexists(const char *_filename)
{
	FILE *temp_chck_99482;

	temp_chck_99482 = fopen(_filename, "w");
	if (!temp_chck_99482) return false;
	else if (temp_chck_99482) return true;
	return false;
}

read function
Code:
FILE *file;
    	char read[1000];
		if(fexists(SPEED_FILE))
		{
    		file = fopen(SPEED_FILE,"r");
    		if (!file)
			{
				AddMessageToSAWindow( D3DCOLOR_XRGB( 0, 0xFF, 0 ), "Failed to read Speed Value from Configuration file.");
			}
			else
			{
    			while (fgets(read,1000, file) != NULL)
				{
					std::string fs(read);			
					MOD_SPEED = std::stof(fs);
				}
			}
			fclose(file);
		}
		if(fexists(HEIGHT_FILE))
		{
    		file = fopen(HEIGHT_FILE,"r");
    		if (!file)
			{
				AddMessageToSAWindow( D3DCOLOR_XRGB( 0, 0xFF, 0 ), "Failed to read Screen Height Value from Configuration file.");
			}
			else
			{
    			while (fgets(read,1000, file) != NULL)
				{	
					x = (int)read;
				}
			}
			fclose(file);
		}
		if(fexists(WIDTH_FILE))
		{
    		file = fopen(WIDTH_FILE,"r");
    		if (!file)
			{
				AddMessageToSAWindow( D3DCOLOR_XRGB( 0, 0xFF, 0 ), "Failed to read Screen Width Value from Configuration file.");
			}
			else
			{
    			while (fgets(read,1000, file) != NULL)
				{	
					y = (int)read;
				}
			}
			fclose(file);
		}
 

Bugman

Active member
Joined
Aug 14, 2014
Messages
68
Reaction score
0
maybe creating a single thread for this

then it should work
 

0x_

Wtf I'm not new....
Staff member
Administrator
Joined
Feb 18, 2013
Messages
1,123
Reaction score
174
wtf? This code is such a mess, I think you should start with explaining what you want to accomplish.
 

0x32789

Expert
Joined
May 26, 2014
Messages
849
Reaction score
51
Location
LongForgotten <-> 0x32789
0x688 said:
wtf? This code is such a mess, I think you should start with explaining what you want to accomplish.

I told you, now you should help me, I cant understand stdio stream reader & writer was easy and better but cant create SAMP Plugin with clr
 

0x_

Wtf I'm not new....
Staff member
Administrator
Joined
Feb 18, 2013
Messages
1,123
Reaction score
174
0x32789 said:
now you should help me

Definitely not like that.

First of all you don't explain what you want to accomplish I can take wild guesses and say you are trying to read some sort of config. But your code completely accomplishes something different you probably didn't read proper documentation of the functions used in your code it just looks a wild copy-paste.
Therefore no one can actually make something meaningful of your provided code snippets AND THEREFORE you should instead tell us what you want to accomplish so we / i can give proper help.
 

0x_

Wtf I'm not new....
Staff member
Administrator
Joined
Feb 18, 2013
Messages
1,123
Reaction score
174
[shcode=cpp]
FILE* pf = fopen("filename", "w");
if(pf)
{
    // write if you want
    fclose(pf);
}


FILE *pf = fopen(szPath, "rb");
if (pf)
{
    fseek(pf, 0, SEEK_END); // go to the file end
    long lSize = ftell(pf); // get current stream position (therefore size as we seek to the end)
    rewind(pf); // go back to the beginning

    char* pszBuffer;
    szBuffer = (char*)malloc(sizeof(char)*lSize); // allocate buffer
    fread(pszBuffer, 1, lSize, pf); // read the file into our newly created buffer

    // parse your file here
    
    free(pszBuffer); // free the memory DO NOT FORGET THIS
    fclose(pf);
}
[/shcode]
What's so difficult all this is possible with reading the documentation.
I see you invoking functions in memory but you can't do something simple like this?
 
Top