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); 

}

    1 answer 1

    UPD: Updated the answer according to the question.

    Go through your code.

    1) The decrypt() ) function is declared as void , but returns 0 if the file could not be opened. Change the function type to int .

     int decrypt() { 

    2) The names i and j for variables that are not very appropriate will call them ...

     int pwd_counter = 0, note_counter = 0; 

    3) The string if (dataFromFilePointer[strlen(dataFromFilePointer) - 1] = "\n") { . You apparently want to replace the newline with a null character. It is necessary to replace the assignment = by comparison == and the string "\n" with the character '\n' (note the quotes).

     if (dataFromFilePointer[strlen(dataFromFilePointer) - 1] == '\n') 

    4) The condition if (i == j){ does not bring down much. Why not just check if the first character of a string is a P :

     if (*dataFromFilePointer == 'P') { 

    5) Why not loop the lines?

     for (int i = 0; i < pwd_counter; ++i) printf("%s \n%s \n", pwdFromFile[i], noteFromFile[i]); 

    6) We need to close the file and release the dataFromFilePointer variable dataFromFilePointer

     free(dataFromFilePointer); fclose(encryptedPasswords); 

    Total:

     int decrypt() { HANDLE currConsole = GetStdHandle(STD_OUTPUT_HANDLE); FILE* encryptedPasswords; char dataFromFile[2048], pwdFromFile[64][1024], noteFromFile[64][1024], cuttedStr[1024]; char* dataFromFilePointer; int pwd_counter = 0, note_counter = 0; printf("Opening file with encrypted passwords..."); encryptedPasswords = fopen("my_file.txt", "r"); if (NULL == encryptedPasswords) { printf("\n[ERR]"); printf("Passwords file not found or can not be read\n"); printf("Exiting into main menu..."); return 0; } else { printf("OK\n"); } while (1) { dataFromFilePointer = fgets(dataFromFile, sizeof(dataFromFile), encryptedPasswords); if (dataFromFilePointer == NULL) { if (feof(encryptedPasswords) != 0) { break; } else { printf("\n[ERR]"); printf("Can't read from file\n"); break; } } if (dataFromFilePointer[strlen(dataFromFilePointer) - 1] == '\n') dataFromFilePointer[strlen(dataFromFilePointer) - 1] = '\0'; if (*dataFromFilePointer == 'P') { sprintf(pwdFromFile[pwd_counter], "%s", strncpy(cuttedStr, dataFromFilePointer + 10, sizeof(cuttedStr))); pwd_counter++; } else { sprintf(noteFromFile[note_counter], "%s", strncpy(cuttedStr, dataFromFilePointer + 6, sizeof(cuttedStr))); note_counter++; } } for (int i = 0; i < pwd_counter; ++i) printf("%s \n%s \n", pwdFromFile[i], noteFromFile[i]); free(dataFromFilePointer); fclose(encryptedPasswords); } 

    Removed all sleep and console color changes, since my editor does not support this. Of course, the approach itself is not very good, but now the code should work properly.

    • Oh, thanks for the reply. In fact, almost all of this was already written to me (but in some places a little differently), and it began to work fatkichno after changing a single line. Instead of fgets, sprintf. That's how it began to save to the array. So far the only problem is in the second array, for some reason, the first element clogs all the characters in front of the line with capital letters M. And in the remaining lines there is no such thing. Strange. - Wheat
    • @ Wheat, but not for that. If you have found a solution, issue it as an answer and accept it as correct. - eanmos
    • @ Wheat, write in more detail what exactly you need, and also lay out the contents of the file being read. - eanmos
    • Read the file in two arrays. In one - "encrypted" passwords, in the second - notes. The task is to encrypt the entered password, write to the file, and then you can decrypt it there too. And at the moment almost everything works: the program reads lines into arrays, in the correct order, but the first line of the second array looks like MMMMMMMMMMMMMMNote: ~! @ # $% ^ & * () _ + And with all the others, it’s normal. In the file, this is: Password: "A $% & _ '+) *`, Note: ~! @ # $% ^ & * () _ + Password: rxf Note: qwe Password: rxfsuz Note: yukuken Password: kchf Note: ytsu Password: хбг79х5г7хь5г75 Note: Feeding - Wheat
    • @ Wheat, do you need to read lines together with the words "Password:" and "Note"? - eanmos