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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:35+00:00 2026-06-14T17:06:35+00:00

when I use c++ to process a file ,I found there is always a

  • 0

when I use c++ to process a file ,I found there is always a blank line in the end of file .Someone says that vim will append an ‘\n’ in the end of file,but when I use gedit,it also has the same question.Can anyone tell me the reason?

1 #include<iostream> 
2 #include<fstream> 
3  
4 using namespace std; 
5 const int K = 10; 
6 int main(){ 
7         string arr[K];
8         ifstream infile("test1");
9         int L = 0;
10         while(!infile.eof()){
11             getline(infile, arr[(L++)%K]);
12         }
13         //line
14         int start,count;
15         if (L < K){
16             start = 0;
17             count = L;
18         }
19         else{
20             start = L % K;
21             count = K;
22         }
23         cout << count << endl; 
24         for (int i = 0; i < count; ++i)
25             cout << arr[(start + i) % K] << endl;
26         infile.close();
27         return 1;
28 }

while test1 file just:
abcd
but the program out is :
2
abcd

(upside is a blank 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-06-14T17:06:36+00:00Added an answer on June 14, 2026 at 5:06 pm
    while(!infile.eof())
    

    infile.eof() only is true after you tried to read beyond the end of the file. So the loop tries to read one more line than there is and gets an empty line on that attempt.

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

Sidebar

Related Questions

I'm trying to use MapViewOfFile in a 64 bit process on a file that
If there is a process that reads a big file and saves it in
Which .PDB file does Visual Studio use if you use the attach to process
I've been asked to prototype a replacement file transformation process (that currently is a
A found an article ( Extend the Visual Studio Build Process ) that explained
Several resources I have found claim, that GMail File Upload incorporates Flash to display
I found that using matplotlib just to draw a diagram into a file is
I found that my DB.sql has error shown below, i did not process it
I'm trying to use VB.NET to process a very large plain text file (2 GB).
I use process explorer (which is a microsoft tool) on windows XP, and often

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.