【问题标题】:C I/O program not workingC I/O 程序不工作
【发布时间】: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


【解决方案1】:

您忘记包含 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;
}

【讨论】:

  • 在下面查看我的答案:D
【解决方案2】:

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

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

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. 您需要为printf() 包含stdio.h
  2. S_IWSUR 拼写错误。应该是S_IWUSR

【讨论】:

  • 它没有正确复制;是的,我已经包含了所有这些东西,但它仍然无法正常工作。我使用带有 Visual Basic 的 C/C++ 编译器,它说这两个标志是未定义的。
  • @DanielBaron:您能否在问题中包含您收到的确切错误消息?也就是说,你能不能把它复制(或者截图)直接逐字复制到你的问题中?
  • 'error C2065: 'S_IRUSR' : undeclared identifier' 和 'error C2065: 'S_IWUSR' : undeclared identifier' 和这个警告 'warning C4013: 'open' undefined 假设 extern 返回 int'
  • @DanielBaron:你的编译器是什么(包括版本)?大概这是 Visual Studio 的某个版本?
  • Mircrosoft Visual Studio Express 2013 for windows 桌面。版本 12.0.21005.1 REL 和 Microsoft .NET Framework。版本 4.5.51209
猜你喜欢
  • 2014-05-17
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
相关资源
最近更新 更多