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

The Archive Base Latest Questions

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

I think I should begin by saying that I’ve just installed linux(debian) in my

  • 0

I think I should begin by saying that I’ve just installed linux(debian) in my pc and have zero foreknowledge about doing things in linux. This problem is probably due to some really simple thing.

The relevant part of the code is similar to this:

ifstream stockNames("names.txt");

    while (!stockNames.eof())
    {
        string snline;
        getline(stockNames,snline);
        cout << snline << endl;
        .
        .
        .
    }

this should print the first line of the file ‘names.txt’. Instead it prints an empty line. And when I try to use snline as an input in another function I get the error ‘Segmentation Fault’. How should I modify my code to do this? Is there any difference in usage of the ifstream in linux? Cause in windows the code works just fine


I’ve written the simple code below

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, const char *argv[])
{
    string dos = "names.txt";
    ifstream stockNames(dos.c_str() );

    string snline;
    while (getline(stockNames,snline))
    {
       cout << snline << " ";

    }
    return 0;
}

content of names.txt is

ABC

DEFG

HBO

instead of showing those line, cout << snline produces nothing


One more update: I’ve written two more codes.

(1)

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, const char *argv[])
{
    cout << "program has initiated" << endl;

    ifstream stockNames("names.txt");

    if( !stockNames )
        cout << "unable to open" << endl;

    string snline;
    while (getline(stockNames,snline))
    {
        cout << snline << endl;

    }
    return 0;
}

Result is as I desired. First ‘program has initiated’, then ABC, DEFG, HBO in different lines. But when I change this part

        cout << snline << endl;

as

        cout << snline << " hey" << endl;

Then ABC DEFG HBO does not appear and instead the only output is ” hey”.

This is crazy, how can this be??

btw I tried to make a debug with ddd and when I check the variable snline, ddd prints the following line
(gdb) print snline
$2 = {static npos = 4294967295, _M_dataplus = {> = {<__gnu_cxx::new_allocator> = {}, }, _M_p = 0x804c1a4 “ABC\r”}}

new mini update: when I change the relevant line as “cout << snline << ” ” << endl;” what’s printed out is ‘ BC’ ‘ FGH’ ‘ BO’ in seperate lines. Why does << operator overwrites over snline??

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

    You have a dos file which uses \r\n at the end of each line. Linux doesn’t recognise \r as part of the line ending so it gets included in the snline string. \r causes the next thing printed to appear at the beginning of the line so ” hey” overwrites the stock names you were expecting to see.

    Try

    cout << snline << " " << endl;
    

    and you’ll see what I mean

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

Sidebar

Related Questions

I am struggling with something that I think should be easily (ish). I have
Ok, I think before I start I should just say that I'm a complete
I have a small problem with how should i think a... problem. I want
Let me begin by saying this; I know that similar questions exist, but they
I have the following code which I think should work to force the keyboard
I think title should be good enough.
I think this should be easy, but it's evading me. I've got a many-to-many
I think I should mention I'm trying to get Entity Framework\SQL server to do
Do you guys think internationalization should only done at the code level or should
What are the expert debugging hints for your favourite language, you think everyone should

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.