Linux | C ++中的分段故障 - 由于函数ifstream [英] Linux | Segmentation Fault in C++ - Due to the function ifstream

查看:209
本文介绍了Linux | C ++中的分段故障 - 由于函数ifstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我应该开始说,我刚刚安装linux(debian)在我的电脑,没有预知在linux做的事情。这个问题可能是由于一些非常简单的事情。

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;
        .
        .
        .
    }

这应该打印文件names.txt的第一行。相反,它打印一个空行。当我尝试使用snline作为另一个函数中的输入时,我得到错误'Segmentation Fault'。我应该如何修改我的代码来做到这一点? ifstream在linux中的使用有什么不同吗?因为在Windows代码工作正常

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

我写了下面的简单代码

#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;
}

names.txt的内容是

content of names.txt is

ABC

DEFG

HBO

而不是显示那些行,cout< snline不生成任何东西

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;
}

结果是我想要的。第一个程序已启动,然后ABC,DEFG,HBO在不同的行。但是当我改变这个部分

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;

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

然后ABC DEFG HBO不出现,而只有输出hey。

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

这是疯了,这怎么可能?

This is crazy, how can this be??

btw我试图使用ddd进行调试,变量snline,ddd打印以下行
(gdb)print snline
$ 2 = {static npos = 4294967295,_M_dataplus = {> = {< __ gnu_cxx :: new_allocator> = {},},_M_p = 0x804c1a4ABC\r}}

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:当我将相关行更改为cout<< << endl;打印出来的是BCFGHBO在单独的行。为什么<<操作符覆盖snline?

推荐答案

您有一个dos文件,使用\r\\\
在每行的结束。 Linux不能识别\r作为行结束的一部分,因此它被包括在snline字符串中。

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.

尝试

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

您会看到我的意思

这篇关于Linux | C ++中的分段故障 - 由于函数ifstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