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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:19:24+00:00 2026-05-19T04:19:24+00:00

I am reading a file like this: char string[256]; std::ifstream file( file.txt ); //

  • 0

I am reading a file like this:

char string[256];

std::ifstream file( "file.txt" ); // open the level file.

if ( ! file ) // check if the file loaded fine.
{
    // error
}

while ( file.getline( string, 256, ' ' )  )
{
    // handle input
}

Just for testing purposes, my file is just one line, with a space at the end:

12345 

My code first reads the 12345 successfully. But then instead of the loop ending, it reads another string, which seems to be a return/newline.

I have saved my file both in gedit and in nano. And I have also outputted it with the Linux cat command, and there is no return on the end. So the file should be fine.

Why is my code reading a return/newline?

Thanks.

  • 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-19T04:19:24+00:00Added an answer on May 19, 2026 at 4:19 am

    First leets make sure your input file is good:

    Run the following command and let us know the output:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <iterator>
    #include <fstream>>
    #include <iomanip>
    #include <algorithm>
    
    int main()
    {
        std::ifstream file("file.txt");
        std::cout << std::hex;
    
        std::copy(std::istreambuf_iterator<char>(file),
                  std::istreambuf_iterator<char>(),
    
                  std::ostream_iterator<int>(std::cout, " ")); 
    }
    

    Edit:

    The output was 31 32 33 34 35 20 0A

    Try running this code and see what the output is:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <iterator>
    #include <fstream>>
    #include <iomanip>
    #include <algorithm>
    
    int main()
    {
        std::ofstream file("file.txt");
        file << "12345 \n";
    }
    

    Dump the output of this file and compare it to the original.
    The problem is that different platforms have different line termination sequences. I just want to verify that ‘0x0A’ is the line termination sequence for your platform. Note the line termination sequence is converted into a ‘\n’ when a file is read in text mode and when you output ‘\n’ to a file in text mode it is converted to the line termination sequence.

    Edit 2

    So I have the file: file.txt

    > od -ta -tx1 file.txt
    0000000    1   2   3   4   5  sp  nl                                    
               31  32  33  34  35  20  0a                                    
    0000007
    

    So the file contains 1 line terminated with 0x0A

    Using this program:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <iterator>
    #include <fstream>>
    #include <iomanip>
    #include <algorithm>
    
    int main()
    {
        std::ifstream   file("file.txt");
    
        std::string line;
        while(std::getline(file,line))
        {
            std::cout << "Line(" << line << ")\n";
        }
    }
    

    I get:

    > g++ t.cpp
    > ./a.out
    Line(12345 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I want to open a file for reading, the C++ way. I need to
I have a file in .gz format. The java class for reading this file
A common piece of code I use for simple string splitting looks like this:
I would like to read a (fairly big) log file into a MATLAB string
I'm reading in a line from a file (char by char, using fgetc()), where
Are there any good configuration file reading libraries for C\C++ that can be used
I am using the following method for reading Csv file content: /// <summary> ///
I am reading a binary file into a parsing program. I will need to

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.