Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 835833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:53:10+00:00 2026-05-15T04:53:10+00:00

I need a fast and efficient method to read a space separated file with

  • 0

I need a fast and efficient method to read a space separated file with numbers into an array. The files are formatted this way:

4 6
1 2 3 4 5 6
2 5 4 3 21111 101
3 5 6234 1 2 3
4 2 33434 4 5 6

The first row is the dimension of the array [rows columns]. The lines following contain the array data.

The data may also be formatted without any newlines like this:

4 6
1 2 3 4 5 6 2 5 4 3 21111 101 3 5 6234 1 2 3 4 2 33434 4 5 6

I can read the first line and initialize an array with the row and column values. Then I need to fill the array with the data values. My first idea was to read the file line by line and use the split function. But the second format listed gives me pause, because the entire array data would be loaded into memory all at once. Some of these files are in the 100 of MBs. The second method would be to read the file in chunks and then parse them piece by piece. Maybe somebody else has a better a way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:53:10+00:00Added an answer on May 15, 2026 at 4:53 am

    How about:

        static void Main()
        {
            // sample data
            File.WriteAllText("my.data", @"4 6
    1 2 3 4 5 6
    2 5 4 3 21111 101
    3 5 6234 1 2 3
    4 2 33434 4 5 6");
    
            using (Stream s = new BufferedStream(File.OpenRead("my.data")))
            {
                int rows = ReadInt32(s), cols = ReadInt32(s);
                int[,] arr = new int[rows, cols];
                for(int y = 0 ; y < rows ; y++)
                    for (int x = 0; x < cols; x++)
                    {
                        arr[y, x] = ReadInt32(s);
                    }
            }
        }
    
        private static int ReadInt32(Stream s)
        { // edited to improve handling of multiple spaces etc
            int b;
            // skip any preceeding
            while ((b = s.ReadByte()) >= 0 && (b < '0' || b > '9')) {  }
            if (b < 0) throw new EndOfStreamException();
    
            int result = b - '0';
            while ((b = s.ReadByte()) >= '0' && b <= '9')
            {
                result = result * 10 + (b - '0');
            }
            return result;
        }
    

    Actually, this isn’t very specific about the delimiters – it’ll pretty much assume that anything that isn’t an integer is a delimiter, and it only supports ASCII (you use use a reader if you need other encodings).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of phone numbers, and I need a fast way to
I need a very fast SQLite database acces. Setting parameters this way: PRAGMA synchronize
I need a fast, efficient way to randomly return to me the names of
Although I know this isn't very efficient, I need to get this working fast
I need a fast way to count the number of bits in an integer
I'm working with Android and I really need a fast way to get a
How can i manage user permission? i need a fast method to manage users
I need to check in some fast way if there is any text nodes
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I need a very fast method to detect if an image is empty. Im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.