Wanting to.... [Read On]

Posts 1–14 of 14 · Page 1 of 1
Wanting to.... [Read On]
ok guys get ready for this.

i am wanting to read a file for specific words line by line when that keyword is found print a byte to another file for example search for "lol1" in nay.txt, then write byte 0x02 to nay.dat, this i have achieved what i know want to do is keep looking through the file and for every "lol1" print a new byte to nay.dat.

Here is my current code for this part of the program:
Code:
            String line;
            byte[] data = System****.File.ReadAllBytes("" + global + "/" + "TROPTRNS.dat"); // put real name

            BinaryWriter BW = new BinaryWriter(File.OpenWrite("" + global + "/" + "TROPTRNS.dat"));
 
            int counter = 0;
            string tt = "T";
            string TextLine = "";
            System****.StreamReader Test = new System****.StreamReader("" + global + "/" + "TROPCONF.sfm");
            TextLine = Test.ReadLine();
           do
            {
                TextLine = Test.ReadLine();


                if (TextLine.Contains("Bronze"))
                {
                    listBox1.Items.Add("Bronze");
                }
                else if (TextLine.Contains("Silver"))
                {
                    listBox1.Items.Add("Silver");
                }
                else if (TextLine.Contains("Gold"))
                {
                    listBox1.Items.Add("Gold");
                }
            } while (Test.Peek() != -1); // while ((line = Test.ReadLine()) != null); //Test.Peek() != -1

            //listBox1.Items.Add(TextLine);

            for (int i = 0x377; i <= 0x377; i++)
            {
                while (i < data.Length)
                {
                    BW.BaseStream.Position = i;
                    if (TextLine.Contains("ttype=\"B\" "))
                    {
                        BW.Write(0x10000004);
                        //i += 176;
                    }
                    else if (TextLine.Contains("ttype=\"S\" ")) // .Contains
                    {
                        BW.Write(0x88888888);
                        //i += 176;
                    }
                    else if (TextLine.Contains("ttype=\"G\" "))
                    {
                        BW.Write(0x99999999);
                        //i += 176;
                    }
                    i += 176;
                    //Test.ReadLine() +1;
                }
            }
            Test.Close();
            BW.Close();
There are alot of comments and stuff that isnt needed, this is here as i have been testing multiple things, anyway i hope some one can help,

i have been trying for hours but i just cant think of a solution...

cheers in advance.
What is the problem?

Set a break point on the line...

TextLine = Test.ReadLine(); // just inside the DO loop

and make sure 'TextLine' is actually being set to 1 line of the txt file.
Keep looping through and make sure TextLine keeps being updated correctly. Contains() looks correct, so I assume you're not reading the file correctly. (?)

"for (int i = 0x377; i <= 0x377; i++)" -- err? aka "do once: no loop needed" ... :/

There are much easier ways to read/write to files. Copy some different code..
Quote Originally Posted by abuckau907 View Post
What is the problem?

Set a break point on the line...

TextLine = Test.ReadLine(); // just inside the DO loop

and make sure 'TextLine' is actually being set to 1 line of the txt file.
Keep looping through and make sure TextLine keeps being updated correctly. Contains() looks correct, so I assume you're not reading the file correctly. (?)

"for (int i = 0x377; i <= 0x377; i++)" -- err? aka "do once: no loop needed" ... :/

There are much easier ways to read/write to files. Copy some different code..
that for loop is needed for the other file im printing to these are the starting offsets, the problem is the program is search through the lines as soon as it sees the first line, that determines the outcome for everything i.e. as soon as the program sees the file contains "nay" it will print "0x02" to the offsets given, i dont want that i want the program to search the line if it contains nay then print 0x02 then search second line , if that contains "lolol" then print 0x05 etc etc.. if that makes sense its quite hard to explain.
that for loop only executes once and isn't really a loop. Or am I reading that wrong?

