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 7051993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:19:49+00:00 2026-05-28T03:19:49+00:00

int j = (1024 * 1024); // = 1 megabyte char[] buffer = new

  • 0
                int j = (1024 * 1024); // = 1 megabyte
                char[] buffer = new char[j];
                int charsRead = 0;
                while ((charsRead = sr.Read(buffer, 0, buffer.Length)) > 0)
                {
                    string john = new string(buffer, 0, charsRead);
                    sw.WriteLine(john);                        
                }

This is my first experience with using a buffer, and the above code does what I want, EXCEPT for the fact that the end of the buffer does not coincide with the end of the lines in the text file being read from. This results in what you see below. Keep in mind that because each line in the source file is potentially a different length, the break doesn’t always occur in the same location in the line:

john likes to farm cattle
john likes to farm beetles
john likes to farm rabbits
john likes to farm carrots
john likes to farm b      <---1MB buffer ends here
ears                      <---new 1MB buffer begins here
john likes to farm antelope
john likes to farm rabies
john likes to farm lions

So is there a way to have a buffer of a specified size (1MB in this example), but only up to the end of the last line before 1MB is reached (so the buffer would most likely always be slightly less than 1MB in size)? I’m guessing part of that process would involve defining what exactly a line is (luckily I know how to do this now), but after that I don’t know what I would need to do.

The only solution I can think of would be to go through after the contents of the buffer have been written to the file and search for incomplete lines and re-join them with the lines they were broken from. This seems really inefficient though.

edit: I forgot to include the format of the source file being read from:

john likes to farm cattle
john likes to farm beetles
john likes to farm rabbits
john likes to farm carrots
john likes to farm bears
john likes to farm antelope
john likes to farm rabies
john likes to farm lions
  • 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-28T03:19:49+00:00Added an answer on May 28, 2026 at 3:19 am

    The most obvious solution (in my opinion) would be to have the strings in your buffer contain the newline (and keep it when they are read) and use Write instead of WriteLine.

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

Sidebar

Related Questions

int length = strlen(src); char *structSpace = malloc(sizeof(String) + length + 1); String *string
Consider this in C#: int[] a = new int[1024]; All integers will be set
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
I'm curious about the following code: class MyClass { public: MyClass() : _myArray(new int[1024])
I want to open a text file (see below), read the first int in
int x = n / 3; // <-- make this faster // for instance
int main(void) { std::string foo(foo); } My understanding is that the above code uses
int bufferlength = 12488; int pointer = 1; int offset = 0; int length
I am new to network programming, and have been learning this by writing small
I have 4 int constants : const int a1 = 1024; const int a2

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.