S_IRGRP and S_IROTH in DEV C++

  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

Hi ppl,

I was using cygnus and those S_IRGRP and S_IROTH I used are perfectly OK but now when I use DEV C++, it says those are undeclared so I comment them out. I mean I simply delete those argments. But then the results are totoally different. The files are read-only but of coz I wanna read those files too....anyone got an idea for this?

These are the lines

Code: [ Select ]
if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ))<0)
  printf("open error\n");
  1. if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ))<0)
  2.   printf("open error\n");

now I have to change it into

now I have to change it into
Code: [ Select ]
if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ))<0)
  printf("open error\n");
  1. if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ))<0)
  2.   printf("open error\n");


but I think the usgae is different......
Somebody please help!! :shock:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

When you change it does the program not run as expected or are you getting errors? If its not running as expected how are the results different?
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

O...first there r error messages when I compile the program. The error messages are

Code: [ Select ]
`S_IRGRP' undeclared (first use in this function)
`S_IROTH' undeclared (first use in this function)
  1. `S_IRGRP' undeclared (first use in this function)
  2. `S_IROTH' undeclared (first use in this function)


So it is like the compiler just doesn't recognize the arguments as some built-in arguments. I think the DEV C++ compiler just doesn't have that syntax.


When I was using cygnus, everything is fione. The file created can be read and written. But then the file created by DEV C++ is read-only. So I couldn't write into the file. That's the major difference between the two. So does anyone by any chance know what should be used to replace those two arguments?


Thanz!!! :D
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

So have you simply tried removing them and see if it compiles fine then?
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

Well yes that's what I said is the problem in the very first message.
You can see I deleted the
Code: [ Select ]
S_IRGRP | S_IROTH
in the codes when using DEV C++. But the thing is the file created can only be read, cannot be written. I want it to be written....and yes those two arguments "S_IRGRP | S_IROTH" are actually meaning the authority to read by a group and by others. And it's just weird that I deleted these arguments which are related to reading and the result is that I can't write into the file.

So....any idea? :D
Thanz!!
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Well, I am more of a C++ person and I would be able to have read and write capability by saying something like this

fstream(fn, ios::in | ios::out)

however that probably does you no good haha. Try this, replace O_WRONLY with O_RDWR, see if that does anything.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Also when it does try to write, does it give you an error? like maybe you have bad permissions? Write access denied or anything?
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Also another thing to try is this

Change adding to the end , 0777

So you would have code that ends up looking like

Code: [ Select ]
if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR, 0777))<0)
  printf("open error\n");
  1. if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR, 0777))<0)
  2.   printf("open error\n");


This probably will not work, but it doesn't hurt to try.
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

ooooo haha hey I think it just works now....relaly don't know why...forgot what I've changed hehe...I did try some of ur suggestions. Thanz really!!

And hey I got another problem...maybe I've asked before. Do you know how to make the contents inside the file readable? I mean yes now I've created a file and write into it. Yes it's ok now. But I wanan read what's inside. Bcoz when writting into the file. I'm actually writing char array into it. But when I open the file, all I can see are just some weird characters and strings...symbols....So do you know now to make it readable?

Here's the function to write into the file
Code: [ Select ]
short int Send(unsigned char *message, unsigned short int nbytes) {
    if ((fd = open("card.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ))<0)
    printf("open error\n");
    if (write(fd, message, nbytes) != nbytes)
        printf("write error\n");
    if (close(fd)<0)
        printf("close error\n");
    return(NO_ERR);
}
  1. short int Send(unsigned char *message, unsigned short int nbytes) {
  2.     if ((fd = open("card.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ))<0)
  3.     printf("open error\n");
  4.     if (write(fd, message, nbytes) != nbytes)
  5.         printf("write error\n");
  6.     if (close(fd)<0)
  7.         printf("close error\n");
  8.     return(NO_ERR);
  9. }


Hope you can give me some hints :D Thanz again!!
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

o stupid me...forgot to ask the most important thing. I mean first of all, for reading the data inside the file. I must have to append the data, or else that'll be meaningless to do it. So do you know how to append the data into the file? Coz I think what I'm doing now with that program above is simply overwritting everytime there's a new string. Actually I wanna make a file and treat it like a log file.

So do u know how to make it? :shock:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yeah get rid of O_TRUNC and it should append the data instead of truncating the file to a size of 0 each time. You might want to read this page, it might help you some:

http://www.opengroup.org/onlinepubs/007 ... /open.html

As far as the character array, when you read the file just make sure the file contents you are reading is stored into data of the same type that it was written in. If you do not do this you would probably get a bunch of messed up sybmols. Anyway after importing the data into the same type, then try to output that data and you should see it is fine.
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

O...yes I mean I can write into the file and the read from it using the same type, char array that is. But when I really open the file, it got all the weird strings. I'm not trying to ouput the data inside the file. I mean I'm really opening the file, and the contents inisde surely make no sense to me haha. But of coz when I printf() in the program, it's alright.

so....what do u think :shock:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Oh haha, that is because you are writing the file in binary probably. If you want to be able to read the contents of the file by lets say opening it with notepad, you do not want to write it in binary mode.
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

.........so how do I write it in the right mode? hehe :lol:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Well you are using the write function to write your bytes to a file, and I think that is what is causing it, however I am not positive. I usually do this in C++ and by default it would be writing in ASCII mode, and if I wanted to write in binary mode I would simply set a flag. See if there is another way to write to your file other than using the write function. I cannot remember how to do it using plain C.
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

So...it should be ASCII...well think I'll just search for some functions on the web then
Thanz for the tips!! :P
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yup no problem, if you figure out the answer please paste it here, I would be curious of whaty you find.

Post Information

  • Total Posts in this topic: 17 posts
  • Users browsing this forum: No registered users and 115 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.