SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting confused, pls help

    Hi all.

    I am reading some ip addresses from a txt file. I want to break them in a set of 30 IP address and assign them to their seperate variables for further reference. The issue is, this text file will contain more ip address in coming days and want my application(console application) to be scalable. I am newbie to .net so i am sorry if i am asking something very basic but pls guide me. I want to achieve something logically like below

    [1] = "10.1.0.1" "10.1.0.2" "10.1.0.3" "10.1.0.4" ..... "10.1.0.30"
    [2] = "10.1.0.31" "10.1.0.32"

    Someone pls guide me

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Can you provide a sample format of the text file?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its like this

    10.1.0.1
    10.1.0.2
    10.1.0.3
    10.1.0.4
    ... so on

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, so the below should accomplish this, and I know there are other ways to accomplish this as well, but this should get you started
    Code:
    			var ipAddresses = new List<List<string>>();
    			using (StreamReader sr = new StreamReader("C:\\ipaddresses.txt"))
    			{
    				string ipAddress;
    				int ipAddressLocation;
    				int ipAddressCount = 1;
    
    				// Read to the end of the file
    				while(!sr.EndOfStream)
    				{
    					// Read the ip address
    					ipAddress = sr.ReadLine();
    					// Determine which array the ip address should be part of
    					ipAddressLocation = (int)Math.Floor(ipAddressCount/30.0);
    
    					// Check that the array exists, otherwise, create it
    					if (ipAddresses.Count <= ipAddressLocation)
    					{
    						ipAddresses.Add(new List<string>());
    					}
    
    					// Add the ip address to its' array
    					ipAddresses[ipAddressLocation].Add(ipAddress);
    
    					ipAddressCount++;
    				}
    			}
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot, it workded perfectly

    thanks alot sir

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sir,

    Please dont mind, i am trying to understand your logic, and so far its amazing. I am having just one problem, if there are any ip addresses that do not fit in the 30 set, they are not counted. I mean i have total 126 ip addresses. As you can see, till 120 it forms 4 sets and the rest 6 ip addresses are not included. Can you kindly help me out

    thanks alot in advance and i wont bother you with continous request. this is the last one sir

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I changed ipAddressCount = 1 to ipAddressCount = 0
    Code:
    			var ipAddresses = new List<List<string>>();
    			using (StreamReader sr = new StreamReader("C:\\ipaddresses.txt"))
    			{
    				string ipAddress;
    				int ipAddressLocation;
    				int ipAddressCount = 1;
    
    				// Read to the end of the file
    				while(!sr.EndOfStream)
    				{
    					// Read the ip address
    					ipAddress = sr.ReadLine();
    					// Determine which array the ip address should be part of
    					ipAddressLocation = (int)Math.Floor(ipAddressCount/30.0);
    
    					// Check that the array exists, otherwise, create it
    					if (ipAddresses.Count <= ipAddressLocation)
    					{
    						ipAddresses.Add(new List<string>());
    					}
    
    					// Add the ip address to its' array
    					ipAddresses[ipAddressLocation].Add(ipAddress);
    
    					ipAddressCount++;
    				}
    			}
    This gave me 5 arrays, the first 4 have 30 ip addresses each, the last has 6 (verified using a debugger).
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •