0

very simple program, not sure why it isn't working:

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

int main ()

{
ofstream myfile ("test.txt");
if (myfile.is_open()) 
{
    for(  int i = 1;  i < 65535;  i++  )

     {
     myfile << ( "<connection> remote 208.211.39.160 %d udp </connection>\n", i );
     }
    myfile.close();
}
  return 0;
}

Basically it should print that sentence 65535 times, and then save it to a txt file. But the txt file just has a list of numbers from 1 to 65535, no words or formatting. Any ideas? Thanks for help.

user1448260
  • 263
  • 1
  • 7
  • 16
  • 5
    The comma operator: Causing confusion since who knows when! – chris Dec 14 '12 at 08:42
  • Read here how comma operator is used. http://stackoverflow.com/questions/12824378/how-is-the-comma-operator-being-used-here/12824426#12824426 – Coding Mash Dec 14 '12 at 08:49
  • Thanks it works now, however i made a mistake in that i can only have up to 64 numbers, and they need to be random between 10001 and 65535. Anyone know? – user1448260 Dec 14 '12 at 09:02

5 Answers5

5

If you want to concatenate the output, just pipe your data into two << operators, as such:

myfile << "<connection> remote 208.211.39.160 %d udp </connection>\n" << i;

Note that interpolation does not work in that case, so if you want to put your i variable into the middle of the string, you have to either split it by hand:

myfile << "<connection> remote 208.211.39.160 " << i << " udp </connection>\n"

Or apply some sort of other interpolation formatting before outputting it.

The problem

The problem exists in your code because in C++, (a, b) (the comma operator) returns b. So, in your code it meant you just wrote i to a file.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
1

change

myfile << ( "<connection> remote 208.211.39.160 %d udp </connection>\n", i );

to

myfile << "<connection> remote 208.211.39.160 " << i << " udp </connection>\n";
billz
  • 44,644
  • 9
  • 83
  • 100
1

Try the following:

myfile << "<connection> remote 208.211.39.160 %d udp </connection>\n" << i;

Basically, myfile << (str , i) means "evaluate (str , i) and write the result of evaluation to ostream myfile".

The result of ( "<connection> remote 208.211.39.160 %d udp </connection>\n", i ) evaluation is equal to i

Take a look to comma operator description: http://en.wikipedia.org/wiki/Comma_operator

werupokz
  • 746
  • 6
  • 4
0

You are using printf syntax to write using ofstream. Others have explained why it doesnt work. To fix it do the following

myfile << "<connection> remote 208.211.39.160"<<i<<"udp </connection>\n";

or if you want to go C style

printf( "<connection> remote 208.211.39.160 %d udp </connection>\n", i ); //fprintf to write to file
Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

Looks like you are trying to "printf" and stream...

I think this is more like what you want:

myfile << "<connection> remote 208.211.39.160 " << i << " udp </connection>"<<std::endl;
Russ Freeman
  • 1,480
  • 1
  • 8
  • 6