writing to file and newline

so am writing to file..and i want to be inserting newlines into the output am writing to file...but using std::endl or '/n' has no effect what so ever...how do i do that? am gonna post a similar code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  std::fstream file;
	file.open("oppose.txt", std::ios::in | std::ios::out | std::ios::trunc);
	if (file.is_open()){

        int k = 0;
for (int i = 0; i < f.size(); i++)
{
     if (k <= 70)
     {
         file<<f[i]<<" "; k++;
     }
     else { k= 1; file<<'/n'<<'/n'<<'/n'<<f[i]<<"       ,";// i tried to leave all this space to see if it will show in the txt file...nothing...
                       }
}
	std::cout<<"writing to file successful!";
	}


	else
        {std::cout<<"could not write to file...";}
Try '\n' - backslash not forward slash.

std::endl should work also.
damn and the compiler is not telling me anything? well am gonna try it now...endl is surly not working..still writing everything in a straight line!!! i never use the backlash n in quotes thing...so endl is what i do by default..not working..dont know why
damn and the compiler is not telling me anything?

If you are using gcc and enable all warnings (-Wall) you will receive a warning about using a multicharacter constant.

http://en.wikipedia.org/wiki/C_syntax#Character_constants (second paragraph)
still not working..am posting a very similar thing to the bigger picture...
I COMMENTED IN THE CODE BELOW WHERE THE PROBLEM ARISES...THANK YOU...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>

int main (){
std::vector<std::vector<int> > test = {{1,2,3,0,3},
                                      { 0,1,3,0,0},
                                      {0,0,0,0,0},
                                      {1,2,3,0,3},
                                     {0,3,4,4,4},};
/*
for (int i = 0; i< 5; i++)
for (int k = 0; k <3; k++)
    std::cout<<test[i][k];
std::cout<<std::endl;
std::cout<<test[4][3]; */

std::vector<int> f = {1,2,3,4,5,6,7,8,9,10};
int n = 0;
for (int i = 0; i < f.size(); i++)
{
     if (n <= 2)
     {
         std::cout<<f[i]<<", "; n++; 
     }
     else { n= 1; std::cout<<std::endl<<std::endl<<f[i]<<", ";
                       }
}

std::fstream file;
	file.open("oppose.txt", std::ios::in | std::ios::out | std::ios::trunc);
	if (file.is_open()){

        int k = 0;
for (int i = 0; i < f.size(); i++)
{
     if (k < 70)
     {
         file<<f[i]<<"    "<<std::endl; k++;// ASKING FOR NEWLINE HERE GIVES GOOD RESULT
     }
     else { k=1;
 file<<'n'<<'\n'<<'\n'<<f[i]<<" ,"; //ALL THE NEWLINES AM ASKING HERE ARE IGNORED! WHY?
                       }
}
	std::cout<<"writing to file successful!";
	}


	else
        {std::cout<<"could not write to file...";}

return 0;
}
Since your vector f only contains 10 items k is always less than 70 so the else condition is never met.

Try changing line 39 to k<5 and try it.
Topic archived. No new replies allowed.