Раздел «Язык Си».CoffeString1:

Чтение строк

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
   int p;
   FILE *fin;
//'\0'
   fin = fopen("001.dat", "r");
   if (errno)
   {
      perror("001.dat");
      exit(1);
   }
   char st[101], st2[101];
   bzero(st, sizeof(char) * 101);
   while ((p = fscanf(fin, "%100s", st)) != EOF)
   {
      printf("st: %s\n", st);
   }
   return 0;
   
}

Сравнение и копирование строк

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(int argc, char ** argv)
{
   char *passwd = "I'm under the table";
   char get[1024], *copy, *f = 0;
   int size = 1024, len2 = 0;
   bzero(get, sizeof(char) * 1024);
   copy = calloc(size, sizeof(char));
   bzero(copy, sizeof(char) * 1024);
   f = copy;
//    strcat(copy, get);
  do
  {
     fgets(get, 100, stdin);
      int len = strlen(get);
      get[ len - 1 ] = '\0';
     printf("len: %d strcmp: %d\n", len, strcmp (get, passwd));  
     int len2 = strlen(copy);
     if (len >= size - len2 )
     {
        size += 1024;
        copy = realloc(copy, size);
        bzero(copy + 1024, sizeof(char)* 1024);  
     }
     strcpy(f, get);
     printf("copy: %s \n", copy);
     f += len - 1;
  }while (strcmp (get, passwd) != 0);
  
   printf("Ura!!\n");
   return 0;
}

Удаление #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(int argc, char ** argv)
{
   char * res;
   char buf[100];
   scanf("%s", buf);
   int len = strlen(buf);
   char *p = buf;
   int i;
   for(i = 0; (p = strstr(p, "#")) != 0; i++)
   {
      p[0] = '\0';
      strcat(buf, p + 1);
      printf("buf: %s\n", buf);   
   }
   return 0;
}
-- TatyanaOvsyannikova2011 - 20 Nov 2019