Fstream C++ 代码和 Ercise 问题

Fstream c++ Code & Ercise problems

大家我是一名学生,在编程方面完全是菜鸟,只知道一点 web 编程 JS-HTML5,但没什么特别的。

我需要帮助解决一些问题。如果你有帮助,谢谢,如果没有,谢谢。 :D

*。用 C++ 编写一个读取文本文件名的程序:NUMBERS.txt,该文件的每一行包含任意数字。该程序必须读取每个数字,将其打印在屏幕上,并检测并打印是否配对。

*。另一个C++程序,随机生成N个整数,存入二进制文件名:wholes.dat。我问过别人这件事,他告诉我:"You might consider giving rand a look, it would help you to generate random numbers. You can set the limit to start from zero to (a limit). Whole numbers start from zero and go to... Infinity.";但我真的不知道如何编码...

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

int main(void){
    double num(123.456), x; //
    fstream escr_leer("arch4.bin", ios::out | ios::in | ios::binary);
    if( escr_leer ){
        escr_leer.write((char*)(&num), sizeof(num));
        escr_leer.seekg(ios::beg);
        escr_leer.read( (char*)(&x), sizeof(x));
        cout << x << endl;
    }
    else{
        cout << "\nERROR ABRIENDO EL ARCHIVO DE TEXTO\n";
        exit(1);
    }
    escr_leer.close();
    return 0;
} 

此代码正在使用 fstrea 进行实验;但不工作。帮助!!谢谢你的时间。

要阅读和返回文本,您也可以使用 getline。只需使用字符串作为文本文档的容器。

fstream file; string line;

file.open("Nooby.txt", ios_base::in);

while (!file.eof()) { getline(file, 100); }

file.close();