-2

everyone I'm a student totally a noob in this of programming just know a litle web programming JS-HTML5, but nothing extraordinary.

I need help with some problems. Thanks if you help, and thanks if not. :D

*. Make a program in C++ that read a text file name: NUMBERS.txt, that file contains any number in each line. teh program haves to reed every number, print it in screen and detect and print too if it's pair or not pair.

*. Another program in C++ that generates N wholes number randomly and stores it in a binary file name: wholes.dat. I aske somebody about this and he told me: "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."; but i really don't get how to code it...

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

This code was sperimenting with fstrea; but's not working. Help!! Thanks for your time.

VolAnd
  • 6,034
  • 3
  • 19
  • 40

1 Answers1

-1

For reading and returning the text you can use getline as well. Just use a string as a container for the text document.

fstream file; string line;

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

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

file.close();

Jonas Aisch
  • 71
  • 10
  • `while (!file.eof())` is bad advice: http://stackoverflow.com/q/5605125/920069 You should also work on the formatting and completeness of your code sample. – Retired Ninja Apr 05 '15 at 08:39