0

Hey guys , i am trying to write an array into a textfile. Here is the coding...

int main() {
    ofstream myfile("report.txt");
    int g = 0;


int billy [] = {16, 2, 77, 40, 12071};

  for (int n=0 ; n<5 ; n++ )
  {
      g = billy[n];
      myfile << g ;
  }

}

The output which comes out in report.txt is...

162774012071

Well , my expected output in report.txt would be.

16
2
77
40
12071

I tried adding "\n" to my statement like this...

myfile << g  << "\n" ;

but it dosnt seem to work. Can sombody tell me what is wrong with the way i write the statement into the file?

3
Contributors
13
Replies
14
Views
6 Years
Discussion Span
Last Post by Fbody
0

so even with the "\n" it writes them all into the same line? It worked fine for me; You might try endl instead of "\n" ; it does the same thing, but you don't have to call it in a string like with "\n".

0

Try using "endl" instead of '\n'. Also, consider getting rid of the "g" variable, all it really does is take up memory and require extra steps.

int main() {
    ofstream myfile("report.txt");


int billy [] = {16, 2, 77, 40, 12071};

  for (int n=0 ; n<5 ; n++ )
  {
      myfile << billy[n] << endl;
  }

  return 0;
}

[edit]
eesh...... I really need to stop distracting myself while posting...

Edited by Fbody: n/a

0

Weird. i tried using endl; but it still reads all into the same line. IM using netbeans ide 6.8 in windows 7.

Edited by gregarion: n/a

0

The IDE is essentially irrelevant. More important is what compiler are you using? For example, many people say they use Dev-C++ IDE. Well, that's all well and good, but which compiler are you using? Dev-C++ uses the MinGW compiler by default, but it can use others.

This works fine for me on MS-VC++ '08

This is what I get:

16
2
77
40
12071

Edited by Fbody: n/a

0

Yep.

#include <fstream>
using std::ofstream;
using std::endl;

int main()
{
  ofstream myfile("report.txt");

  int billy [] = {16, 2, 77, 40, 12071};

  for (int n=0 ; n<5 ; n++ )
  {
    myfile << billy[n] << endl;
  }

  return 0;
}

Are you sure you are opening the correct output file? Are you opening the produced file on a *nix system after producing it on a Win system (or vice versa)?

Edited by Fbody: n/a

0

Im not sure but does it has to do with the fact i am using windows notepad to create the txt file?

0

I've opened the file in FBIde, notepad, notepad++, VS, and WordPad. They all appear correctly.

I suggest that you delete the file you are opening and see if the program re-creates it when run. If it does not, then we know you were opening the incorrect version of the file.

Edited by Fbody: n/a

0

Post the new file's contents and your new code for us to look at.

You should be able to treat a file stream just like a console stream. I think you should try a different compiler. There may be a bug in yours. Try MinGW or VC++ Express.

Edited by Fbody: n/a

0

Sorry , but im quite new at this . Im not very sure on how i can go about downloading a new compiler. The one i am using is from the Netbeans website tutorial about using C++.

Any instructions i can follow?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.