S
S
samsungovetch2018-07-06 12:42:20
C++ / C#
samsungovetch, 2018-07-06 12:42:20

C - How to output lines of a certain length (> 60 characters) from a file?

Greetings. The following C (not C++) program simply prints all lines to the screen.

#include <stdio.h>

void main()
{
  FILE * fil;
  int temp;
  char mem;
  fil = fopen("D:\\FilesProjects\\1file.txt", "rt");
  while (!feof(fil)) 
  {
    int res;
    res = fscanf(fil, "%c", &mem);

    if (res == EOF)
      break;
    
    printf("%c", mem);
    
  }
  printf("\n");
  fclose(fil);
  system("pause");
}

How to write a condition for displaying not all lines, but lines longer than 60 characters?
As a result of executing this code, an empty console opens and closes after a few seconds.
#include <stdio.h>

void main()
{
  FILE * fil;
  int temp;
  char mem;
  fil = fopen("D:\\FilesProjects\\1file.txt", "rt");
  while (!feof(fil)) 
  {
    int res;
    res = fscanf(fil, "%c", &mem);
    if (res == EOF)
      break;
    if (strlen(mem) <= 60)
    {
      printf("%c", mem);
    }
    
  }
  printf("\n");
  fclose(fil);
  system("pause");
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question