| |

VerySource

 Forgot password?
 Register
Search
View: 749|Reply: 3

The problem of file flow is very simple.

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-10 09:20:01
| Show all posts |Read mode
Assume that some data is stored in the file:
x = 0, y = 0, z = 0
x = 10, y = 0, z = 0
x = 20, y = 0, z = 0
x = 30, y = 0, z = 0
x = 40, y = 0, z = 0
x = 50, y = 0, z = 0
x = 60, y = 0, z = 0
x = 70, y = 0, z = 0
x = 80, y = 0, z = 0
x = 90, y = 0, z = 0
I need to read them line by line and store them in an array like this:
First define a structure:
struct Node
{
public int node_x;
public int node_y;
                  public int node_z;
}
public Node [] Nodelist = new Node [];
Then save the x, y, and z values ​​in the file to node_x, node_y, and node_z in the NodeList. Which method is faster and better? The amount of data is about a few thousand lines.
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-13 13:00:01
| Show all posts
int x = Environment.TickCount;
            StreamReader file = new StreamReader ("d:\\1.txt", Encoding.Default);
            string [] tmp = file.ReadToEnd (). Split (new string [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
            Node [] NodeList = new Node [tmp.Length];
            for (int i = 0; i <tmp.Length; i ++)
            {
                string [] str = tmp [i] .Split (',');
                NodeList [i] .Node_x = int.Parse (str [0] .Split ('=') [1]);
                NodeList [i] .Node_y = int.Parse (str [1] .Split ('=') [1]);
                NodeList [i] .Node_z = int.Parse (str [2] .Split ('=') [1]);
            }
            file.Close ();
            x = Environment.TickCount-x;
            MessageBox.Show (x.ToString ());


10 thousand lines is 31 milliseconds ^^
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-14 13:27:01
| Show all posts
static void Main (string [] args)
{
    int i = 0;
    StreamReader sr = new StreamReader ("C:\\test.txt", Encoding.Default);
    Regex regNum = new Regex (@ "\d +");
    for (string line = sr.ReadLine (); line! = null; line = sr.ReadLine ())
    {
        MatchCollection mc = regNum.Matches (line);
        if (mc.Count> 0)
        {
            Nodelist [i] .node_x = int.Parse (mc [0] .Value);
            Nodelist [i] .node_y = int.Parse (mc [1] .Value);
            Nodelist [i] .node_z = int.Parse (mc [2] .Value);
            i ++;
        }
    }
    sr.Close ();
}

For reference only ~~
Reply

Use magic Report

0

Threads

10

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-19 21:45:02
| Show all posts
Use StreamReader for stream reading,
Loop the StreamReader.ReadLine method,
The speed should be very fast. Only a few thousand lines can't feel it.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list