'i' will == 0x377 the first go-around. i will get + 0x176'd inside the WHILE loop a few times, so once the while loop is done, it'll jump back to the top of the for loop
and 'i' will be too large and it will exit the loop. It only loops once. :/ Where did you get 0x377 and 0x176?

Why not do this with plain .txt files and use regular characters so you can easily verify the output?
--upload to pastebindotcom the example input file you're working with, and the output you got.
Quote Originally Posted by abuckau907 View Post
that for loop only executes once and isn't really a loop. Or am I reading that wrong?

'i' will == 0x377 the first go-around. i will get + 0x176'd inside the WHILE loop a few times, so once the while loop is done, it'll jump back to the top of the for loop
and 'i' will be too large and it will exit the loop. It only loops once. :/ Where did you get 0x377 and 0x176?

Why not do this with plain .txt files and use regular characters so you can easily verify the output?
--upload to pastebindotcom the example input file you're working with, and the output you got.

im using a file configured by the playstation im making a trophy modding tool, i need to look up a certain file for values then whatever the values are i need to print certain offset values to another file here is my code now

Code:
System****.StreamReader Files = new System****.StreamReader("" + global + "/" + "TROPCONF.sfm");
            int counter = 0;
            string Line;
            string TTs = null;

            while ((Line = Files.ReadLine()) != null)
            {
                listBox1.Items.Add(Line + counter);
                //Line += 4;
                TTs = Line;

                if (counter >= data.Length) break;
                counter++;
            }

            for (int i = 0x377; i <= 0x377; i++)
            {
                while (i < data.Length)
                {

                        BW.BaseStream.Position = i;
                        if (TTs.Contains("ttype=\"B\" "))
                        {
                            BW.Write(0x10000004);
                            //i += 176;
                        }
                        if (TTs.Contains("ttype=\"S\" ")) // .Contains
                        {
                            BW.Write(0x88888888);
                            //i += 176;
                        }
                        if (TTs.Contains("ttype=\"G\" "))
                        {
                            BW.Write(0x99999999);
                            //i += 176;
                        }
                        i += 176;
                        //counter++;
                       
                        //Test.ReadLine() +1;
                    }
                }
            //counter++;
            //}
            
            BW.Close();
            Files.Close();


---------- Post added at 10:30 PM ---------- Previous post was at 10:29 PM ----------

i think the bug maybe where im looking at every line, i need to look at every 4 lines any idea how to achieve this?

---------- Post added at 10:32 PM ---------- Previous post was at 10:30 PM ----------

heres the top of the file so you understand what im doing
Code:
            String line;
            byte[] data = System****.File.ReadAllBytes("" + global + "/" + "TROPTRNS.dat"); // put real name

            BinaryWriter BW = new BinaryWriter(File.OpenWrite("" + global + "/" + "TROPTRNS.dat"));
            for (int i = 0x357; i <= 0x357; i++)
            {
                while (i < data.Length) // need to get last offset of file.
                {
// dont need to see this
                }
            }


---------- Post added at 10:33 PM ---------- Previous post was at 10:32 PM ----------

where it says you dont need to see this, this is just a piece of code that has no meaning to you just me

---------- Post added at 10:36 PM ---------- Previous post was at 10:33 PM ----------

Also i is the starting offset value, data.length is the length of the file, im using a while loop, if i is less that the file size then carry on, else break the operation.
I know what the loops are for. I was saying you could have just created and initialized the variable 'i' : the 'for loop' only executes once and isn't a loop. Not a problem, but it's weird.

Code:
                while (i < data.Length)
                {

                        BW.BaseStream.Position = i;
                        if (TTs.Contains("ttype=\"B\" "))
                        ..
                }
//TTs has 1 value through entire while loop?

Reading 4 lines at a time isn't hard/a problem. I understand what you mean, but not exactly what you're trying to do sorry. Screen shots maybe?
Quote Originally Posted by abuckau907 View Post
I know what the loops are for. I was saying you could have just created and initialized the variable 'i' : the 'for loop' only executes once and isn't a loop. Not a problem, but it's weird.

