String wrap

Sorry for my English, and code. I'm just studying both.

Problem is with text wraping by words. I mean single word in single line.
We have file with text (for example: in data.txt is written "I do not have smth.") I need to write it to rez.txt every word in new line. I cannot find nessesary operator. Please, teach me. Compiler is DEV C++ 4.9.9.2. Thanx a lot.

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

main()
{
      char eil[50][50],s;
      int n, i=0;
      
      ifstream outfile ("data.txt");
       while (!outfile.eof())
       {
             outfile.getline (eil[i],50);
             i++;
       }
       n=i;
       
       for (i=0;i<n;i++)
       {
           cout<<eil[i]<<endl;
       }
       outfile.close(); 
      
      ofstream infile ("rez.txt");
      for (i=0;i<n;i++)
      {
          infile <<eil[i]<<endl;
      }
          infile .close();  
      
    system("PAUSE");
}
Last edited on
Hi, I too started to learn File I/O and I think what you should do is just make a string variable in which you'll store the new text. Read the first text in a different string variable and make a while loop or some other and go through each index element of string. Add those elements (characters) to that final string variable and if your current element is a space just add \n sign to your final string.
Also, I think there is a string function called replace or something. I don't know. After you finish with your final string variable just output it to your file rez.txt! Hope this helps! :)
you can store the words from 'data.txt' in a string using the >> operator, then write that word and a newline character ( or what makes a new line on your computer ) using the << operator
Thanx a lot :) That was more easy then I thought :) There is a body:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

main()
{
      string  word;
      
      ifstream outfile ("data.txt");
      ofstream infile ("rez.txt");
      
      while (!outfile.eof())
      {
            outfile>>word;
            cout<<word<<endl;
            infile<<word<<endl;
      }
            infile.close();
            outfile.close();
      
    system("PAUSE");
}
Last edited on
Topic archived. No new replies allowed.