0

so im trying to open .txt(or .bin) files in my C program. When i run the code it gives me 0 errors, but the console display is empty. Im fairly sure my code is correct. Is there something i need to setup in order to be able to open files on linux(im fairly new to it)? Like permission,but then again it would have given me the error no permission, right ?

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

struct znakovi{
   int tocka;
   int upitnik;
   int usklicnik;
};
struct znakovi interpukcijski(FILE *f){
  int brojac_tocki=0,brojac_upitnika=0,brojac_usklicnika=0;
  char c;
  while((c==fgetc(f))!=EOF){
         if(c=='.')
           brojac_tocki++;
         if(c=='?')
           brojac_upitnika++;
         if(c=='!')
           brojac_usklicnika++;
  }
  struct znakovi z;
   z.tocka=brojac_tocki;
   z.upitnik=brojac_upitnika;
   z.usklicnik=brojac_usklicnika;
  return z;
}

int main(){
    struct znakovi d;
    FILE *f;
    f=fopen("/home/matej/pmalabos/vjezba1/zd.txt","r");
    if(f==NULL)
      return -1;
    d=interpukcijski(f);
    printf("%d %d %d",d.tocka,d.upitnik,d.usklicnik);
    fclose(f);
    return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
broskyy
  • 11

1 Answers1

1

You have a an expression where you want an assignment:

while((c==fgetc(f))!=EOF)

->

while((c=fgetc(f))!=EOF)

Since c is not initialised, this is very likely to result in "garbage" being compared to input, probably "false".
Then "false" gets compared to "EOF".
That translated more of less to "0!=-1", which is true.
This turns the loop into an endless loop.
Which prevents any output ever happening.

You should also take Anttii Haapalas hint to use the right type for c.

Another way of ending your program wihtout output seems to be a failing fileopen. To diagnose that change to this:

if(f==NULL)
{
  printf("File open error, leaving!\n");
  return -1;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • I did not noticed the c==fgetc(f), thanks for that.Ive changed that, and the int c, but still no display in output.Ive made basic program, FILE*f=fopen("something.txt","r"); if(f==NULL) return -1. The something.txt is empty so it should return -1, but again no display.I know what your thinking,false path to .txt but ive checked that numerous times. – broskyy May 25 '18 at 07:54
  • Please edit your question, adding something like "EDIT: After changing to `=` is still have the problem ..." and additionally show the new code. Be careful not to change the question to something completly different; in that case a new question would be appropriate. Elaborating on "still no output" seems however fair. Just give me some 14 hours to react, I am off to work now. – Yunnosch May 25 '18 at 08:00
  • Insert a `printf("Error, leaving!\n");` inside the `if`, before `return -1;`. Use `{}` to do so. – Yunnosch May 25 '18 at 08:01
  • Opening an empty file does not necessarily fail. – Yunnosch May 25 '18 at 15:28
  • Please edit your question to also show the new code. – Yunnosch May 25 '18 at 15:29