#include <conio.h> #include <stdio.h> #include <stdlib.h> FILE *f_out, *f_in; void output_0(char *stroka){ int i,n; while (stroka[i] != '!'){ i++; n = i; } for (i=0; i<=n; i++){ if (stroka[i] == 'ю'){ printf("YES"); } else { printf("NO"); } } } void output_1(char *stroka){ f_out = fopen("out.txt", "w"); int i,n; while (stroka[i] != '!'){ i++; n = i; } for (i=0; i<=n; i++){ if (stroka[i] == 'ю'){ fprintf(f_out, "YES"); } else { fprintf(f_out, "NO"); } } } int main (void){ int in, out, next, n, i; system("chcp 1251"); char *stroka; do{ printf("input from console - 0 or from file - 1? "); scanf ("%d", &in); printf("output from console - 0 or from file - 1? "); scanf("%d", &out); if (in ==0){ printf("Введите строку: "); stroka = malloc( sizeof(*stroka) * 64 ); scanf("%s", *stroka); } else if (in == 1){ f_in = fopen("in.txt", "r"); stroka = malloc( sizeof(*stroka) * 64 ); fscanf(f_in, "%s", *stroka); } else { printf("ОШИБКА!"); } if (out == 0){ output_0(stroka); } else if (out == 1){ output_1(stroka); } else { printf("ОШИБКА!"); } printf("\n\nNEXT - 1, EXIT - 0? "); scanf("%d", &next); } while (next !=0); free(stroka); return 0; } 
  • Why did you decide that "it does not work out to pass a pointer to a string in a function as a parameter"? - AnT 8:36 pm
  • either the problem of memory allocation, or in the transfer of parameters, I do not know. - YailGafu 8:43 pm
  • You have a lot of problems in the code, but they have nothing to do with "passing a pointer to a string into a function" - AnT pm
  • Yeah, thanks to the bad program, I agree - YailGafu

5 answers 5

you can take a string of unknown length

 #include <stdio.h> #include <stdlib.h> #include <assert.h> char *getstring(FILE *f) { size_t size = 4, idx = 0; char *buf = malloc(size); if (!buf) return NULL; int ch; while ((ch = fgetc(f)) != '\n' && ch != EOF) { if (idx == size - 1) { size *= 2; char *tmp = realloc(buf, size); if (!tmp) return NULL; buf = tmp; } buf[idx++] = ch; } if (idx == 0 && ch == EOF) return NULL; if (idx > 0 && idx < size - 1) { char *tmp = realloc(buf, idx + 1); if (!tmp) return NULL; buf = tmp; } buf[idx] = '\0'; return buf; } int main(void) { char *s; while((s = getstring(stdin))) { printf("%s\n", s); free(s); } return 0; } 
  • Thank you very much! - YailGafu 6:03 pm

Possible problem in line

 scanf("%s", *stroka); 

You pass char type, instead of pointer. Try passing a pointer.

 scanf("%s", stroka); 

Similar to fscanf .


Edit

This code works fine when using ASCII. (mingw32-gcc 8.1.0)

 #include <stdio.h> #include <stdlib.h> #include <string.h> int getfrom(const char* /* __restrict__ */ name, const char* /* __restrict__ */ mode, char* /* __restrict__ */ buffer) { FILE* fin = fopen(name, mode); fscanf(fin, "%s", buffer); fclose(fin); return strlen(buffer); } void output_file(char* buffer) { FILE* fout = fopen("out.txt", "w"); while (*buffer == '!') buffer++; for (int i = 0; i < strlen(buffer); i++) if (buffer[i] == 'w') fprintf(fout, "YES"); else fprintf(fout, "NO"); fclose(fout); } void output_print(char* buffer) { while (*buffer == '!') buffer++; for (int i = 0; i < strlen(buffer); i++) if (buffer[i] == 'w') printf("YES"); else printf("NO"); printf("\n"); } int main(void) { while (1) { int fin; printf("Use file input? (1/0): "); scanf("%d", &fin); int fout; printf("Use file output? (1/0): "); scanf("%d", &fout); char* buffer = malloc(sizeof(char) * 64); if (fin) getfrom("in.txt", "r", buffer); else { printf("Input (max 64): "); scanf("%s", buffer); } if (fout) output_file(buffer); else output_print(buffer); free(buffer); int continue_; printf("Continue? (1/0): "); scanf("%d", &continue_); if (!continue_) break; } return 0; } 
  • for sure! I'll check it out - YailGafu
  • no, does not display anything - YailGafu
  • Strangely, tested on gcc 8.1.0, this is exactly what caused the problem. - Wisser Tg 8:55 pm
  • I have CodeBlocks ....... ah - YailGafu
  • one
    @AliyaGafurova I didn’t notice right away, I’m not an ace in C, but in the line malloc(sizeof(*stroka) * 64) replace *stroka with char . - Wisser Tg 9:31 pm

