windows 替代 fgets()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to fgets()?
提问by user62619
Description:
描述:
- Obtain output from an executable
- 从可执行文件中获取输出
Note:
笔记:
- Will not compile, due to fgets() declaration
- 由于 fgets() 声明,不会编译
Question:
题:
- What is the best alternative to fgets, as fgets requires char *?
- Is there a better alternative?
- fgets 的最佳替代品是什么,因为 fgets 需要 char *?
- 有更好的选择吗?
Illustration:
插图:
void Q_analysis (const char *data)
{
string buffer;
size_t found;
found = buffer.find_first_of (*data);
FILE *condorData = _popen ("condor_q", "r");
while (fgets (buffer.c_str(), buffer.max_size(), condorData) != NULL)
{
if (found == string::npos)
{
Sleep(2000);
} else {
break;
}
}
return;
}
回答by sfossen
You should be using the string.getline function for strings cppreference
您应该将 string.getline 函数用于字符串 cppreference
however in your case, you should be using a char[] to read into.
但是在您的情况下,您应该使用 char[] 来读入。
eg
例如
string s;
char buffer[ 4096 ];
fgets(buffer, sizeof( buffer ), condorData);
s.assign( buffer, strlen( buffer ));
or your code:
或您的代码:
void Q_analysis( const char *data )
{
char buffer[ 4096 ];
FILE *condorData = _popen ("condor_q", "r");
while( fgets( buffer, sizeof( buffer ), condorData ) != NULL )
{
if( strstr( buffer, data ) == NULL )
{
Sleep(2000);
}
else
{
break;
}
}
}
回答by MarkusQ
Instead of declaring you buffer as a string declare it as something like:
不是将缓冲区声明为字符串,而是将其声明为:
char buffer[MY_MAX_SIZE]
call fgets with that, and then build the string from the buffer if you need in that form instead of going the other way.
使用它调用 fgets,然后如果需要以这种形式从缓冲区构建字符串,而不是采用其他方式。
The reason what you're doing doesn't work is that you're getting a copyof the buffer contents as a c-style string, not a pointer into the gut of the buffer. It is, by design, read only.
您正在做的事情不起作用的原因是您正在获取缓冲区内容的副本作为 c 样式字符串,而不是指向缓冲区内部的指针。按照设计,它是只读的。
-- MarkusQ
——马库斯Q
回答by Rob Kennedy
You're right that you can't read directly into a std::string
because its c_str
and data
methods both return const pointers. You could read into a std::vector<char>
instead.
您不能直接读入 a 是对的,std::string
因为它的c_str
和data
方法都返回 const 指针。你可以读入 astd::vector<char>
代替。
You could also use the getline
function. But it requires an iostream object, not a C FILE pointer. You can get from one to the other, though, in a vendor-specific way. See "A Handy Guide To Handling Handles" for a diagram and some suggestions on how to get from one file type to another. Call fileno
on your FILE*
to get a numeric file descriptor, and then use fstream::attach
to associate it with an fstream
object. Then you can use getline
.
您也可以使用该getline
功能。但它需要一个 iostream 对象,而不是一个 C FILE 指针。但是,您可以以特定于供应商的方式从一个到另一个。有关如何从一种文件类型转换为另一种文件类型的图表和一些建议,请参阅“处理句柄的便捷指南”。调用fileno
你FILE*
来获取一个数字文件描述符,然后用fstream::attach
它来关联一个fstream
对象。然后你可以使用getline
.
回答by Eric Petroelje
Try the boost library - I believe it has a function to create an fstream from a FILE*
试试 boost 库 - 我相信它有一个功能可以从 FILE* 创建一个 fstream
or you could use fileno() to get a standard C file handle from the FILE, then use fstream::attach to attach a stream to that file. From there you can use getline(), etc. Something like this:
或者您可以使用 fileno() 从 FILE 获取标准 C 文件句柄,然后使用 fstream::attach 将流附加到该文件。从那里你可以使用 getline() 等。像这样:
FILE *condorData = _popen ("condor_q", "r");
std::ifstream &stream = new std::ifstream();
stream.attach(_fileno(condorData));
回答by bert hubert
I haven't tested it all too well, but the below appears to do the job:
我还没有完全测试过,但以下似乎可以完成这项工作:
//! read a line of text from a FILE* to a std::string, returns false on 'no data'
bool stringfgets(FILE* fp, std::string& line)
{
char buffer[1024];
line.clear();
do {
if(!fgets(buffer, sizeof(buffer), fp))
return !line.empty();
line.append(buffer);
} while(!strchr(buffer, '\n'));
return true;
}
Be aware however that this will happily read a 100G line of text, so care must be taken that this is not a DoS-vector from untrusted source files or sockets.
但是请注意,这将很容易读取 100G 的文本行,因此必须注意这不是来自不受信任的源文件或套接字的 DoS 向量。