Why is my program failing? "add" is not equal to "add"?

This is my code. Just note that almost everything written in errormsg() is incorrect. So, this is what I executed: ./exec BATTLETO.NES after.nes 0 55 1 add 0

Even though everything seems good to me, my program seems to disagree. It seems to fail when checking the value of argv[6], which should be "add" but the program says otherwise. Any ideas?

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
#include<cstdio>
#include<cstdlib>
#include"badmath.h"

using namespace std;

void errormsg()
{
printf("%s","Syntax error. Syntax is:\n");
printf("%s","corrupt <ifile> <ofile> <strt> <stop> <every> <operation> <additional>\n");
printf("%s","ifile is the path to the input file.\n");
printf("%s","ofile is the path to the output file.\n");
printf("%s","strt or \"start\" is the position in the file to start corrupting.\n");
printf("%s","stop is the position in the file to stop corrupting.\n");
printf("%s","every is the frequency of memory to ignore.\n");
printf("%s","\n");
printf("%s","Operations:\n");
printf("%s","add\n");
printf("%s","\n");
printf("%s","Extra:\n");
printf("%s","When writing numbers, write them in hexadecimal (1956) being (00000007A4). This program supports files up to 1TB.\n");
}

bool read;
unsigned char argcount;
FILE* ifile;
FILE* ofile;
long long int strt;
long long int stop;
unsigned char frequency;

int main(int argc, char* argv[])
{
if(argc < 8){errormsg();return 1;}
ifile = fopen(argv[1],"rb");
if(ifile == NULL){printf("%s","ifile does not exist.\n");return 1;}
ofile = fopen(argv[2],"wb");
strt = atoi(argv[3]);
stop = atoi(argv[4]);
frequency = atoi(argv[5]);
char addative = atoi(argv[7]);
char reg;
unsigned char countr = 0;
printf("%s",argv[6]);
if(argv[6] == "add"){
while((reg = fgetc(ifile)) != EOF){if(frequency == countr){fputc(reg + addative, ofile);}countr = countr +1;}
}
else{errormsg();return 1;}
printf("%s","The file seems to have been successfully corrupted...\n");
return 0;
}


Oh, and if you want to compile it, badmath.h is unused...
Last edited on
argv[6] is definitely "add", but you have to use strcmp() to compare two c-strings, not equality operators. You should consider using the string class instead. Something like this would work:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>

using std::string;

int main (int argc, char* argv[]) {
     string op = argv[6];
     //your code
     if(op == "add"){
          while((reg = fgetc(ifile)) != EOF){if(frequency == countr){fputc(reg + addative, ofile);}countr = countr +1;}
     }
     //the rest of your code
     return 0;
}


String are much easier to work with than char arrays.
Last edited on
Oooh, you're awesome! I totally forgot about strcomp... I'm currently using exclusively cstd libraries because the program is made to run on all/most platforms. I'm not exactly used to it because I usually use regular strings and iostream. Thanks!
Topic archived. No new replies allowed.