Open, Edit, Write textfile

Hello,

I already did some programming with C++ by implementing different formulas in dll files and so I want to use C++ for other operations. Now we have to deal with large textfiles with million of lines (numbers) in the format:
2,3,4
5,4,6
4,7,9
...
...
I want to open the text file (this is no problem), delete for example every 10th line, and write the results to a new text file.
Can someone give me a short example code for this?
Thank you vey much,

Coupland
You need to first include fstream & string header files.. then you need to create an ifstream and then to open the file, check if it is open, and if it is open start manipulating the file gathering the informations you require (with getline() for lines) and when you're done close that file then initialize the ofstream and open the file where you want to write the result then close this one too....

Here is an example of use:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream file;
ofstream result;
string line, address, *source;
int n = 0, l = 1, m;

int main()
{
     while (true)
     {
          cout << "Enter the file you wish to open: ";
          cin >> address;
          file.open(address);

          if (file.is_open()) {
               while (!file.eof()) {
                    getline(file, line);
                    n++;
               }

               for (int x=0 ;; x+=10) {
                    if (n == x) {
                         m = n-(n/10);
                         source = new string[m];
                         break;
                    }
                    else if (n < x) {
                         m = (x-10)/10;
                         source = new string[m];
                         break;
                    }
               }

               for (int x=0; x!=n; x--) {
                    if (l != 10) {
                         getline(file, source[x]);
                         l++;
                    }
                    else {
                         getline(file, line);
                         l = 0;
                    }
               }
          }
          else {
               cout << "An error occured .... Please Retry!" << endl;
               continue;
          }

          file.close();
          cout << "Enter the file you wish to output the result: ";
          cin >> address;

          result.open(address);
          
          if (result.is_open()) {
               for (int x=0; x!=m; x++) {
                    result << source[x];
               }
          }
          else {
               cout << "An error occured .... Please Retry!" << endl;
               continue;
          }

          result.close();
          cout << "Process completed" << endl;
          break;
     }

     delete [] source;
     return 0;
}


Hope it helps! (for more go to http://www.cplusplus.com/doc/tutorial/files/)
Last edited on
Topic archived. No new replies allowed.