They are global because many of the functions need to access them, and it's more convenient than passing them as parameters.
Alright here's the program (modified for testing) everything that runs up to the point of where it messes up
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* username[50];
char* password[50];
char* type[50];
int recordcount;
//Gets all the data for the 3 arrays containing usernames, passwords, and types
int getData()
{
FILE* pfile;
pfile = fopen("password.csv", "a+");
char * token;
char line[300];
int i = 0;
while (fgets(line, 300, pfile) != NULL)
{
printf("Getting the line: %s", line);
token = strtok(line,",");
printf("Token 1 stored in username[%d]: %s\n", i, token);
username[i] = token;
token = strtok(NULL,",");
printf("Token 2 stored in password[%d]: %s\n", i, token);
password[i] = token;
token = strtok(NULL,"\n");
printf("Token 3 stored in type[%d]: %s\n", i, token);
type[i] = token;
i++;
}
recordcount = i;
return 0;
}
int main(int argc, char* argv[])
{
getData();
//Checks that there aren't no parameters
if (argc == 1)
syntaxerror();
printf("Test 0: %s, %s, %s\n", username[0], password[0], type[0]);
char * switchParameter = argv[1];
printf("Test 1: %s, %s, %s\n", username[0], password[0], type[0]);
if (strcmp("-menu",switchParameter) == 0)
menu();
else
{
printf("Test 2: %s, %s, %s\n", username[0], password[0], type[0]);
/*Rest of program*/
}
}
This is tested using a password.csv file with just the single line:
running ./a.out [somerandomparameter not -menu]
and the output up to this point is:
Code:
Getting line: abc,def,ghi
Token 1 stored in username[0]: abc
Token 2 stored in password[0]: def
Token 3 stored in type[0]: ghi
Test 0: abc, def, ghi
Test 1: abc, def, ghi
Test 2: �W�, , 8��
Now as you can see... Test 2 is completely messed up and I have no idea why.
Thanks in advance guys, really appreciate it.