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

  • Home
  • SEARCH
  • 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 8231091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:21:41+00:00 2026-06-07T17:21:41+00:00

code in strfile.cpp: #include <fstream> #include <iostream> #include <assert.h> #define SZ 100 using namespace

  • 0

code in strfile.cpp:

#include <fstream>
#include <iostream>
#include <assert.h>

#define SZ 100

using namespace std;

int main(){
char buf[SZ];
{
    ifstream in("strfile.cpp");
    assert(in);
    ofstream out("strfile.out");
    assert(out);
    int i = 1;

    while(!in.eof()){
        if(in.get(buf, SZ))
            int a = in.get();
        else{
            cout << buf << endl;
            out << i++ << ": " << buf << endl;
            continue;
        }
        cout << buf << endl;
        out << i++ << ": " << buf << endl;
    }
}
return 0;
}

I want to operate all file
but in strfile.out:

1: #include <fstream>
2: #include <iostream>
3: #include <assert.h>
4: ...(many empty line)

I know that fstream.getline(char*, int) this function can manage it,but I want to know how to do this just use the function “fstream.get()”.

  • 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-06-07T17:21:42+00:00Added an answer on June 7, 2026 at 5:21 pm

    Because ifstream::get(char*,streamsize) will leave the delimiter (in this case \n) on the stream, your call never advances and thus it appears to your calling program that you are endlessly reading blank lines.

    Instead you need to determine if a newline is waiting on the stream, and move past it using in.get() or in.ignore(1):

    ifstream in("strfile.cpp");
    ofstream out("strfile.out");
    
    int i = 1;
    out << i << ": ";
    
    while (in.good()) {
        if (in.peek() == '\n') {
            // in.get(buf, SZ) won't read newlines
            in.get();
            out << endl << i++ << ": ";
        } else {
            in.get(buf, SZ);
            out << buf;      // we only output the buffer contents, no newline
        }
    }
    
    // output the hanging \n
    out << endl;
    
    in.close();
    out.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
Code to create new form instance of a closed form using form name I
Code: <?php //initializing script, do not modify session_start(); define('IN_SCRIPT', true); //so that global.php cannot
Code sample for my question: IList<TestDataAnimal> testDataFromDb = this.db.TestDataAnimals.Include(t => t.TestType).Include(t => t.Visit).Include(t =>
Code below sets default values for new row if row is added using form.
Code goes first: class A { public: ... int *foo() const { return _px;
Code: %define x 0x03 x equ 0x03 What's the difference between them?
The code I'm using is: import time print(time.strftime(%H:%M:%S)) In the dynamic shell, this (as
Code: String message = MessageFormat.format(error {0},e); E.g. message: java.text.ParseException: Unparseable date: sdf sf sa
Code : $('#myLink').click(function (e) { e.preventDefault(); ... if (someCondition) { ... code ... }

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.