I would advise using something like this:

 #define LEN 1024 char * readString (FILE * file) { char buf [LEN]; char * ret; if (fgets (buf, LEN, file) == NULL) { return NULL; } /* +1 для завершающего символа '\0' */ ret = malloc (strlen (buf) + 1); if (ret == NULL) { return NULL; } return strcpy (ret, buf); } 
     void output_0(char *stroka){ int i = 0,n; // !!! stroka = malloc( sizeof(char) * 64 ); // что такое 64? 
    • stroka = malloc (char * 64); // what is 64? honestly I don’t understand myself - YailGafu
    • those. malloc allows you to allocate memory for each new character entered, therefore you need to multiply by sizeof (char)? - YailGafu
    • one
      @ AliyaGafurova is not for everyone, but for all sixty-four at once - Igor 8:36 pm
    • and how can I do to properly allocate memory for a string of unknown length? - YailGafu
    • one
      sizeof(char) - always and in all systems today == 1 , multiplication without meaning, only for demonstration and training purposes. - NewView pm
     В итоге сделала так: #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void output_0(char* str){ int i, n=0; for (i=0; i<=strlen(str); i++){ if (str[i] == '!'){ n = i; } } for (i=0; i<=n; i++){ if (str[i] == 'ю'){printf("YES");} else {printf("NO");} } } void output_1(char* str, FILE* file_out){ int i,n=0; for (i=0; i<=strlen(str); i++){ if (str[i] == '!'){ n = i; } } for (i=0; i<=n; i++){ if (str[i] == 'ю'){fprintf(file_out,"YES");} else {fprintf(file_out, "NO");} } } int main (){ int in, out, next, i; system("chcp 1251"); char ch; do{ char* file_in_str = malloc(1); char* file_out_str = malloc(1); char* str = malloc(1); printf("input from console - 0 or from file - 1? "); scanf ("%d", &in); printf("output from console - 0 or from file - 1? "); scanf("%d", &out); if (in ==0){ printf("Введите строку: "); getchar(); for (i=0; (ch = getchar()) != '\n';){ str = realloc(str, i+1); str[i] = ch; i++; } } else if (in == 1){ printf("Введите адрес файла: "); getchar(); for (i=0; (ch = getchar()) != '\n';){ file_in_str = realloc(file_in_str, i+1); file_in_str[i] = ch; i++; } if (i == 0) file_in_str = "in.txt"; else file_in_str[i] = '\0'; FILE* file_in = fopen(file_in_str, "r"); fgets(str, 100, file_in); fclose(file_in); } else { printf("ОШИБКА!"); } if (out == 0){ output_0(str); } else if (out == 1){ printf("Введите адрес файла: "); for (i=0; (ch = getchar()) != '\n';) { file_out_str = realloc(file_out_str, i+1); file_out_str[i] = ch; i++; } if (i == 0) file_out_str = "out.txt"; else file_out_str[i] = '\0'; FILE* file_out = fopen(file_out_str, "w+"); output_1(str, file_out); fclose(file_out); } else { printf("ОШИБКА!"); } free(file_in_str); free(file_out_str); free(str); printf("\n\nNEXT - 1, EXIT - 0? "); scanf("%d", &next); } while (next !=0); return 0; }