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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:59:46+00:00 2026-05-28T13:59:46+00:00

i wanna read all the lines from a text so am doing this int

  • 0

i wanna read all the lines from a text so am doing this

int main(){
    fstream fs("test.txt",fstream::in|fstream::ate);
    int length = fs.tellg();
    std::vector<char> buffer(length);
    fs.seekg(0, ios::beg);
    fs.read(buffer.data(), length);

    int newlen= 0;
    int ptrSeek = 0;

    while(buffer.data()[ptrSeek] != 0){
        ptrSeek++;
        newlen++;
        if(ptrSeek == buffer.size()) { break;}
    }

    std::vector<char> temp(newlen,0);
    memcpy(&temp[0],&buffer[ptrSeek-newlen],newlen);

}

test.txt:

this is a test
this is a test

so when it reads it, it reads it like this

[t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t] [ ] [t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t]

how can i know it start reading from the next line?

  • 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-28T13:59:46+00:00Added an answer on May 28, 2026 at 1:59 pm

    You can check against \n to know if the character is a newline.

    However, in your case, I would suggest you to high-level function, such as std::getline which reads a single line at a time, saves you much of the labor you’re doing manually.

    The idiomatic way to read line would be as:

    int countNewline= 0;
    std::ifstream fs("test.txt");
    std::string line;
    while(std::getline(fs, line))
    {
          ++countNewline;
          //a single line is read and it is stored in the variable `line`
          //you can process further `line`
          //example
          size_t lengthOfLine = line.size();
          for(size_t i = 0 ; i < lengthOfLine ; ++i)
              std::cout << std::toupper(line[i]); //convert into uppercase, and print it
          std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna do read all lines from txt file and I use to it
I wanna read text between <p></p> tags which comes from database field. I think
I wanna read feed entries and I'm just stuck now. Take this for example
I have a file and I don't wanna take a copy from this file.
When I use ifstream to read file, I loop over all lines in the
Working on a school project, the program is supposed to read from a text
in this i write the cookie value in controller file. i wanna read that
I wanna get the Timedate value from another page using request.querystring and then use
I wanna stop the reading of my text input file when the word synonyms
I wanna add server controls by using javascript. The main purpose of why I

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.