0

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.

Hogan
  • 69,564
  • 10
  • 76
  • 117
Corot
  • 33
  • 1
  • 1
  • 8
  • 1
    `if (strcmp(line[0], "-") == 0)` ==> `if (line[0] == '-')` – pmg May 20 '19 at 21:09
  • 1. You need to read the line before you test to see if the first char is '-'. 2. Don't use strcmp, you are not comparing strings you are comparing characters. – Stuart May 20 '19 at 21:11
  • Have replaced the condition with line[0] == '-', but it still won't print any line whatsoever. – Corot May 20 '19 at 21:15
  • https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – William Pursell May 20 '19 at 21:32
  • Don't read whole lines--you only need one character at a time. Read the first char. If it isn't `-`, print it and enter state PRINTING. If it is, enter state NOTPRINTING. Read the next character and print or not depending on the current state. If you read a newline, you may transition states on the next read. – William Pursell May 20 '19 at 21:35

4 Answers4

1

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.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

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);
jspcal
  • 50,847
  • 7
  • 72
  • 76
0

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);
}
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

@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;
 }
eapetcho
  • 527
  • 3
  • 10