none
change array dimension RRS feed

  • Question

  • I have a program where i am setting array to read file data into it.

    If there are more than dimension ,

    How can i change it?

    Code:

    bytes[] = File.ReadAllBytes(filename); //read binary file

    int i = 0;

    int[] Data = new int[100]

    while (i<read.length)

    {

         data[i] = BitConverter.ToInt32(bytes, i*4);

        i++;

    }

    if length is great than 400 ,

    I want to add extra until all are read into data.

    Tuesday, November 5, 2019 3:30 PM

Answers

  • Rather than an array, use a list and add to it. If once done you need an array rather than a list use .ToArray() in the list e.g.

    var data = someList.ToArray();

    byte[] bytes = File.ReadAllBytes(fileName); //read binary file
    var intList = new List<int>();
    
    int i = 0;
    while (i < read.length)
    {
    
        intList.Add(BitConverter.ToInt32(bytes, i * 4));
    
        i++;
    
    }
    
    var data = intList.ToArray();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 5, 2019 3:50 PM
    Moderator

All replies

  • Rather than an array, use a list and add to it. If once done you need an array rather than a list use .ToArray() in the list e.g.

    var data = someList.ToArray();

    byte[] bytes = File.ReadAllBytes(fileName); //read binary file
    var intList = new List<int>();
    
    int i = 0;
    while (i < read.length)
    {
    
        intList.Add(BitConverter.ToInt32(bytes, i * 4));
    
        i++;
    
    }
    
    var data = intList.ToArray();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 5, 2019 3:50 PM
    Moderator
  • If you do not need the bytes, then check this approach too:

    int[] Data;

     

    using( var fs = File.OpenRead( filename ) )

    {

           Data = new int[fs.Length / sizeof( Int32 )];

     

           using( var br = new BinaryReader( fs ) )

           {

                 for( int i = 0; i < Data.Length; ++i )

                 {

                        Data[i] = br.ReadInt32( );

                 }

           }

    }

     


    • Edited by Viorel_MVP Tuesday, November 5, 2019 6:38 PM
    Tuesday, November 5, 2019 6:33 PM