Help with File Descriptor?...

Hello Guys!
Im using file descriptor but.. i've tryied to new line with no success :/
Is there a possibility to go at new line with file descriptor?

I open the FD as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char buf[] = "try";
static int flag = 1; //If 1, file never opened. If 0, file already opened during run
if(flag){
	if((fd = open(PATH_IPCS, O_CREAT | O_RDWR | O_TRUNC)) != -1){
		TEST_ERROR
		printf("File descriptor opened, value: %d\n", fd);
		write(fd, buf, sizeof(buf));
		flag--;
	}else{
		printf("Can't open the file\n");
		TEST_ERROR
	}
}else{
	if((fd = open(PATH_IPCS, O_CREAT | O_RDWR | O_APPEND)) != -1){
		printf("File descriptor already opened, value: %d\n", fd);
		write(fd, buf, sizeof(buf));
	}else{
		printf("Can't open the file\n");
		TEST_ERROR
	}
}


maybe is the Append? I don't know..
When i open the file, is saved as: trytrytrytry
All in the same line..

P.S If someone of you can help me to optimize these line.. i can't find another way to check if file has already opened (because if yes, i can't overwrite the file, but just append)
THanks all..
Last edited on
> char buf[] = "try";
...
> When i open the file, is saved as: trytrytrytry
Well write() doesn't automatically append a newline.

If you want lines, you need to write \n's yourself.


Nothing.. it-s incredible.. ican't go to new line :/
I tryied even with this try code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#define PATH "/tmp/try.txt"

int main(){
	int fd = 0;
	for(int i = 0; i < 2; i++){
		char buf[10] = {"try\ntry"};
		if((fd = open(PATH, O_CREAT | O_RDWR | O_APPEND)) != -1){
			printf("File descriptor opened, value: %d\n", fd);
			write(fd, buf, sizeof(10));
			write(fd, "\n", 0);
		}
	}
	exit(EXIT_SUCCESS);
}


Nothing.. i've inserted '\n' everywhere..
Last edited on
Nothing.. now it works.. thanks a lot
Line 10 and 11. Check the last parameter.

Review the meaning of sizeof.
Last edited on
Registered users can post here. Sign in or register to post.