0

i'm having some trouble using lseek and the buffer for an assigment. The objective is to read a file and change every letter 'a' to a '?'. I'm running some minor programs to understand how the functions and buffer works and i'm having some trouble.. Imagine that my file "teste" has only "abcabcabc" in it. If i do this:

int fd = open("teste", O_RDWR);
char buf[1];
int fptr = lseek(fd, 0, SEEK_SET);
fflush(stdout);
read(fd, buf, 1);
printf("%s\n", buf);

i get on my console "a", so it reads well the first letter, because i put the pointer to the beggining. But if i do a if condition before the printf, comaparing buf to 'a', like:

if(buf == 'a') printf("%s\n", buf);

It doesn't work, it doesn't print anything, so it doesn't enter the if statement.. I need to compare the buffer to letters so i can change all 'a' of the file.. How can i do this guys?

Ok, this part is already solved due to the answers bellow, but now i'm trying to read all the file and compare each charecter to 'a', making a simple printf just to see if it's working.. i wrote this:

int fd = open("teste", O_RDWR);
char buf[1];
int fptr = lseek(fd, 1, SEEK_SET);
fflush(stdout);
read(fd, buf, 1);
while(fptr > 0){
  read(fd, buf, 1);
  if(buf[0] == 'a'){
    printf("%s\n",buf);
  }
  fflush(stdout);
  fptr=lseek(fd, (off_t)(1), SEEK_CUR);
}
close(fd);

But it's now working.. It prints only one'a', and then doesn't close and don't do anything.. Its like an infinite cycle but without entering the if statement. What's wrong?

César Pereira
  • 249
  • 4
  • 17
  • `printf("%s\n", buf);` is [UB](https://en.wikipedia.org/wiki/Undefined_behavior). `%s` wants a `NULL` terminated string. – LPs Mar 24 '16 at 16:09
  • UB? @LPs what does that mean? – César Pereira Mar 24 '16 at 16:11
  • but without the if statement it gives me the right output... The problem is comparing buf with 'a'.. – César Pereira Mar 24 '16 at 16:12
  • In C a string literal is between double-quotes. A single character literal is in single-quotes. Also in C a string is an array of character bytes that ends with a zero, aka `'\0'`. Your `char buf[1]` is too small to contain a zero and so your printf is likely to continue reading memory until it finds one, and that is likely to result in random-looking strings. Since your `buf` is an array `buf == 'a'` is actually something like `0x601035 == 0x61` which will never match. Your compiler should have yelled loudly at you about mismatched variable types. If it didn't, increase the warning level. – Zan Lynx Mar 24 '16 at 16:23
  • Your printf still wrong... – LPs Mar 24 '16 at 17:09
  • When i changed %s to %c i get "w" on the console, and i don't even have any 'w' on my file O.o – César Pereira Mar 24 '16 at 17:11
  • Because you didn't change buf to buf[0], I guess – LPs Mar 24 '16 at 17:13
  • i did change them both – César Pereira Mar 24 '16 at 17:15
  • Take note that read already move fd forward of 1 char each time a char is read. You do not need lseek inside loop. – LPs Mar 24 '16 at 17:18
  • that is true... I can't read all sentence. Can you write me a code of how can i do it? Read each charecter and compare each? – César Pereira Mar 24 '16 at 17:20
  • I'm sure you didn't wrote into you code printf("%c\n", buf[0]); – LPs Mar 24 '16 at 17:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107254/discussion-between-cesar-pereira-and-lps). – César Pereira Mar 24 '16 at 17:24

2 Answers2

2
  1. printf("%s\n", buf); is UB. %s wants a NULL terminated string. Use printf("%c\n", buf[0]);
  2. if(buf == 'a') must be if(buf[0] == 'a'). You are comparing address of buf with 'a' char but you want to compare the content of first (an unique) cell of buf array.
LPs
  • 16,045
  • 8
  • 30
  • 61
  • When i change the printf it prints me 'w', and i don't even have any 'w' on my file.. With your second suggestion it already works.. But now can you see what i added to the question and help me out on that stage now? @LPs – César Pereira Mar 24 '16 at 17:12
  • I commented the post. See there. – LPs Mar 24 '16 at 17:19
1

buf is a pointer containing the address. So the conditon

if(buf=='a')

becomes false and doesnt goes into if block.
Try using

if(*buf=='a') OR if(buf[0]=='a')

kvorobiev
  • 5,012
  • 4
  • 29
  • 35
Mukesh
  • 83
  • 6
  • thanks, that worked.. Can you see what i added to the question and help me out at that stage now? @MukeshKumarChaursiya – César Pereira Mar 24 '16 at 17:05
  • use while(*buf != '\n') and in printf use printf("%c\n",*buf); and remove dat line calling lseek in while – Mukesh Mar 24 '16 at 20:13