writing to file is not readable by user


 
Thread Tools Search this Thread
Top Forums Programming writing to file is not readable by user
# 1  
Old 12-03-2010
writing to file is not readable by user

In the following code segment I write to some file using
Quote:
write()
, but this write is not readable by me when i open the file. any helps would be thankful.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<ctime>
#include<cmath>
#include<iostream>
using namespace std;
#define FILE_MODE  (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define _PARAM 5
#define _RAND_RANGE 100
#define DOWNLOADER_FIFO "/tmp/fifo.0"

int main(int argc, char **argv)
{

     int fd, value = 19;
   int n;
   char c[10];

   if( (fd = open("file.txt", O_WRONLY | O_CREAT, FILE_MODE) ) < 0 ){
        perror("file creation failed..\n");
          return false;
   }      sprintf(c, "%d", value);
          if((n = write(fd, c, sizeof c)) < 0){
                  perror("\n");
                     return false;}
   close(fd);

  return 0;
}


Last edited by saman_glorious; 12-03-2010 at 01:33 PM..
# 2  
Old 04-29-2011
Looks good, but maybe the file was already there chmod 200 ? Man Page for open (All Section 2) - The UNIX and Linux Forums
# 3  
Old 04-29-2011
What are the permission for the created file.txt ? Can you check the file mode creation mask of your shell (simply type umask in the shell).

cheers, Loïc
# 4  
Old 04-29-2011
The shell umask is a call to umask() to set the process mask, which is inherited, like signals, environment and fd (but they may be close-on-exec), and is not stored within the shell programs's allocated variables.

The open man page says:
The argument mode specifies the permissions to use in case a new file is created. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask). Note that this mode only applies to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor.
# 5  
Old 04-29-2011
Quote:
The open man page says:
The argument mode specifies the permissions to use in case a new file is created. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask). Note that this mode only applies to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor.
Yeah, right. Thanks.

But if the file.txt has not the right permission, I'd expect a "file creation failed..." error message?

Cheers, Loïc
# 6  
Old 04-30-2011
after transfered a txt file from pc to unix using FTP, how can i open it in unix?
anybody can help?
# 7  
Old 05-02-2011
Check out the modes you have specified.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert UNIX timestamp to readable format in the file

Hello I have a file : file1.txt with the below contents : 237176 test1 test2 1442149024 237138 test3 test4 1442121300 237171 test5 test7 1442112823 237145 test9 test10 1442109600 In the above file fourth field represents the timestamp in Unix format. I found a command which converts... (6 Replies)
Discussion started by: rahul2662
6 Replies

2. UNIX for Dummies Questions & Answers

How do I make this file readable/printable?

When I do the file I get ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped I am almost 100% sure I was able to print a readable version of this file in the past but I cannot remember how. Is it possible to convert this file into something that can be read and or... (3 Replies)
Discussion started by: fsanchez
3 Replies

3. Shell Programming and Scripting

Writing to User-Specified File

Hi, I'm writing an awk script to remove redundant XML data. I plan on running the script with the following line: cat xmlFile.xml | awk -f scriptFile I want the user to be able to choose the filename that the slimmed down XML code is written to. All of the writing to the slimmed-file is... (2 Replies)
Discussion started by: jeg90
2 Replies

4. Shell Programming and Scripting

check if file is readable by others

hi all, in ksh script how do i detect if a file is readable by others ?? thanks. (6 Replies)
Discussion started by: cesarNZ
6 Replies

5. Shell Programming and Scripting

How to check if a file is not readable by anyone except the owner?

All, I have a script where I get a filename as input and do some processing with the file that I got as input. Requirement: Now I have a requirement where I need to check the following: If either of this goes wrong, the script should pop out a warning message. I tried searching the... (6 Replies)
Discussion started by: bharath.gct
6 Replies

6. Shell Programming and Scripting

check whether file is readable or not in ksh

i want to check the readability of a file inside the script. when i use if then echo the file "$sourcef" is not readable else echo something fi i am getting the error : f: unknown test operator when i tried to check the availability with if i was... (3 Replies)
Discussion started by: gotam
3 Replies

7. UNIX for Dummies Questions & Answers

How to make user's qutoa in human readable format?

$ quota Disk quotas for user cqlouis (uid 1254): Filesystem blocks quota limit grace files quota limit grace /dev/sdb1 64 300000 320000 8 0 0 $ I want to make the output of command quota in human readable format? How to? As we... (2 Replies)
Discussion started by: cqlouis
2 Replies

8. UNIX for Dummies Questions & Answers

Converting binary file to readable format in Ksh

In Unix/Ksh, when I try to look inside a file it says that the file may be a binary file and if I want to see it anyway. When i say 'yes', it shows me the content filled with unreadable symbols (looks like binary). Is there a command that I can run from the Unix prompt to convert/translate that... (3 Replies)
Discussion started by: arthurs
3 Replies

9. HP-UX

file in malibox is readable format?

Hi, Files coming to mailbox are in readable format? Is there any special command to read these files. suppose i have sent a file like this megh$mailx -s "mesg" xyz@server.domain<file1.dat can xyz directly read the file from his mailbox? (1 Reply)
Discussion started by: megh
1 Replies

10. Programming

Core file without a readable stack trace

I am using gdb to examine a core file but the output contains only the method addresses in hex. Is there anyway to translate these addresses to a human-readable form? :confused: (0 Replies)
Discussion started by: ciregbu
0 Replies
Login or Register to Ask a Question