The tape is read from the file, and the MT program itself is also read from there. The essence of the MT program is to translate 0 to 1 and vice versa. Copes with the task, but at the end reports an error Mistake

The conversion is in progress, but instead of writing to the file, it gives the specified error.

Please pay attention to one interesting fact. If you enter the ribbon: 00110011, then everything works fine. But if you first enter 1, and then 0, for example 11010, then crashes with the specified error. Here is the code itself:

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAXCOMMAND 256 typedef struct _progr { char inputsymb; //Π’Ρ…ΠΎΠ΄Π½ΠΎΠΉ символ int state; //ΠΠ°Ρ‡Π°Π»ΡŒΠ½ΠΎΠ΅ состояниС char outputsymb; //Π‘ΠΈΠΌΠ²ΠΎΠ» Π½Π° Π²Ρ‹Ρ…ΠΎΠ΄Π΅ int newstate; //НовоС состояниС char action; //ДСйствиС } progr; /*----------------------------------Бписок----------------------------------*/ /*Π‘Ρ‚Ρ€ΡƒΠΊΡ‚ΡƒΡ€Π° ΡƒΠ·Π΅Π»*/ typedef struct _Raw { char value; /*Π₯Ρ€Π°Π½ΠΈΡ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅*/ struct _Raw *next; /*Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт*/ struct _Raw *prev; /*ΠŸΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ элСмСнт*/ } Raw; typedef struct _DbLink { size_t size; /*Π₯Ρ€Π°Π½ΠΈΡ‚ Ρ€Π°Π·ΠΌΠ΅Ρ€ списка*/ Raw *head; /*Начало списка*/ Raw *tail; /*ΠšΠΎΠ½Π΅Ρ† списка*/ } DbLink; /*Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ списка*/ DbLink* createDbLink() { DbLink *tmp; tmp = (DbLink*)malloc(sizeof(DbLink)); //ДинамичСскоС Π²Ρ‹Π΄Π΅Π»Π΅Π½ΠΈΠ΅ памяти tmp->size = 0; //ΠžΠ±Π½ΡƒΠ»Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° списка tmp->head = tmp->tail = NULL; return tmp; } /*Ѐункция удалСния списка*/ void deleteDbLink(DbLink *list) { Raw *tmp = list->head; Raw *next = NULL; while (tmp) { next = tmp->next; free(tmp); tmp = next; } free(list); } /*Ѐункция добавлСния Π² ΠΊΠΎΠ½Π΅Ρ† списка (Ρ‚.ΠΊ. Ссли Π΄ΠΎΠ±Π°Π²Π»ΡΡ‚ΡŒ Π² Π½Π°Ρ‡Π°Π»ΠΎ, вся Π»Π΅Π½Ρ‚Π° Π±ΡƒΠ΄Π΅Ρ‚ Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΉ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ*/ void pushBack(DbLink *list, void *value) { Raw *tmp = (Raw*)malloc(sizeof(Raw)); if (tmp == NULL) { exit; } tmp->value = value; tmp->next = NULL; tmp->prev = list->tail; if (list->tail) { list->tail->next = tmp; } list->tail = tmp; if (list->head == NULL) { list->head = tmp; } list->size++; } /*Ѐункция распСчатывания списка*/ void printfDbLink(DbLink *list) { Raw *tmp; tmp = list->head; while (tmp != NULL) { printf("%c ", tmp->value); tmp = tmp->next; } printf("\n"); } /*ΠžΠΏΠ΅Ρ€Π°Ρ†ΠΈΡ Π½Π°Π΄ Π»Π΅Π½Ρ‚ΠΎΠΉ(Π°Π»Ρ„Π°Π²ΠΈΡ‚ΠΎΠΌ)*/ void turing(DbLink *list, struct _progr arr[], int cmd) { int error = 0; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ останова int headstate = 1; //БостояниС Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ char replace_symb; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ для внСсСния Π½ΠΎΠ²ΠΎΠ³ΠΎ значСния FILE *result = fopen("result.txt", "wb"); int i = 0; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ для счСтчика Raw *buff, *tmp; //Π”Π²Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΡΡΡ‹Π»Π°ΡŽΡ‰ΠΈΠ΅ΡΡ Π½Π° структуру _Raw tmp = list->head; //ΠŸΡ€ΠΈΡΠ²ΠΎΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ссылки Π½Π° Head Π² спискС list fprintf(result, "Result is: "); while (tmp != NULL && error != 1) { for (i = 0; i < cmd; i++) { buff = tmp->value; //ΠŸΡ€ΠΈΡΠ²ΠΎΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ - значСния Π»Π΅ΠΆΠ°Ρ‰Π΅ΠΉ Π² Head списка list if (buff == arr[i].inputsymb && headstate == arr[i].state) { replace_symb = arr[i].outputsymb; headstate = arr[i].newstate; fprintf(result, "%c", replace_symb); printf("%c ", replace_symb); switch (arr[i].action) { case 'R': { tmp = tmp->next; break;} //ΠŸΡ€ΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Π΅ R сдвигаСтся Π½Π° ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт Π² спискС case 'L': { tmp = tmp->prev; break;} //ΠŸΡ€ΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Π΅ L сдвигаСтся Π½Π° ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ элСмСнт Π² спискС case 'S': { error = 1; break;} } } } } printf("\n"); } /*---------------------------------main------------------------------------*/ int main(void) { DbLink *list = createDbLink(); //Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ списка int headstate = 1, bool = 1, len = 0, c = 0, i = 0, b = 0; long size = 0; char sep[] = ":,"; char line[100]; char *token; char *buf; //Массив для хранСния Π°Π»Ρ„Π°Π²ΠΈΡ‚Π° считываСмого ΠΈΠ· Ρ„Π°ΠΉΠ»Π° progr arr[MAXCOMMAND]; //Массив стуктуры для записи ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ МВ FILE *fp = fopen("lenta.txt", "r"); //ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° fseek(fp, 0, SEEK_END); //ΠŸΡ€ΠΎΡ…ΠΎΠ΄ Π΄ΠΎ ΠΊΠΎΠ½Ρ†Π° Ρ„Π°ΠΉΠ»Π° size = ftell(fp); //Π£Π·Π½Π°Π΅ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ Ρ„Π°ΠΉΠ»Π° Π² Π±Π°ΠΉΡ‚Π°Ρ… buf = (char*)malloc(size * sizeof(char)); //ДинамичСскоС Π²Ρ‹Π΄Π΅Π»Π΅Π½ΠΈΠ΅ памяти ΠΏΠΎΠ΄ массив хранСния Π»Π΅Π½Ρ‚Ρ‹ rewind(fp); //Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π΅Π½ΠΈΠ΅ ΠΊ Π½Π°Ρ‡Π°Π»Ρƒ Ρ„Π°ΠΉΠ»Π° while (!feof(fp)) { //Π”ΠΎ Ρ‚Π΅Ρ… ΠΏΠΎΡ€ ΠΏΠΎΠΊΠ° Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ достигнут ΠΊΠΎΠ½Π΅Ρ† Ρ„Π°ΠΉΠ»Π° fscanf(fp, "%c", &buf[i]); //По ΠΎΠ΄Π½ΠΎΠΌΡƒ символу Π±ΡƒΠ΄Π΅Ρ‚ записыватся Π² массив i++; //ΠŸΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ Π½Π° ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт массива } c = i - 1; ///Π‘Π΅Π· этого ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ лишний пустой символ (????????) for (i = 0; i < c; i++) { pushBack(list, buf[i]); } /*---------------Π˜Π·Π²Π»Π΅Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ МВ ΠΈΠ· Ρ„Π°ΠΉΠ»Π°!----------------------*/ FILE *prog = fopen("prog.txt", "r"); i = 0; while (fgets(line, 100, prog) != NULL) { if (bool == 1) { len = strlen(line); for (i = 0;i<len;i++) { if (line[i] != '\r' && line[i] != '\n') { } else { bool = 0; break; } } } else { headstate = 1; token = strtok(line, sep); while (token != NULL) { char symb; if (strlen(token) == 1) { symb = *token; } else { if (sscanf(token, "%c%cq%d", &(arr[i].outputsymb), &(arr[i].action), &(arr[i].newstate)) >= 0) { arr[i].inputsymb = symb; arr[i].state = headstate++; printf("%cq%d-%cq%d%c\n", arr[i].inputsymb, arr[i].state, arr[i].outputsymb, arr[i].newstate, arr[i].action); i++; } } token = strtok(NULL, sep); } } } /*------------------ΠžΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΠ΅ считывания!--------------------*/ printfDbLink(list); printf("DBLink length %d\n", list->size); turing(list, arr, i); deleteDbLink(list); system("pause"); } 

What could be the problem?

Update.

Corrected comparison, now

 char temp = tmp->value; temp == arr[i].inputsymbol tmp->value = value 

(char * value now)

All the same mistake sameerror

----- Update -----

Fixed a bug, the problem really was in working with memory. I tried to take a value from null. Added another check if (tmp != NULL) So here you are, thanks to everyone for the help!

 void turing(DbLink *list, struct _progr arr[], int cmd) { int error = 0; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ останова int headstate = 1; //БостояниС Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ char replace_symb; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ для внСсСния Π½ΠΎΠ²ΠΎΠ³ΠΎ значСния char temp; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ для записи значСния ΠΈΠ· head->value FILE *result = fopen("result.txt", "wb"); int i = 0; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ для счСтчика Raw *tmp; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ ΡΡΡ‹Π»Π°ΡŽΡ‰Π°ΡΡΡ Π½Π° структуру _Raw tmp = list->head; //ΠŸΡ€ΠΈΡΠ²ΠΎΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ссылки Π½Π° Head Π² спискС list fprintf(result, "Result is: "); while (tmp != NULL && error != 1) { for (i = 0; i < cmd; i++) { if (tmp != NULL) { temp = tmp->value; //ΠŸΡ€ΠΈΡΠ²ΠΎΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ - значСния Π»Π΅ΠΆΠ°Ρ‰Π΅ΠΉ Π² Head списка list if (temp == arr[i].inputsymb && headstate == arr[i].state) { replace_symb = arr[i].outputsymb; headstate = arr[i].newstate; fprintf(result, "%c", replace_symb); printf("%c ", replace_symb); switch (arr[i].action) { case 'R': { tmp = tmp->next; break;} //ΠŸΡ€ΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Π΅ R сдвигаСтся Π½Π° ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт Π² спискС case 'L': { tmp = tmp->prev; break;} //ΠŸΡ€ΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Π΅ L сдвигаСтся Π½Π° ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ элСмСнт Π² спискС case 'S': { error = 1; break;} } } } } } printf("\n"); } 

    2 answers 2

    I did not dig deep. So just stick with the obvious.

     tmp->value = value; 

    A variable of type char assigned a value of type void* .

     buff = tmp->value; 

    The same, but in the opposite direction.

     buff == arr[i].inputsymb 

    Pointer comparison and char .

     pushBack(list, buf[i]) 

    I don’t understand at all - are you, compiler warnings turned off or ignored, without delving into? The compiler immediately throws a dozen. Understand, to understand why the program does not work, with this source - a thankless task ...

    Update

    Here's another -

     while (!feof(fp)) { //Π”ΠΎ Ρ‚Π΅Ρ… ΠΏΠΎΡ€ ΠΏΠΎΠΊΠ° Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ достигнут ΠΊΠΎΠ½Π΅Ρ† Ρ„Π°ΠΉΠ»Π° fscanf(fp, "%c", &buf[i]); //По ΠΎΠ΄Π½ΠΎΠΌΡƒ символу Π±ΡƒΠ΄Π΅Ρ‚ записыватся Π² массив i++; //ΠŸΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ Π½Π° ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт массива } c = i - 1; ///Π‘Π΅Π· этого ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ лишний пустой символ (????????) 

    Detection of the end of the file occurs when you try to read, that's where you have the extra character. And in general - why are you reading this if you figured out the size in advance? Simply

     fread(buf,1,size,fp); 

    - and no cycles :)

    I would highly not recommend using bool as a variable name ...

    • while pouring coffee, already answered))) - Yaroslav
    • Fixed errors, nothing has changed, the same problem, the same error! - Stab Ones
    • Well, I did not say that the reason for this. Most likely it is in some wrong release of memory, going beyond the array boundary, etc. At least you have two readable files β€” lenta.txt and progs.txt β€” you need to have them in order to get a specific, reproducible example. For example, I only know about the Turing machine that there is such a name - so I won’t understand exactly what the code does. But if there is a reproducible example, I will try to find out what exactly leads to this error, nothing more. But you don’t have this example ... - Harry
    • lenta.txt 1000000 prog.txt 01 0: 1Rq2,1Rq2 1: 0Rq2,0Rq2 Here) - Stab Ones

    How it works for you is interesting.
    in line

      buff = tmp->value; //ΠŸΡ€ΠΈΡΠ²ΠΎΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ - значСния Π»Π΅ΠΆΠ°Ρ‰Π΅ΠΉ Π² Head списка list 

    You assign a char pointer to a Raw structure.

    • Fixed, also does not work - Stab Ones