【发布时间】:2015-02-22 03:55:34
【问题描述】:
我最近开始学习 C,但是,我正在编写一小段示例/练习代码,它恰好显示错误。我使用了文件描述符,并且“打开”命令中的一些标志不起作用,即使我似乎包含了正确的头文件。这可能是我看过的一个简单问题。
问题出现在“S_IRUSR”和“S_IWUSR”似乎未定义的地方。在此之后我没有再写任何代码,所以我将发布我所拥有的所有内容。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
void usage(char *prog_name, char *filename)
{
printf("Usage: %s <data to add to %s>\n", prog_name, filename);
exit(0);
}
void fatal(char *);
void *ec_malloc(unsigned int);
int main(int argc, char *argv[])
{
int fd; //file descriptor
char *buffer, *datafile;
buffer = (char *) ec_malloc(100);
datafile = (char *)ec_malloc(20);
strcpy(datafile, "/tmp/notes");
if (argc > 2)
usage(argv[0], datafile);
strcpy(buffer, argv[1]);
printf("[DEBUG] buffer\t @ %p: \'%s\'\n", buffer, buffer);
printf("[DEBUG] datafile\t @ %p: \'%s\'\n", datafile, datafile);
strncat(buffer, "\n", 1);
//opening file - this line of code is causing the problem.
fd = open(datafile, O_WRONLY |O_CREAT | O_APPEND, S_IRUSR|S_IWUSR)
}
void fatal(char *message)
{
char error_message[100];
strcpy(error_message, "[!!] Fatal Error ");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
void *ec_malloc(unsigned int size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
就像我说的那样,我不相信那里有任何拼写错误,据我所知,那里有正确的标题,但如果我错了,请纠正我。感谢您的帮助。
【问题讨论】:
-
“我不相信那里有任何错别字” - 好吧,首先是
S_IWSUR。
标签: c file io flags file-descriptor