1.

Solve : File/String manipulation, C++?

Answer»

I have been hunting around, but not yet found a way to do this...

Warning: Much stuff Below this line!
--------------------------------------------------------------------------------------

Before we begin, I'm using Dev C++ 4.9

Here is how it's supposed to work:

Code: [Select]#include <iostream>
#include <windows.h>
/* Sometimes mingw (part of Dev C++) doesn't include this file, however it might be useful...
*/
#include <stdio.h>
#include <fstream>
#include <string>

int main()
{
new filestr[128]; // String, containing the filename or the path to it, like: C:\myfolder\myfile.txt
new str[96]; // Main data string

printf("Specify the file to open:\n");
cin >> filestr;
/* By default, the TXT extension will be added to filestr
Trying to open filestr.txt

if that file doesn't exist, do this:
Try opening filestr.map
if that file doesn't exist, do this:
Try opening filestr
if that file doesn't exist, do this:
list all files found with similar name, Example: 133.txt, 133.log, 133.inf, 133.ini, 133.map
and let the user choose which one to open

if no files are found, an error message would SHOW: "Error: File doesn't exist!"
else, we just continue:
*/
fstream file;
ofstream FILE;
FILE.open(filestr);
/* Here we begin messing with the text
if the opened file contains any of these, we proceed, else this would appear:"\nError: File Not Useble!\n"

The usual:
AddStaticVehicleEx(modelid, x, y, z, a, colorA, colorB, delay);
Would be changed into:
Vehicle[£] = AddStaticVehicleEx(modelid, x, y, z, a, colorA, colorB, delay);

Or The messy one:

<object name="test">
<position>x y z</position>
<rotation>rX rY rZ</rotation>
<model>OBJ_model</model>
</object>

NOTE: The numbers can be within these ranges:
Name: This is used as a name and/or description FIELD
Position: 3 float variables, raging from -3000 to 3000, example: 2000.1 & 2991.06 & -1200.78
Rotation: 3 float variables, I don't know the range, it's counted in radians
Model: OBJ_model, raging from 1 to 70000

However that block or WHATEVER we should name it, would turn into this:

We start by converting rX, rY and rZ:
rX = RadiansToDegrees(rX);
rY = RadiansToDegrees(rY);
rZ = RadiansToDegrees(rZ);

Note: RadiansToDegrees doesn't exist, I would create it if I knew how to convert between radians and degrees

if name is object or object(int) , we do this:
"Object[@] = CreateObject(modelid, x, y, z, rX, rY, rZ); // name\n";
else we do this:
first we check if 'name' contains spaces and REPLACE them with '_' (underline/underscore)
Then we do:
"name = CreateObject(modelid, x, y, z, rX, rY, rZ);\n";

Now lets continue, this was just the half of it :)
*/
printf("Should the file be saved as a PWN file?");
/* Here we ask the user if he/she want's to save the file as filestr.pwn instead of filestr with it's original
extension.
However, if the user opens a PWN file, it ins't neccesarry to ask about it, so we skip that question and
save the file later as filestr.pwn

if the user chooses yes, we do this: (Applies when asked if save as PWN)
FILE << "#include <a_samp>\n\n"; // This is supposed to be on the first line in the file we write to
*/

// Now lets continue..

int obj_count;
int veh_count;

FILE << "new Vehicle[£] :\n";
FILE << "new Object[@] :\n";

FILE << "// Vehicles:\n";
for(veh_count < £; veh_count++; veh_count = 0){
FILE << "Vehicle[£] = AddStaticVehicleEx(modelid, x, y, z, a, colorA, colorB, delay);\n";
}
FILE << "// Objects:\n";
for(obj_count < £; obj_count++; obj_count = 0){
FILE << "Object[@] = CreateObject(modelid, x, y, z, rX, rY, rZ;\n";
}
cout >> "Vehicles: " >> veh_count << endl;
cout >> "Objects: " >> obj_count << endl;
FILE.close();
printf("\n\nSuccess!");
}

£ means a number which starts from 0 (zero) and increases with each instance of AddStaticVehicleEx()
@ means a number which starts from 0 (zero) and increases with each instance of CreateObject()

Notes:

This should not be case sensitive when reading files

The code isn't supposed to write the same line twice, even if it kinda LOOKS that way


Help would be appreciated, as I'm not getting anywhere myself Quote from: Ryder17z on September 23, 2009, 05:04:10 PM

Note: RadiansToDegrees doesn't exist, I would create it if I knew how to convert between radians and degrees

A small contribution:

A circle is 2*pi radians, therefore a half circle is pi radians. So you should be able to easily see that degrees = radians * 180/pi.

It may help to think of a radian as being the angle of an arc created by wrapping the radius of a circle around its circumference.



Discussion

No Comment Found