I have 890kb text file i need to put each line in its place in the original file i extracted it from

問題 I have 890kb text file i need to put each line in its place in the original file i extracted it from

  • Wednesday, April 25, 2012 8:54 AM
     
      Has Code
    private void retriving()
            {
                w = new StreamWriter(@"d:\test.txt");
                string str = f;
                Regex pattern = new Regex("(?<=\").*?(?=&lt;pa&gt;\">)", RegexOptions.RightToLeft);
    
                MatchCollection allMatches = pattern.Matches(str);
    
                // Iterate and print all matches
                foreach (var m in allMatches)
                {
                    listBox1.Items.Add(m);
                    w.WriteLine(m);
                }
                w.Close();
            }

    This is how i extract take out the text i needed.

    Now i need to put it all back i have a new text file 890kb called: test1.txt and i need to put the text in the right places in the big 43mb file the text was taken from.

    This is how i read the big file to extract from:

    r = new StreamReader(@"D:\New folder (22)\000004aa.xml");
    f = r.ReadToEnd();

    Now i need to take all the text in the 890kb file and put it back to the exact locations in the 000004aa.xml file


    danieli


    • Edited by chocolade Wednesday, April 25, 2012 8:55 AM
    •  

All Replies

  • Thursday, April 26, 2012 5:58 PM
     
      Has Code

    What i mean is something like this:

    private void addingTranslatedFiles()
            {
                StreamWriter w = new StreamWriter(@"D:\New folder (22)\000004aa.xml");
                w.AutoFlush = true;
                int currentLength;
                int currentIndex;
                int lastIndex = 0;
                string startTag = "\"";
                string endTag = "&lt;pa&gt;";
                int startTagWidth = startTag.Length;
                //int endTagWidth = endTag.Length;
                int index = 0;
                while ((line = h.ReadLine()) != null)
                {
    
                    //while (true)
                    //{
    
                    index = f.IndexOf(startTag, index);
                    if (index == -1)
                    {
                        //startTag not found, but endTag may still be found
                        index = f.IndexOf(endTag, lastIndex - 1);
                        if (index != -1)
                        {
                            w.Write(f.Substring(lastIndex, endTag.Length));
                        }
    
                        break;
                    }
                    // else more to do - index now is positioned at first character of startTag    
                    int start = index + startTagWidth;
    
                    if (start >= f.Length)
                        break;
    
                    currentIndex = start;
                    index = f.IndexOf(endTag, start + 1);
                    if (index == -1)
                    {
                        break;
                    }
                    // found the endTag    
                    string g = f.Substring(start, index - start - 1);
                    currentLength = index - start;
                    string hh = f.Substring(lastIndex, currentIndex - lastIndex);
                    w.Write(hh);
                    w.Write(line);
                    listBox2.Items.Add(hh);
                    lastIndex = currentIndex + currentLength;
                    listBox1.Items.Add(g);
                    //}
                    //w.Close();
                }
                w.Write(f.Substring(lastIndex));
                w.Close();
            }
    

    But it dosent work good.

    I have a small xml file 819kb and i have another big xml file 43mb

    I need to read from the big file i think and then in the first time im getting to place wich have the start tag and the end tag to read a line from the small file and put this line into the big file in the exact place where it find the startTag and the endTag then to read the next big file again to continue reading it untill the next place the start and end are found then read the second line from the small file and put the second line in the big file in the exact place.


    And so on.


    The startTag is "

    The endTag is

    &lt;pa&gt;

    But this code dosent work good.





    danieli

  • Friday, April 27, 2012 10:04 AM
    Moderator
     
     

    Hi chocolade,

    Welcome to the MSDN forum.

    Can you provide more detailed information about the structure of your file please?


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, April 27, 2012 10:19 AM
     
     

    Hi chocolade,

    Welcome to the MSDN forum.

    Can you provide more detailed information about the structure of your file please?


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/88c728f9-5e6e-405f-862c-f10e4d45f300

    danieli

  • Monday, April 30, 2012 11:34 AM
    Moderator
     
     

    Hi danieli,

    In the other thread you mentioned, you parse the text content from a big file.  Now you want to put it back?  I feel confused.  The content you extract should be in the big file already, right?   Why do you want to put the extract content again? 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, May 01, 2012 2:34 AM
     
     

    Hi danieli,

    In the other thread you mentioned, you parse the text content from a big file.  Now you want to put it back?  I feel confused.  The content you extract should be in the big file already, right?   Why do you want to put the extract content again? 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us


    I translated the xml text i extracted from germany to english now when its english i want to replace this english text with the german text in the big xml file.

    danieli

  • Tuesday, May 01, 2012 3:10 AM
    Moderator
     
     

    Hi danieli,

    So if I understand it correctly, we can use the similar method when we extract the content to replace the text in the big file.  If you have any specific technical problem to do so, could you make your request be more specific?  Ít will be easier for us to help you. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us