Corot
Corot

Reputation: 43

Printing specific lines of a file

I'm trying to printf specific lines of a .txt file by comparing the first char of the line to "-", and only printing if it isn't the same.

void menu() {

  FILE *fp =fopen("save_projeto.txt","r");
  char line[MAX_LENGTH];
  fgets(line, MAX_LENGTH, fp);

  while(!feof(fp)){

    if (strcmp(line[0], "-") == 0) {
        fgets(line, MAX_LENGTH, fp);
    }

    else {
        printf("%s", line);
        fgets(line, MAX_LENGTH, fp);
    }
  }
}

The file I'm trying to print is formatted like this, with 20 Locals and each one with up to 1.3 different PDI's.

1º Local
Amsterdao

1.1 PDI
Casa de Anne Frank
-Descricao: Museu biografico localizado na cidade de Amsterdao, capital dos Paises Baixos.
-Horario de funcionamento: *7*19

When I build the code, it runs without error messages, but the console does not print anything at all.

Upvotes: 0

Views: 183

Answers (4)

William Pursell
William Pursell

Reputation: 212248

Don't read more than you need to. One character at a time will do:

#include <stdio.h>

/* Print lines that do not start with '-' */
int
main(int argc, char **argv)
{
        int first_char=1;
        int print;
        int c;

        FILE *fp = argc > 1 ? fopen(argv[1],"r") : stdin;
        if( fp == NULL ) {
                perror(argv[1]);
                return 1;
        }

        while( (c = fgetc(fp)) != EOF ) {
                if( first_char )
                        print = c != '-';
                if( print )
                        putchar(c);
                first_char = c == '\n';
        }
        return ferror(fp);
}

Upvotes: 0

eapetcho
eapetcho

Reputation: 527

@Corot Another approach to solving your problem is the following code:

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

/* Path to the file you are reading */
#define FILENAME  "save_projeo.txt"

/* Set the maximum length of text line in the input file */
#define MAX_LENGTH    92

/**************
 * Main Driver
 **************/
 int main()
 {
     FILE *fp;
     char buffer[MAX_LENGTH+1]; // buffer to hold data read from input file
     const char *str = "-";  // String (here char) to be searched
     size_t num = 1;

     fp = fopen(FILENAME, "r");

     // Is the file opened to read?
     if( !fp){
         fprintf(stderr, "Unable to open file <%s>\n", FILENAME);
         exit(EXIT_FAILURE);
     }
     while(fgets(buffer, MAX_LENGTH, fp)){
          // If the first num bytes of buffer does not match the first num byte of str
          // then print the content of buffer
          if(memcmp(buffer, str, num) != 0)
              printf("%s\n", buffer);
     }
     fclose(fp);
     return EXIT_SUCCESS;
 }

Upvotes: 0

jspcal
jspcal

Reputation: 51904

The comparison should be for the first character:

if (line[0] == '-') {
    /* First char is a dash */
}

Perhaps add a newline to the string when printing?

 printf("%s\n", line);

If you want each line to be immediately written to the output stream, then fflush it:

fflush(stdout);

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

strcmp compares an entire string, not just a single character out of it.

Write line[0] == '-' as a condition for testing just the first character. And note the single quotes in '-', which denote a single character, whereas double quotes like "-" represent a 0-terminated string literal.

Upvotes: 1

Related Questions