标签: getline

符合ANSI C的实现是否可以在其标准库中包含其他功能?

是否符合ANSI C标准的实现允许在其标准库中包含其他类型和函数,超出标准列举的那些类型和函数?(理想的答案将参考ANSI标准的相关部分.)

我特别要求,因为Mac OS 10.7 getline在stdio.h中声明了该函数,即使使用该-ansi标志使用gcc或clang进行编译也是如此.这打破了几个定义自己getline功能的旧程序.这是Mac OS 10.7的错吗?(getlineMac OS 10.7上的手册页说明getline符合2008年发布的POSIX.1标准.)

编辑:为了澄清,我发现奇怪的是,在Mac OS 10.7上的ANSI C89程序中包含stdio.h也会引入该getline函数的声明,因为getline它不是K&R(可能是ANSI)中所列举的函数之一. stdio.h中.特别是,尝试编译noweb:

gcc -ansi -pedantic    -c -o notangle.o notangle.c
In file included from notangle.nw:28:
getline.h:4: error: conflicting types for ‘getline’
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
Run Code Online (Sandbox Code Playgroud)

它是否是Mac OS 10.7中的一个错误,getline即使在编译ANSI C89标准时也包括stdio.h中的声明?

c standards ansi getline osx-lion

9
推荐指数
1
解决办法
1516
查看次数

基本字符串输入

我刚刚遇到了一些允许用户在命令提示符下输入字符串的代码.我知道他们做了什么,这一切都很棒.但是我对cin和getline()函数有疑问.

string name ;
cout << "Please enter your full name: " ;
cin >> name ;
cout << "Welcome " << name << endl ;
cout << "Please enter your full name again please: " ;
getline(cin , name) ;
cout << "That's better, thanks " << name << endl ;
return 0 ;
Run Code Online (Sandbox Code Playgroud)

现在当这是输出时,我得到了一些东西:(使用约翰史密斯作为输入)

Please enter your full name: john smith
Welcome John
Please enter your full name again: That's better thanks Smith
Run Code Online (Sandbox Code Playgroud)

我理解为什么会发生这种情况,getline仍在从输入缓冲区读取,我知道如何修复它.我的问题是,为什么在"请再次输入您的全名"之后没有换行?当我将代码更改为:

string name ;
cout …
Run Code Online (Sandbox Code Playgroud)

c++ string input cin getline

9
推荐指数
1
解决办法
3万
查看次数

使用getline()而不设置failbit

是否可以getline()在没有设置的情况下读取有效文件failbit?我想使用,failbit以便在输入文件不可读时生成异常.

以下代码始终basic_ios::clear作为最后一行输出- 即使指定了有效输入.

test.cc:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
    ifstream inf;
    string line;

    inf.exceptions(ifstream::failbit);
    try {
        inf.open(argv[1]);
        while(getline(inf,line))
            cout << line << endl;
        inf.close();
    } catch(ifstream::failure e) {
        cout << e.what() << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

input.txt中:

the first line
the second line
the last line
Run Code Online (Sandbox Code Playgroud)

结果:

$ ./a.out input.txt 
the first line
the second line
the last line
basic_ios::clear
Run Code Online (Sandbox Code Playgroud)

c++ file-io exception getline eof

8
推荐指数
1
解决办法
3135
查看次数

awk解决方案,用于将当前行与下一行进行比较,并根据条件打印其中一行

我有一个看起来像这样的输入文件(第一列是位置编号,第二列是应该随时间增加的计数):

1       0
1       2
1       6
1       7
1       7
1       8
1       7
1       7
1       9
1       9
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10
Run Code Online (Sandbox Code Playgroud)

并且我想修复它看起来像这样(替换计数随先前计数减少):

1       0
1       2
1       6
1       7
1       7
1       8
1       8
1       8
1       9
1       9
1       10
1       10
1       10 …
Run Code Online (Sandbox Code Playgroud)

awk getline

8
推荐指数
1
解决办法
1万
查看次数

C++:使用ifstream和getline();

检查这个程序

ifstream filein("Hey.txt");
filein.getline(line,99);
cout<<line<<endl;
filein.getline(line,99);
cout<<line<<endl;
filein.close();
Run Code Online (Sandbox Code Playgroud)

Hey.txt文件中有很多字符.超过1000

但我的问题是为什么我第二次尝试打印线.它不打印?

c++ file-io fstream getline

8
推荐指数
2
解决办法
8万
查看次数

为什么当异常掩码未设置为eofbit时,getline()会抛出'std :: ios_base :: failure'?

请考虑以下代码:

    ifstream in;
    try {
        in.exceptions ( ifstream::failbit | ifstream::badbit );
        in.open(pConfLocation);
    } catch ( ifstream::failure e ) {
        throw std::runtime_error("Can't open configuration file\n");
    }

    vector<string> lns;
    string s;

    in.clear();
    while ( !in.eof() ){
        getline( in, s );
        boost::algorithm::trim(s);
        lns.push_back( s+='\n');
    }
Run Code Online (Sandbox Code Playgroud)

所以:

  1. 我为try-catch块的需要设置了以下"异常掩码"(ifstream :: failbit | ifstream :: badbit).该文件打开没有问题.
  2. 在while {}块中,我知道在文件末尾将设置eofbit.

异常掩码是所有流对象的内部值,指定哪些状态标志在设置时必须抛出异常.

我没有设置ifstream :: eofbit,但无论如何在运行时出现以下错误:

terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_ios::clear
The program has unexpectedly finished.
Run Code Online (Sandbox Code Playgroud)

我无法理解这种行为.我试图在{}之前使用in.clear() …

c++ exception getline

8
推荐指数
1
解决办法
9908
查看次数

如何正确释放与getline()函数相关的内存?

我刚刚开始编程并有一个初学者的问题,我想编写一个函数来逐行读取未知长度的文件.因为我不知道每行的长度所以我使用了getline()函数:

void readDict(FILE *dict_file){
  //Read dic 
  char *line;
  size_t len = 0, read;
  while((read = getline(&line, &len, dict_file))!=-1){
    check(line); 
  }
  free(line);
  return;
}
Run Code Online (Sandbox Code Playgroud)

因为getline()有点类似于malloc()realloc()字符串,所以如果我继续使用这个函数来读取很多未知长度的行,我会得到内存泄漏还是内存不足?

c pointers getline dynamic-memory-allocation

8
推荐指数
1
解决办法
5507
查看次数

getline检查行是否为空格

有没有一种简单的方法来检查一条线是否为空.所以我想检查它是否包含任何空格,如\ r \n\t和空格.

谢谢

c whitespace getline

7
推荐指数
1
解决办法
2万
查看次数

getline()带有文件描述符而不是文件指针

据我所知,没有getline()与文件描述符相同的libc ,而不是使用FILE *.

是否有(技术)原因?

c posix libc getline

7
推荐指数
1
解决办法
5184
查看次数

为什么没有std :: from_string()?

为什么没有

template <typename T>
T std::from_string(const std::string& s);
Run Code Online (Sandbox Code Playgroud)

在C++标准?(看看有什么std::to_string()功能,我的意思是)

PS - 如果您对未被采纳/考虑的原因有所了解,只需回答/评论并说出它是什么,而不是低估.我实际上并未提出将其纳入标准的提案.

c++ getline text-parsing istream

7
推荐指数
1
解决办法
3193
查看次数