我找不到打开,编写和关闭的程序,无法在找到的源代码中进行标识。

时间:2018-11-07 15:15:16

标签: visual-studio include

这一次,我问为什么这个特定的源代码无法编译。我拥有所有适当的库,所以我想。我是否需要另一个库,以便我的打开,写入和关闭标识符满足其预期功能?

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>

void usage(char *prog_name, char *filename) {
    printf("Usage: %s <data to add to %s>\n", prog_name, filename);
    exit(0);
}

void fatal(char *);             //A function for fatal errors
void *ec_malloc(unsigned int);  //An error-checked malloc() wrapper

int main(int argc, char *argv[])
{
    int fd;                     //filedescriptor
    char *buffer, *datafile;

    buffer = (char *)ec_malloc(100);
    datafile = (char *)ec_malloc(20);
    strcpy_s(datafile, sizeof(datafile), "/tmp/notes");

    if (argc < 2)               //If there aren'tcommand line arguments,
        usage(argv[0], datafile);//display usage message and exit
    strcpy_s(buffer, sizeof(buffer), argv[1]);//Copy into buffer

    printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer);
    printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

    strncat_s(buffer, sizeof(buffer), "\n", 1); //Add newline on the end

    //  Opening file
    fd = open(datafile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
    if (fd == -1)
        fatal("in main() while opening file");
    printf("[DEBUG] file descriptor is %d\n", fd);
    //  Writing file
    if (write(fd, buffer, strlen(buffer)) == -1)
        fatal("in main() while writing buffer to file");
    //  CLosing file
    if(close(fd) == -1)
        fatal("in main() while closing file");

    printf("Note has been saved.\n");
    free(buffer);
    free(datafile);

    getchar();
    return 0;
}

//A function to display an error message and then exit
void fatal(char *message) {
    char error_message[100];

    strcpy_s(error_message, sizeof(error_message), "[!!] Fatal Error ");
    strncat(error_message, message, 83);
    perror(error_message);
    exit(-1);
}

//An error-checked malloc() wrapper function
void *ec_malloc(unsigned int size) {
    void *ptr;
    ptr = malloc(size);
    if (ptr == NULL)
        fatal("in ec_malloc() on memory allocation");
    return ptr;
}

我还想知道我是否应该使用Linux来充分利用此源代码? VS 2015是否适合我的特定应用程序。希望我有道理。在编码方面,我还是很绿的,所以大多数这些概念对我来说都是陌生的。我愿意获得所有建议。enter image description here

0 个答案:

没有答案