importing files with strtok

Hi,

I have a problem when I import two files and then do some stuff and outpout the result. Here is my code below. The program works perfectly , there are not syntax error. the problem is that one the files does not import properly because the n1++ is at line 92 .If I put it at line 90. so it will change for each token. the imports works but it creates another problem. the first two lines of my output there added garbage which wasn't

I hope I'm clear enough. I put the whole code so you could understand the logic better

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>


using namespace std;

#define LONGUEUR 8
#define LONGUEUR1 3
#define MARKET 6
#define NAME 50

struct ListeInter 
{
char SymbolTSX[LONGUEUR+1];
char SymbolTSX_1[LONGUEUR1+1];
char USMarket[MARKET+1];
char SymbolUSA[LONGUEUR+1];
char Name [NAME+1];
};

struct ListeAll
{
char SymbolTSX[LONGUEUR+1];
};

void LireFichier(  ListeInter Liste[], int &n)
{

ifstream  fichier("interlisted.txt",ios::in);

while (!fichier.eof())
{
int i=0;
char ligne[80]; 
fichier.getline(ligne,80);

char *tokenPtr;
tokenPtr = strtok(ligne,"\t");

while (tokenPtr != NULL)
{
      if (i == 0)
      {
      strncpy(Liste[n].SymbolTSX, tokenPtr,LONGUEUR);
      strncpy(Liste[n].SymbolTSX_1, tokenPtr,LONGUEUR1);
      tokenPtr = strtok( NULL, "\t");
      i++;
      }   
      if (i == 1)
      {
      strncpy(Liste[n].USMarket, tokenPtr, MARKET); 
      tokenPtr = strtok( NULL, "\t");
      i++;
      }
      if (i == 2)
      {
      strncpy(Liste[n].SymbolUSA, tokenPtr, LONGUEUR); 
      tokenPtr = strtok( NULL, "\t");
      i++;
      }
      if (i == 3)
      {
      strncpy(Liste[n].Name, tokenPtr, NAME); 
      tokenPtr = strtok( NULL, "\t");
      i++;
      }
}
n++;
}
}
void LireFichier1 (ListeAll Liste1[],int &n1)
{
ifstream  fichier1("all.txt",ios::in);

while (!fichier1.eof())
      {
      char ligne[15]; 
      fichier1.getline(ligne,15);
      char *tokenPtr;
      tokenPtr = strtok(ligne,"/");

         while (tokenPtr != NULL)
               {
               strncpy(Liste1[n1].SymbolTSX, tokenPtr, 3);
               tokenPtr = strtok(NULL, "/");
              
               }
       n1++;
      
      }
}
void enleverpoint (ListeInter Liste[],int &n)
{
     int i=0;
     int j=0;
     while (j<n)
     {
     
           char *tokenPtr;
           tokenPtr = strtok(Liste[j].SymbolTSX_1,".");
           
           while ( i <1  )
              {
                    if (i == 0)
                    {
                    strcpy(Liste[j].SymbolTSX_1, Liste[j].SymbolTSX);
                    tokenPtr = strtok(NULL, ".");
                    i++;
                    }
              }
     j++;        
     }
}

void comparercode(ListeInter Liste[], ListeAll Liste1[], int &n,int &n1)
{
ofstream fichier1("resultat.txt", ios::out);
int result =0;
int k=0;
    while (k<n1) 
    {
    int m=0;
          while(m<n)
          {
          result = strncmp(Liste1[k].SymbolTSX, Liste[m].SymbolTSX_1,3);
          if (result ==0)
             {
             fichier1 <<  Liste[m].SymbolTSX  << '\t' << Liste[m].USMarket  << '\t' << Liste[m].SymbolUSA << '\t' << Liste[m].Name  <<'\n'; 
             k++;
             }
             else
              {
              m++;
              }           
          }
    k++;
    }
fichier1.close();       
}

  


int main()
{
int n=0; 
int n1=0;   
ListeInter Liste[178] = {0};


ListeAll Liste1[250] = {0};


LireFichier(Liste,n);    
LireFichier1(Liste1,n1);
enleverpoint(Liste,n);
comparercode(Liste, Liste1, n, n1);

cout << " fin" << endl;
    
return 0;   
}


Why not use std::string and getline to tokenize?
I thought that's what I did . I import the line ( line 82) and then I tokenize (line 84 ) and it works but when there are more than one token , the program only takes the last one because the program overwrite over it because the n1++ is at line 92 but If I put at line 90 , it imports correctly but the output because weird and I don't know why.

result when n1++ is at line 92 ( which I don't want because the import is bad)

AAV NYSE AAV Advantage Oil & Gas Ltd.
ABX NYSE ABX Barrick Gold Corporation
AEM NYSE AEM Agnico-Eagle Mines Ltd.
AGU NYSE AGU Agrium Inc.
ANV NYSE ANV Allied Nevada Gold Corp. J
ARZ NYSE AZK Aurizon Mines Ltd. J

result when n1++ is at line 90 ( import good but see below the result -- see the first two line)

AAVEXE XE AGSL AIMXntage JIN & GasPMZd.
ABX TKOE ABWPT BaWTNck GolXRCorporation
AEM NYSE AEM Agnico-Eagle Mines Ltd.
AGU NYSE AGU Agrium Inc.
ANV NYSE ANV Allied Nevada Gold Corp. J
ARZ NYSE AZK Aurizon Mines Ltd. J

Topic archived. No new replies allowed.