Code:
                while (i < data.Length)
                {

                        BW.BaseStream.Position = i;
                        if (TTs.Contains("ttype=\"B\" "))
TTS is always the last line read from the file?

Reading 4 lines at a time isn't hard/a problem. I understand what you mean, but not exactly what you're trying to do sorry. Screen shots maybe?

really hard to screenshot, its a bastard to explain ive used TTS as a variable that stores the line so it can be used else where, the program prints information into the file i want in the correct places, just not the correct information, for example i am search file.sfm for keywords such as "ttype="B"" "S" and "G" if the line contains "ttype="B"" then write a certain hex value to another file, if the line contains "ttype="S"" then write another certain hex value and so on, but the program only prints the "ttype="B"" hex value therfore my program is only writing the hex from the first keyword found. (i think thats the problem)
TTs always == the last line of the 'TROPCONF.sfm'

You never change it inside the loops, so it's always whatever the text was for the very last line in the file.

WhileLoop1()
{
TTs = ReadLine()
}
//when the loop is all done, tts = the last line of the file
//and then
ForLoop1()
{
Code:
while (i < data.Length)
{
BW.BaseStream.Position = i;
 if (TTs.Contains("ttype=\"B\" ")) // you want TTS to == each new line (or 4 lines?) of the file?
}

--------------------
check for me..
"but the program only prints the "ttype="B""

Does the last line of the file..um...'TROPCONF.sfm' contain ttype="B" ?
Quote Originally Posted by abuckau907 View Post
TTs always == the last line of the 'TROPCONF.sfm'

You never change it inside the loops, so it's always whatever the text was for the very last line in the file.

WhileLoop1()
{
TTs = ReadLine()
}
//when the loop is all done, tts = the last line of the file
//and then
ForLoop1()
{
Code:
while (i < data.Length)
{
BW.BaseStream.Position = i;
 if (TTs.Contains("ttype=\"B\" ")) // you want TTS to == each new line (or 4 lines?) of the file?
}

--------------------
check for me..
"but the program only prints the "ttype="B""

Does the last line of the file..um...'TROPCONF.sfm' contain ttype="B" ?
yes it does, glad you pointed that out, so lets try adding a loop inside the while unless you have a better option?

I have just done a test, renamed the last ttype to "S" and it still wrote the "B" value...
Idk the file format so I'm not 100% sure what you're trying to do.

Currently you start at the file pox 0x377 and keep going up by 176.
Code:
                while (i < data.Length)
                {
                 BW.Write(********)
                 i += 176;
                }
so every 176 you're writing a new value. Is this intended? Or, only if the line contains 'ttype' + 'G,S,C' or whatever?

You need to either read 4 lines a time, or change the +176 stuff. You're basically writing some bytes every single pass in the 'while' loop, even if the line from the input file doesn't contain 'ttype'. I think ?
Quote Originally Posted by abuckau907 View Post
Idk the file format so I'm not 100% sure what you're trying to do.

Currently you start at the file pox 0x377 and keep going up by 176.
Code:
                while (i < data.Length)
                {
                 BW.Write(********)
                 i += 176;
                }
so every 176 you're writing a new value. Is this intended? Or, only if the line contains 'ttype' + 'G,S,C' or whatever?

You need to either read 4 lines a time, or change the +176 stuff. You're basically writing some bytes every single pass in the 'while' loop, even if the line from the input file doesn't contain 'ttype'. I think ?
yes im intending to write bytes every +176 bytes.
so every line of the input file has

ttype="ABC" in it, Or every 4th line? // B,S,G actually

----
edit: I think this is what you're going for. Maybe?

copy-pasted your code and changed a little..
Code:
System****.StreamReader Files = new System****.StreamReader("" + global + "/" + "TROPCONF.sfm");
BinaryWriter BW = new BinaryWriter(File.OpenWrite("" + global + "/" + "TROPTRNS.dat"));
            int outputIndx = 0x377;
	    int nextObjectOffset = 176; // should be const
	    int counter = 1; // total number of lines in input file
            string Line; //individual line read from input file
 
            while ((Line = Files.ReadLine()) != null)
            {
                listBox1.Items.Add(counter + ": " + Line);
                  
  	        if (Line.Contains("ttype=\"B\" "))
                 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x10000004);
                  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"S\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x88888888);
		  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"G\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x99999999);
		  outputIndex += nextObjectOffset;
		 }
		else
		 {
                 //this line contains junk data, don't write anything yet
		 //don't increase outputIndx because we haven't written yet.
                 }
                counter++;
            }
^^not the most efficient way, but maybe helps.
Quote Originally Posted by abuckau907 View Post
so every line of the input file has

ttype="ABC" in it, Or every 4th line? // B,S,G actually

----
edit: I think this is what you're going for. Maybe?

copy-pasted your code and changed a little..
Code:
System****.StreamReader Files = new System****.StreamReader("" + global + "/" + "TROPCONF.sfm");
BinaryWriter BW = new BinaryWriter(File.OpenWrite("" + global + "/" + "TROPTRNS.dat"));
            int outputIndx = 0x377;
	    int nextObjectOffset = 176; // should be const
	    int counter = 1; // total number of lines in input file
            string Line; //individual line read from input file
 
            while ((Line = Files.ReadLine()) != null)
            {
                listBox1.Items.Add(counter + ": " + Line);
                  
  	        if (Line.Contains("ttype=\"B\" "))
                 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x10000004);
                  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"S\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x88888888);
		  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"G\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x99999999);
		  outputIndex += nextObjectOffset;
		 }
		else
		 {
                 //this line contains junk data, don't write anything yet
		 //don't increase outputIndx because we haven't written yet.
                 }
                counter++;
            }
