-5

So I have this project where I need to open a file and print the contents in C, but I have to use unix system calls. However I'm not to clear as to why nothing prints. I try to run the code, but it seems to skip straight to the close error. I'm aware I need unistd.h, I already have that included.

#define MAXBUF 4096
int main(int argc, char* argv[]){
int x = 0, y, z = 1, a;         //x: Counter, Y:int for open, z:another counter, a:int for read
char buff[MAXBUF];              //set buffer size
int fe,fn,ft;                   //fe = found E, fn = found n, ft = found T
if((y = open( argv[2], O_RDONLY )) == -1){
    errHandler("Couldn't open ", argv[2]);
    }
            .
            .
            .

if(fn  == 1){                                   //if fn returned true
    printf("%d ", z++);
    }

while( ( a = read( y, buff, MAXBUF ) ) > 0 ){           

    if ( buff == '\n' && fe && fn )        //<--
        printf("$\n%4d:  ", z++ );
    else if ( buff == '\n' && fe )
        printf("\n");                      //Flags for various options
    else if ( buff == '\t' && ft )         
        printf("^T");
    else if ( buff == '\n' && fn )
        printf("\n%4d:  ", z++ );          //<--
    else
        printf("%c", buff);
}
close(y);
if ( close( y ) == -1)
    printf("Unable to close file");
return 0;
}

edit: Here's the declarations. The '.' are skipping the code that's just setting the flags.

  • 2
    You closed the file twice. You should remove the first close instance. – hesham_EE Oct 13 '15 at 21:57
  • What is `buff`? The type signature for `read` requires it to be a `void*`, but you're comparing it against `char`s. – jwodder Oct 13 '15 at 21:58
  • 3
    Please post an [MCVE](https://stackoverflow.com/help/mcve). We can see some possible problems in your code (such as pointed out by jwodder) but we can't give a definitive answer unless you show all the relevant code in an MCVE. For example, the logic relies on variable values that you have not shown (`fe`, `fn`, `ft`, etc). – kaylum Oct 13 '15 at 22:02
  • @jwodder Well, the type signature requires it to be some kind of pointer, typically `char *`. – melpomene Oct 13 '15 at 22:05
  • Tab (`'\t'`) is `^I`, not `^T`. – melpomene Oct 13 '15 at 22:07

1 Answers1

1

In your:

if ( buff == '\n' && fe && fn )        //<--

I assume you want to check if an empty line was read by checking it against the newline character. However, buff is not the character; it is a pointer to were all the characters read are stored. To check if the first character of buff is the newline character, use:

if ( *buff == '\n' && fe && fn )        //<--

The * dereferences the pointer and gives you the first character. Same for your other checks. If these checks fail, then print the line:

else
    printf("%s", buff);

Note the formatting character %s, not %c, to print a string (the buffer) instead of a single character.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Given the original code, I can see how you got *buff, but because buff is defined as a char array, I'd use buff[0] for cleanliness. If we had an additional char *bufp = buff, I'd go with *bufp (or bufp[0]) – Craig Estey Oct 13 '15 at 23:28