Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Search Data from File in FIle2

Search Data from File in FIle2

Scheduled Pinned Locked Moved C#
questionregexlounge
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The foreach statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.

                while (!sr1.EndOfStream)
                {
                    holdLineHid = sr1.ReadLine();
                    holdLineNot = sr.ReadToEnd();
                        if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot))
                            continue;
                        string hid1 = holdLineHid.Substring(0, 11);
                        string newhid = holdLineHid.Substring(14, 11);
                        string hid2 = holdLineNot.Substring(0, 11);
                        string notremainder = holdLineNot.Substring(11, 683).Trim();
                        foreach (String hid in holdLineHid)
                        {
                            if (hid1 == hid2)
                            {
                                sw.WriteLine(newhid + notremainder);
                            }
    

    For instance: In File1 I have the following: C0000000001 C0000013456 C0000000002 C0003245678 In File2 I have the following data: C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match How do I get this to run properly? Thanks in advance.

    Excellence is doing ordinary things extraordinarily well.

    L M 2 Replies Last reply
    0
    • M MumbleB

      Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The foreach statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.

                  while (!sr1.EndOfStream)
                  {
                      holdLineHid = sr1.ReadLine();
                      holdLineNot = sr.ReadToEnd();
                          if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot))
                              continue;
                          string hid1 = holdLineHid.Substring(0, 11);
                          string newhid = holdLineHid.Substring(14, 11);
                          string hid2 = holdLineNot.Substring(0, 11);
                          string notremainder = holdLineNot.Substring(11, 683).Trim();
                          foreach (String hid in holdLineHid)
                          {
                              if (hid1 == hid2)
                              {
                                  sw.WriteLine(newhid + notremainder);
                              }
      

      For instance: In File1 I have the following: C0000000001 C0000013456 C0000000002 C0003245678 In File2 I have the following data: C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match How do I get this to run properly? Thanks in advance.

      Excellence is doing ordinary things extraordinarily well.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The easiest way is too read the file using : string[] lines = File.ReadAllLines(@"c:\myfile.txt"); Then just use: foreach(string line in lines) { //... do what you want here... }

      M 1 Reply Last reply
      0
      • M MumbleB

        Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The foreach statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.

                    while (!sr1.EndOfStream)
                    {
                        holdLineHid = sr1.ReadLine();
                        holdLineNot = sr.ReadToEnd();
                            if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot))
                                continue;
                            string hid1 = holdLineHid.Substring(0, 11);
                            string newhid = holdLineHid.Substring(14, 11);
                            string hid2 = holdLineNot.Substring(0, 11);
                            string notremainder = holdLineNot.Substring(11, 683).Trim();
                            foreach (String hid in holdLineHid)
                            {
                                if (hid1 == hid2)
                                {
                                    sw.WriteLine(newhid + notremainder);
                                }
        

        For instance: In File1 I have the following: C0000000001 C0000013456 C0000000002 C0003245678 In File2 I have the following data: C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match How do I get this to run properly? Thanks in advance.

        Excellence is doing ordinary things extraordinarily well.

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        your foreach will only loop once as your only giving in one string in the holdLineHid (which i assume is a string) what you want to do is create a streamreader for file 1. Read the first line into a string and then create a while loop while not null ...

        StreamReader sr = new StreamReader("file1");

        string line = sr.ReadLine();

        while(line != null)
        {
        //do something with the line
        line = sr.ReadLine();
        }

        sr.close();

        Now, in your instance for file 1 you have put two id numbers on each line. Are they one per line in the file? or is there more than one on each line? Then in your while loop, where i put the comment you could do the following...

        StreamReader sr2 = new StreamReader("file2");

        string line2 = sr2.ReadLine();

        while(line2 != null)
        {
        string hid = line2.Substring(0, 11);

        if(hid == line1)
        {
        //Do something with the stream writer
        break;//Break the while loop
        }

        line2 = sr2.ReadLine();
        }

        sr2.Close();

        That should do what you want

        Life goes very fast. Tomorrow, today is already yesterday.

        M 1 Reply Last reply
        0
        • L Lost User

          The easiest way is too read the file using : string[] lines = File.ReadAllLines(@"c:\myfile.txt"); Then just use: foreach(string line in lines) { //... do what you want here... }

          M Offline
          M Offline
          MumbleB
          wrote on last edited by
          #4

          Thanks mate. I'll give this a go this evening and see how it works.

          Excellence is doing ordinary things extraordinarily well.

          1 Reply Last reply
          0
          • M musefan

            your foreach will only loop once as your only giving in one string in the holdLineHid (which i assume is a string) what you want to do is create a streamreader for file 1. Read the first line into a string and then create a while loop while not null ...

            StreamReader sr = new StreamReader("file1");

            string line = sr.ReadLine();

            while(line != null)
            {
            //do something with the line
            line = sr.ReadLine();
            }

            sr.close();

            Now, in your instance for file 1 you have put two id numbers on each line. Are they one per line in the file? or is there more than one on each line? Then in your while loop, where i put the comment you could do the following...

            StreamReader sr2 = new StreamReader("file2");

            string line2 = sr2.ReadLine();

            while(line2 != null)
            {
            string hid = line2.Substring(0, 11);

            if(hid == line1)
            {
            //Do something with the stream writer
            break;//Break the while loop
            }

            line2 = sr2.ReadLine();
            }

            sr2.Close();

            That should do what you want

            Life goes very fast. Tomorrow, today is already yesterday.

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            Hi Mate. I get the jist of this but it seems that the code you have given above puts me in an infinate loop. TO answer your questions:

            musefan wrote:

            Are they one per line in the file? or is there more than one on each line?

            File 1 contains +- 1000 lines with two ID's on each line, the first ID is the one to matched to file2 and the second ID is trhe one to be written to the output file along with the remainder of the data on the matching line of file2. Hope this makes sense. Almost like doing a match in Microsoft Access and then output a combination of the data from the two files.

            Excellence is doing ordinary things extraordinarily well.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups