Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving text manipulated data

Status
Not open for further replies.

leprogrammer

Programmer
Mar 21, 2008
25
0
0
DK
Hello.

I have a small question/problem.

I have this text file:
Code:
// Test
0
0    0    0    15    15    7    // 0
end

1
1    1    30    25    25    240    240    -1    5    // 1   
end


I read it into an array in C#, but in my array I read it in as
Code:
0    0    0    15    15    7    // 0
1    1    30    25    25    240    240    -1    5    // 1

Since this is a easyer way of manipulating with the data, but now I want to save the data to the text file.
I know the line number of my array, but that does not add up with the orginal file since I stripped som things

So my question is: How do I save the manipulated data so it fits with the orginal data?

My code looks like this:
Code:
            string[] SaveMonsterLines = File.ReadAllLines(XMLfileLocation);
            SaveMonsterLines[linenumber] = "1    1    30    25    25    240    240    -1    5    // 1";
            File.WriteAllLines(@"D:/output.txt", SaveMonsterLines);

If you dont understand my question/problem, please speak up :)
 
Where does this file format come from? Are you pulling it from some other app?

If you can - I would use XML instead as it makes data import/export easier.

Also - I'd be tempted to load each line into its own class that you can work with since each record seems to be standard. Then make the class write itself back out to the file using a Save() method or something...

As for saving the data back out - you are creating a new file correct? You want to write out each record into this new file?

 
All right..

I will try to explain again

I have the array MonsterDataArray which contains the data from the text file

The array looks like this:
0 0 0 15 15 7 // 0
1 1 30 25 25 240 240 -1 5 // 1


And both of the lines above has a line number in the array which is 0 and 1


Now I changed something in my array. Lets say I changed line number 0

I changed it from:
0 0 0 15 15 7 // 0

to:
4 7 2 30 45 9 // bleh


Now the line number 0 is updated in the MonsterDataArray, but it is not updated in the text file

This is where my problem is.. I dont know how to save the changed line number 0 from my MonsterDataArray to my text file because if I use my current code the line number from my array doesnt match up with my text file since I removed some things

I hope this helps
 
A text file should just be a snapshot of your current data. Read your data in - all of it - use it, change it, delete some of it, add to it - whatever you want. When you're done and want to save your data again you should write the data to a new file.

Then delete the old file and rename the new one.

 
i agree with jurkmonkey. you need to read the whole file into a collection of objects, modify the objects and write the contents out again.

your using text files as persistent storage which is probably the worst option. even xml would be better than text. Since your dealing with monsters. I'm assuming this is a video game of sometype. maybe a better option would be a sqlite or sqlcd database.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
We've seem to have a lot of these monster posts on here...

If it's an assignment - I'd love to see what it says and smack the teacher that is giving it out :)

 
Well its not an assigment, but just a sparetime project I have been working on to develop my C# skills

As for using text files I cant rellay do anything about it, because I didn develop the game. Im just making software for it :)

I would personally prefer a sql database, but I cant rellay do anything about that :p


To the topic: I need to recode big parts of my project, but I cant seem what your point is and I will consider it
 
Ok now I got the saving text working by nor removing any data from the text file so my array looks like this:
Code:
// Test
0
0    0    0    15    15    7    // 0
end

1
1    1    30    25    25    240    240    -1    5    // 1   
end

But now I ran into another problem

I have a image where I draw ellipses on with the coordinates from my text file (X: 15 Y: 15 and the 2: X: 25, Y: 25). It triggers on the paint event from the image

I also have a mouse down event on my image

I save all my ellipses in a list called
Code:
List<Rectangle> mapRectangleList = new List<Rectangle>();

When I do mouse down I see if the mouse is over a ellipse. If it is it jumps to a specific line in my MonsterDataArray, but the problem is when I save all my ellipses in the list I dont take end, 0, // and the other things from my text with. I only take:
Code:
1	0	30	25	25	240	240	-1	5 	// Red Dragon	
2	0	30	35	35	185	132	-1	10  	// Golden Titan

So when I jump to specific line in my array the line doesnt match up

Yeah im kinda lost here..

Here is the mouse down part:
Code:
// Getting cursor position
            Point p = mapImage.PointToClient(Cursor.Position);

            for (int i = 0; i < mapRectangleList.Count; i++)
            {
                if (mapRectangleList[i].Contains(p))
                {
                    ellipseNum = i;
                    break;
                }
            }
            // If ellipsenum is over a ellipse
            if (ellipseNum > -1)
            {
                MessageBox.Show(Convert.ToString(ellipseNum));
                displayLine(ellipseNum);
            }
            else
            {
                // Else show alert
                MessageBox.Show("Not currently over any ellipse");
            }

Paint event:
Code:
            // Open the data file (msbr = monsterSetBaseRead)
            StreamReader msbr = new StreamReader(new FileStream(@XMLmonsterSetBaseLocation, FileMode.Open));

            // Reading all data
            while ((MapMonsterSetBaseData = msbr.ReadLine()) != null)
            {
                // Splitting data into TxtPiecesMap[]
                String[] TxtPiecesMap = MapMonsterSetBaseData.Split('\t');

                // Checking if data is a correct monster
                checkIf(MapMonsterSetBaseData);

                // If it is then continue
                if (checkIfStatus == true)
                {
                    // Getting the x and y cord
                    MapXCord = Convert.ToInt16(TxtPiecesMap[3]) * 2;
                    MapYCord = Convert.ToInt16(TxtPiecesMap[4]) * 2;

                    // Creating grapic
                    Graphics g = e.Graphics;

                    // Creating rectangle with cords from coordinates
                    Rectangle r = new Rectangle(MapXCord, MapYCord, 6, 6);

                    // Drawing ellipse on map
                    g.DrawEllipse(Pens.Gold, r);

                    // Adding data for ToolTips and Rectangles lists
                    mapRectangleList.Add(r);
                }

And my checkif
Code:
        private void checkIf(string data)
        {
            // If monster string is correct
            if (data != "" && data.Length != 3 && data != "end" && data.Substring(0, 1) != "/" && data.Length != 1)
            {
                // Passes
                checkIfStatus = true;
            }
            else
            {
                // Failed
                checkIfStatus = false;
            }
        }

Dont know if this makes any sense

There might be an easier soloution, but I dont know it

Its allmost the same problem as before, but in a diffrent context
 
does the new problem relate to the reading/writing text files. if not you should start a new thread.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top