[英]How to use ifstream of C++ to read from a file using length of the data rather than new line?
[英]Using popen as a substitue for “write to file” then “use ifstream to read from it” c++
我是初学者,所以如果问题显而易见,请继续。
当前版本的代码如下所示。 output.txt使用ifstream打开,然后送到Coll类型的对象,因为它理解“理解”生成的output.txt文件的格式。
std::system("./Pogram > output.txt");
Coll inputout;
ifstream ifsout("output.txt");
ifsout >> inputout;
我的目标是摆脱中间的output.txt,并做如下所示。
FILE * f = popen("./Program", "r");
Coll inputout;
f >> inputout;
这会产生以下错误:
error: no match for ‘operator>>’ in ‘f >> inputout’
你能建议任何补救措施吗?
你的问题是popen
只提供了一个FILE *
,我不相信有任何(便携,可靠)的方法将它转换成文件流。 所以你将不得不处理使用fgets
读取一行作为C字符串和stringstream
将其转换为您的类型,或使用fscanf
或类似。
f
的类型为FILE
,没有>>
运算符。 你需要使用像fread()
和fwrite()
。 你也没有获得ifstream借给你的所有奇特类型的转换,如果你要使用FILE
你基本上必须直接读取和写入位。
fread(&inputout, sizeof(Coll), 1, f);
这是从当前文件位置读取内存并将其放入变量inputout
,其大小为Coll x 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.