Search code examples
c++ofstream

ofstream will not write to a text file


I am using Dev C++ to write to a text file using ofstream but it does not work, I have made a text file in dev c++and saved it and on another source file I wrote the following code:

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

int main(){

srand(time(0));
ofstream out(“hello.txt”);

for(int i=0;i<100;i++)
          out << rand()%1000 << “ “;
out.close();


return 0;
}

However, when I try to compile this code I get an error and it highlights the following in red:

ofstream out(“hello.txt”);

It says hello undeclared.

The tutorials that I am following are from youtube and the programmer is using a Linux operating system, he is using g++, will the code still be the same on all operating systems? because I am using windows 7.


Solution

  • However, when I try to compile this code I get an error and it highlights the following in red:

    Your quotes are "smart quotes". See how they're all bendy (“”), instead of straight ("")?

    Use normal, straight quotation marks.

    Do not program in Word. You may also have to change a Windows setting as I vaguely recall that some keyboard drivers have a smart quotes mode (which boggles the mind).


    The tutorials that I am following are from youtube and the programmer is using a Linux operating system, he is using g++, will the code still be the same on all operating systems? because I am using windows 7

    For standard code like this yes, absolutely, by design.

    When you start playing with OS-specific functionality (i.e. anything from POSIX or the Windows API) then it gets more complex, but you're not there yet.

    And I can't recommend enough that you put down your "tutorials" and get a real C++ book.

    (It may even be that the smart quotes came from the tutorial website itself! If so, you should inform the author immediately.)