There is a file: it contains some lines.
Password: "A$%&_'+)*` Note: ~!@#$%^&*()_+ Password: rxf Note: qwe Password: rxfsuz Note: йцукен Password: кчф Note: йцу Password: хьбг79х5г7хь5г75 Note: фывфыв The task is to read passwords in one array, and notes in another.
The problem is that I cannot correctly read a string into an array. On c-cpp.ru I read about arrays of strings: this is char [number of strings] [length of strings] Okay. I declared such an array. Two arrays. I start line by line, in while (true), read the file. Like this: dataPointer = fgets (dataFromFile, 2048, file); This should take a maximum of 2047 characters into a variable from the current line of the file, right? In order to write strings to arrays alternately, I did this:
int i=0, j=0; if (i=j){ fgets(stMass[i], 2048, dataPointer); i++; } else { fgets(ndMass[i], 2048, dataPointer); j++ } It just does not write the string to the array, throws away the violation of read rights from the memory. I will say straight away that I am a freshman student whose primary language is VB6, and C is a separate subject, but not explained at all, but it’s necessary to pass. Until now, I was able to find the answers to the questions myself, but now it is a plug. Actually, the question is: how to correctly read a string into an array? Perhaps in fgets you need to write not a pointer, but the dataFromFile, which is specified in fgets, to which this pointer refers? But this also does not work. I read about pointer dereferencing (* in front of the name), but it doesn’t work either, apparently, I don’t understand the mechanism of this.
The code is:
void decrypt(){ HANDLE currConsole = GetStdHandle(STD_OUTPUT_HANDLE); FILE *encryptedPasswords; char dataFromFile[2048], *dataFromFilePointer, pwdFromFile[64][1024], noteFromFile[64][1024]; int i = 0, j = 0; char cuttedStr[1024]; printf("Opening file with encrypted passwords..."); encryptedPasswords = fopen("encrypted passwords.txt", "r"); if (NULL == encryptedPasswords) { SetConsoleTextAttribute(currConsole, Black << 4 | LightRed); printf("\n[ERR]"); SetConsoleTextAttribute(currConsole, Black << 4 | White); printf("Passwords file not found or can not be read\n"); Sleep(500); printf("Exiting into main menu..."); Sleep(2000); return 0; } else { SetConsoleTextAttribute(currConsole, Black << 4 | Green); printf("OK\n"); SetConsoleTextAttribute(currConsole, Black << 4 | White); while (true) { dataFromFilePointer = fgets(dataFromFile, sizeof(dataFromFile), encryptedPasswords); if (NULL == dataFromFilePointer) { if (feof(encryptedPasswords) != 0) { break; } else { SetConsoleTextAttribute(currConsole, Black << 4 | LightRed); printf("\n[ERR]"); SetConsoleTextAttribute(currConsole, Black << 4 | White); printf("Can't read from file\n"); break; } } if (dataFromFilePointer[strlen(dataFromFilePointer) - 1] = "\n") { dataFromFilePointer[strlen(dataFromFilePointer) - 1] = '\0'; } if (i == j){ sprintf(pwdFromFile[i], "%s", strncpy(cuttedStr, dataFromFilePointer + 10,sizeof(cuttedStr))); i++; } else { sprintf(noteFromFile[i], "%s", strncpy(cuttedStr, dataFromFilePointer + 6, sizeof(cuttedStr))); j++; } } } printf("%s \n", pwdFromFile[0]); printf("%s \n", noteFromFile[0]); printf("%s \n", pwdFromFile[1]); printf("%s \n", noteFromFile[1]); printf("%s \n", pwdFromFile[2]); printf("%s \n", noteFromFile[2]); printf("%s \n", pwdFromFile[3]); printf("%s \n", noteFromFile[3]); printf("%s \n", pwdFromFile[4]); printf("%s \n", noteFromFile[4]); Sleep(5000000); }