[HELP]Write in files

Hello guys.
I need a help with writing on files.

I have a file with a list of numbers(lista.txt) that i want to swap to another, like this:
0123;4567

and a file(teste.txt) that i want to swap those numbers, like this:

00000000012399999999

so it'll be:
00000000456799999999

and i need to write those lines in the file "listanova.txt", but the program crashes when i run it...

please, someone help me...

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
#include <iostream> 
#include <fstream> 
#include <string>

using namespace std; 
int main()
{ 
  string linha; 
  string aux;
  int auxcount = 0;
  ifstream arquivoImportacao ("D:\\teste.txt");  
  ifstream arquivoLista ("D:\\lista.txt"); 
  fstream listanova;
  listanova.open("D:\\listanova.txt");
  string lista[100][2];
  
  string func;
  int procura = 0;
  
  while(! arquivoLista.eof() ) 
  { 
    getline(arquivoLista, aux); 
    
    lista[auxcount][0] = aux[0];
    lista[auxcount][0] += aux[1];
    lista[auxcount][0] += aux[2];
    lista[auxcount][0] += aux[3];
    lista[auxcount][1] = aux[5];
    lista[auxcount][1] += aux[6];
    lista[auxcount][1] += aux[7];
    lista[auxcount][1] += aux[8];
    
    auxcount++;
  } 
  while(!arquivoImportacao.eof() ) 
  { 
    getline(arquivoImportacao, linha); 
    func = linha[8];
    func += linha[9];
    func += linha[10];
    func += linha[11];
    while(func != lista[procura][0]){
               procura++;
               if(procura > 100) break;
    }
    if (lista[procura][1] != "") cout << "O funcionário " << func << " nao foi encontrado" << endl;
    else {
         linha[8]  = char(lista[procura][1][0]); //the program crashes here
         listanova << linha << endl;
    }
    procura = 0;
  } 

  arquivoImportacao.close(); 
  listanova.close();
  system("PAUSE"); 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  while(!arquivoImportacao.eof() ) 
  { 
    getline(arquivoImportacao, linha); 
    func = linha[8];
    func += linha[9];
    func += linha[10];
    func += linha[11];
    while(func != lista[procura][0]){
               procura++;
               if(procura > 100) break; /// should be > 99
    }
 /// if func is not found in lista, procura will be > 99 here and out of bounds. You need to add code to stop going to next line. 
    if (lista[procura][1] != "") cout << "O funcionário " << func << " nao foi encontrado" << endl;
    else { /// So if lista[procura][1] == "" you try to update linha...
         /// lista[procura][1][0] does not exist since you have an empty string
         linha[8]  = char(lista[procura][1][0]); //the program crashes here
         listanova << linha << endl;
    }
    procura = 0;
  } 
Last edited on
problem solved by following the instructions

1
2
3
4
5
6
7
8
9
while(func != lista[procura][0]){
               procura++;
               if(procura > 99) {
                          listafim = 1;
               }
               if(listafim == 1) break;
    }

    if (listafim == 1) cout << "O funcionário " << func << " nao foi encontrado" << endl;



amazing, perfect, works perfectly

so much thank you!
Last edited on
Topic archived. No new replies allowed.