^^not the most efficient way, but maybe helps.
thanks for your effort so far, but i have tried similar methods to what you have done, but without the other while i had "while(i<data.length)" +176 is always being looped and a simple condition break doesnt break the while loop.

---------- Post added at 03:53 PM ---------- Previous post was at 03:52 PM ----------

therefore you can never open the file and it causes the program to crash.

---------- Post added at 04:13 PM ---------- Previous post was at 03:53 PM ----------

Quote Originally Posted by abuckau907 View Post
so every line of the input file has

ttype="ABC" in it, Or every 4th line? // B,S,G actually

----
edit: I think this is what you're going for. Maybe?

copy-pasted your code and changed a little..
Code:
System****.StreamReader Files = new System****.StreamReader("" + global + "/" + "TROPCONF.sfm");
BinaryWriter BW = new BinaryWriter(File.OpenWrite("" + global + "/" + "TROPTRNS.dat"));
            int outputIndx = 0x377;
	    int nextObjectOffset = 176; // should be const
	    int counter = 1; // total number of lines in input file
            string Line; //individual line read from input file
 
            while ((Line = Files.ReadLine()) != null)
            {
                listBox1.Items.Add(counter + ": " + Line);
                  
  	        if (Line.Contains("ttype=\"B\" "))
                 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x10000004);
                  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"S\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x88888888);
		  outputIndex += nextObjectOffset;
		 }
		else if (Line.contains("ttype=\"G\" "))
		 {
                  BW.BaseStream.Position = outputIndex;
		  BW.Write(0x99999999);
		  outputIndex += nextObjectOffset;
		 }
		else
		 {
                 //this line contains junk data, don't write anything yet
		 //don't increase outputIndx because we haven't written yet.
                 }
                counter++;
            }
^^not the most efficient way, but maybe helps.

ok i have now got it working, tweaked the code you gave me and walah, all i need to do know is read every 4 lines any idea? would i have to set variable as a string array, then each line represents a number in the array or is there any easier ways?
Nice. Array works fine. Get used to 'handling data' of different types, you're going to be doing it a lot
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Talk with us