Problem in a function when i pass THE string

HELLOOOO

i have a stange problem.. hoping you can help me.
this is the main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char * argv[]){
    
    if (!argc>2) {cout<<"\nPochi argomenti\n";system("pause>nul");exit(1);}
    const int dim_comand=1000;
    
    int nfile=0, tmp=0;//della riga
    char riga[dim_comand];
    char *dir=new char[strlen(argv[1])];
    char *filtro=new char[strlen(argv[2])];
    char *valore;
    
    char comand[dim_comand];//per comandi sys
    ifstream dirfile;
    
   //legge cartella e nome del filtro file
        strcpy(dir, argv[1]);
    strcpy(filtro, argv[2]);
    
    //creazione file dir
system(set("dir", dir));
system(comand_dir(dir, "%temp%\\dirfile.txt"));
    delete[] dir;

this is the function in which the running stops for an error

1
2
3
4
5
6
7
8
9
10
    char* set (char * var, char* valore){
          #define add(x) strcat(comando, x)

:::HERE ERROR:::
          char* comando=new char[1000]; strcpy(comando, ""); 
          add("set "); add(var); add("="); add(valore);

          #undef add
          return comando;
}


when i pass parametres in "filtro" as ":c:?.at?"the program crash.
even with ":c:?.bat"

WHY!?!??!?!?
that's make no sense... the variable doeas't SEE THE FUNCTION!!

THIS IS THE ERROR DETAILS:
Firma problema:
  Nome evento problema:	APPCRASH
  Nome applicazione:	1.exe
  Versione applicazione:	0.0.0.0
  Timestamp applicazione:	559553ae
  Nome modulo con errori:	ntdll.dll
  Versione modulo con errori:	6.1.7601.18247
  Timestamp modulo con errori:	521ea8e7
  Codice eccezione:	c0000005
  Offset eccezione:	000674b9
  Versione SO:	6.1.7601.2.1.0.256.1
  ID impostazioni locali:	1040
  Informazioni aggiuntive 1:	0a9e
  Ulteriori informazioni 2:	0a9e372d3b4ad19135b953a78882e789
  Ulteriori informazioni 3:	0a9e
  Ulteriori informazioni 4:	0a9e372d3b4ad19135b953a78882e789

Leggere l'informativa sulla privacy online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0410

Se l'informativa sulla privacy online non è disponibile, leggere quella offline:
  C:\Windows\system32\it-IT\erofflps.txt
Last edited on
> if (!argc>2)
¿don't you bother to read the warnings?
warning: logical not is only applied to the left hand side of comparison

So it will be interpreted as if( (!argc) > 2 )
which translates to 0>2 -> false

Also, your example doesn't compile, it is missing the `command_dir()' definition.

> :::HERE ERROR:::
¿how did you determine that?

> the variable doeas't SEE THE FUNCTION!!
no idea what you are trying to say

> THIS IS THE ERROR DETAILS:
useless.
Provide a backtrace, that'll give the exact line that causes the crash.
you may also want to inspect the value of `comando'
MY BAD.

The program RUN, and i just takeoff all cout<<""; to show me where the prog crash.

the ":::HERE ERROR:::" is where program CRASH, i put it HERE on the forum, not in the EDITOR (it doeasn't go over that point (when filtro is like examples ))

"the variable doeas't SEE THE FUNCTION!!"
Mean that the variable that push the crash is filtro (probably) and "filtro" never appear in the funtion in which prog. crash

This is the FULL prog.
I underlined the funtion in which the program crash and the line in the main where func is called.

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
170
171
172
173
174
175
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "D:\funz.cpp"
#include <windows.h>
#define reset(x) strcpy(x, "")

using namespace std;
using namespace MioNS;

char * estraistring(const char*riga, int a, int b){ 
     int dimout=b-a+2;
     char * out=new char[dimout];
     
     strcpy(out,"");
     for (int i=a, j=0; i<b+1; i++, j++)
         out[j]=riga[i];
     out[dimout-1]='\0';
     return out;
     }

     void jump_row(ifstream & row){
          string tmp;
          getline(row,tmp);
          }
          
    char* comand_dir(char path[]){
          #define add(x) strcat(comando, x)
          char * comando=new char[1000];
          strcpy(comando, "");
          add("dir "); add("\""); add(path);add("\"");
          //#undef add
          return comando;
          }
          
    char* comand_dir(char path[], char file[]){
          //#define add(x) strcat(comando, x)
          char * comando=comand_dir(path);
          add(" > "); add(file);
          //#undef add
          return comando;
          }
          
    char* comand_dir(char path[], char file[], char param[]){
          //#define add(x) strcat(comando, x)
          char * comando=comand_dir(path, file);
          add(" "); add(param);
          //#undef add
          return comando;
          }

    char* set (char * var, char* valore, char* param){
          //#define add(x) strcat(comando, x)
          char * comando=new char[1000]; strcpy(comando, "");
          add("set "); add(param); add(" ");
          add(var); add("=");add(valore);
          //#undef add
          return comando;
          }
          
    char* set (char * var, char* valore){    
          //#define add(x) strcat(comando, x)
          char* comando=new char[1000]; strcpy(comando, "");
          add("set "); add(var); add("="); add(valore);
          #undef add
          return comando;
          }
               
    char * nomevar(int& ndir, char* nomeconst){
         char* nome=new char[1000];
         strcpy(nome,nomeconst);
         delete[] nomeconst;
         ndir++;
         strcat(nome,conv_num_numchar(ndir));
           return nome;
           }
           
    char * nospace(const char * str){
         int i=0, j=0;
         for (i=0; str[i]==' ' && i<strlen(str); i++){};
         for (j=strlen(str)-1; str[i]==' ' && j>=0; j--){};
         return estraistring(str, i, j);
         }
         
    bool ok(char* valore, char filtro[]){
         #define check if (filtro[i_filtro]==':') goto ast; if (filtro[i_filtro]=='?') goto inter; goto norm
         #define lval i_val==strlen(valore)-1
         #define lfiltro i_filtro==strlen(filtro)-1
         int i_filtro=0, i_val=0;
         check;//controllo su valore iniziale(non sappiamo come arriva), contro su file finale(esiste almeno un file)
         norm:

              if ((lval+1) || filtro[i_filtro]!=valore[i_val]){cout<<"\nreturn nor1"; return false;}//se val è finito e siamo in filtro lettera. must false. oppure..ovvio.
              if (lval && lfiltro){cout<<"\nreturn nor2"; return true;}//se sono l'ultima di both. must true
              i_filtro++; i_val++; if (lfiltro+1) {cout<<"\nreturn nor3";return false; }//se filtro finito e val no. essendo come last filtro una lettera. must false.
              check;
         inter:

               if (filtro[i_filtro+1]==':') goto ast;
               if (lval+1) {cout<<"\nreturn ?1";return false;}//? non trova confronti, par brutt. must false.
               if ((lval) && (lfiltro)) {cout<<"\nreturn ?2";return true;} //usciti insieme. must true.
               i_filtro++; i_val++; if (lfiltro+1){cout<<"\nreturn ?3"; return false; }//se lfiltro+1 allora prima era lfiltro, ma non lval e nemmeno lval+1 quindi ci sono altre lettere da controllora ma non c'è filtro(finito con'?'). must false.
               check;
         ast: 
             if (lfiltro) {cout<<"\nreturn ast1";return true;}
             if (filtro[i_filtro+1]==':' || filtro[i_filtro+1]=='?') {i_filtro++; goto ast;}
             while (!(i_val==strlen(valore))){
                   if (valore[i_val]==filtro[i_filtro+1]) {
                      i_filtro++;
                      goto norm;
                      }
                   i_val++;
                   } 
                      {cout<<"\nreturn ast2";return false;}
         }
#undef check
#undef lval
#undef lfiltro
             
int main(int argc, char * argv[]){
    
    if (!(argc>2)) {cout<<"\nPochi argomenti\n";system("pause>nul");exit(1);}
    const int dim_comand=1000;
    
    int nfile=0, tmp=0;//della riga
    char riga[dim_comand];
    char *dir=new char[strlen(argv[1])];
    char *filtro=new char[strlen(argv[2])];
    char *valore;
    
    char comand[dim_comand];//per comandi sys
    ifstream dirfile;
    
   //legge cartella e nome del filtro file
        strcpy(dir, argv[1]);
    strcpy(filtro, argv[2]);

    
    //creazione file dir
    system(set("dir", dir));    

    system(comand_dir(dir, "%temp%\\dirfile.txt"));
    delete[] dir;
    
    
    //lettura file
    system("ritardo 5");
    dirfile.open("C:\\Users\\Peppe\\AppData\\Local\\Temp\\dirfile.txt");
    if (dirfile.fail()) {
       cout<<"\nDirFile non trovato. Il programma terminera'.";
       system("pause > nul");
     exit(1);
       }
       
    //ignora prime 3 righe
    for (int i=0; i<3; i++)
        jump_row(dirfile);

    while (dirfile.getline(riga, dim_comand) && !strncmp(riga, " Directory di ", 14)){//finche file cattura e c'è "Direc.." 
          jump_row(dirfile);
          while (dirfile.getline(riga, dim_comand) && strncmp(riga, "     ", 5)){//fiche file cattura e nnon ci sono "spazi"
                valore=estraistring(riga, 36, strlen(riga)-1);//cattura nome del file/dir
                if ((strcmp(estraistring(riga,21,25),"<DIR>")) && ok(valore, filtro)){//se la stringa non è una "DIR" ed è "ok" per il filtro
                   system(set(nomevar(nfile, "file_"),valore));//crea nome della variabile e setta la var. con il nome del file
                     delete[]valore;}
                delete[]valore;
                } 
          system(set(nomevar(tmp, "byte"), nospace(estraistring(riga, 21,35)))); //estra la grandezza totale dei file

          }
    dirfile.close();
          
    system("pause > nul");
}


I'm getting mad. Thanks for your help

P.S. Thanks for the advice of "if (argc)"
(i don't know why, it never gave me problem)
Last edited on
> #include "D:\funz.cpp"
- Do not include source files (only headers). Instead, add the sources to your project.
- I do not have that file and so cannot compile your program.


1
2
char *dir=new char[strlen(argv[1])];
strcpy(dir, argv[1]);

remember that c-strings are zero terminated, you need to reserve enough space for the terminator.
You were trying to access out of bounds in the copy.
char *dir=new char[strlen(argv[1]) + 1];

by the way, I don't see the point in copying your parameters.
Also, you may want to use std::string to avoid the manual memory handling.


> The program RUN, and i just takeoff all cout<<""; to show me where the prog crash.
try to use a debugger, it will stop at the exact line where the crash occurs, you may be able to observe the variables, and there is no need to dirty your code.
THANKS. the problm was right that, the encrising dimention.

anyway, this is the only function missed for the correct compiling :
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
char* conv_num_numchar(int num0){
 //funzione log vuole il double come ritorno - mentre trunc vuole il double
    unsigned long long int maxunita=1000000000;
    int num=num0;
    if (num0<0) num*=(-1);
    char* numchar=new char[23];
    int unita;

    //trovare unita del numero
    for (int i=1; i<=maxunita; i=i*10)
        if (num/i <=9 ) {unita=i; break;}
    
    //scrivere ogni caratere trasfoamndo man mano il numero nelle singole unita
    for (int temp=0, i=0, j=unita; i<=log10(unita); i++, j/=10)   {
        
        temp=(int)(trunc(num/j)-(10*trunc(num/(j*10))));
        numchar[i]=(char)temp+48;
        numchar[i+1]='\0';
        }
    //se numero è negativo aggiunge il "-" inizial
    int j=strlen(numchar);
    if (num0<0) {
       for (int i=strlen(numchar); i>0; i--)
           numchar[i]=numchar[i-1];
       
       numchar[j+1]='\0';           
       numchar[0]='-';
       }
    return numchar;
    }

you can put it above all function.

>by the way, I don't see the point in copying your parameters
? i don't understand

Thanks for all advices

I have a new problem. and i don't know if you can help me.

This program should (i said should becouse function "ok" is under testing) read all file's name from a direcoty, filter them and store the files name in variables on the prompt instance in which the program has been call.
BUT
I doeans't work. The generic variable "%a%" through system(COMAND); (COMAND="set a=file") will not be stored.

Any ideas??
THANKS FOR ALL HELP
>>by the way, I don't see the point in copying your parameters
>? i don't understand
what I mean is that you can simply use argv[1] instead of `dir', or if you want clarity (which is good) char *dir = argv[1]
I don't see why you need to duplicate the string.


Sorry, but your code is too hard to follow (with all those macros and jumps).
If you say that COMMAND has what it should, then perhaps is an issue with how batch and system() work.
iirc system will spawn another shell, so the changes may be local to that shell, and lost when the command ends. But I don't use windows, so my information is quite vague.


You could make your program to simply output the filenames that pass the filter, redirect the output to a file and then take a simple batch script to do whatever you want with those files.
Well.. gode point (about argv), my is a stupid move.

thanks for advices and the alternative prospettive for my purpose.

anyway, the variabile doesn't be setted not even White program is runnig (i tried).

thanks anyway.
Topic archived. No new replies allowed.