C I / O程序无法正常工作

时间:2015-02-22 03:55:35

标签: c file io flags file-descriptor

我最近开始学习C语言,但是,我正在编写一个小样本/练习代码,它恰好显示错误。我使用文件描述符和“打开”中的一些标志。命令arn工作,甚至认为我似乎包含了正确的头文件。它可能是一个我看过的简单问题。

问题出在' 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;
}

就像我说的那样,我不相信那里会有任何拼写错误,据我所知,正确的标题就在那里,但如果我错了,请纠正我。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

你忘了包含stdio.h,并在S_IWUSR上输入错误,并忘记了半冒号。

#include <stdlib.h>
#include <stdio.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;
}

答案 1 :(得分:0)

一般来说,包含编译器告诉您的确切消息是非常有用

在这种情况下,我的编译器会报告:

cc     foo.c   -o foo
foo.c:8:5: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)'
    printf("Usage: %s <data to add to %s>\n", prog_name, filename);
    ^
foo.c:8:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
foo.c:35:63: error: use of undeclared identifier 'S_IWSUR'
    fd = open(datafile, O_WRONLY |O_CREAT | O_APPEND, S_IRUSR|S_IWSUR) 
                                                              ^
foo.c:44:5: warning: implicit declaration of function 'perror' is invalid in C99 [-Wimplicit-function-declaration]
    perror(error_message);
    ^
2 warnings and 1 error generated.

阅读本文,我们可以找到两个错误:

  1. 您需要stdio.h包含printf()
  2. S_IWSUR拼写错误。它应该是S_IWUSR