2017-05-07 41 views
-1

我试图打开文件并使用C追加到它们。我基于进程ID动态命名目录,并根据随机选择的“房间”创建文件名在循环。我的意图是打开文件,将房间名称附加到文件中,然后关闭文件并移动到下一个房间并执行相同的功能。我遇到的问题是“开放”。它似乎只返回-1,这表明一个错误。该错误消息指出“权限被拒绝”。我很困惑,因为我似乎在open函数中设置了适当的权限。我尝试过使用fopen(),但是这导致产生了分段错误11.我的roomFilePath声明和用法有问题,还是我的打开用法不正确?以下是包含该问题的代码部分。 makeRooms()函数是我检查文件是否被正确打开的地方。谢谢!尝试追加到文件时拒绝的权限C

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <unistd.h> 
#include <sys/stat.h> 
#include <string.h> 
#include <errno.h> 
#include <fcntl.h> 

#define NUM_ROOMS 10 
#define NUM_USED_ROOMS 7 
#define MAX_CONNECTIONS 6 

time_t t; 
char* usedRooms[NUM_USED_ROOMS]; 
int i; 
char directoryName[100]; 
char* baseDirectory = "walterer.rooms."; 
int processId; 
char roomFilePath[75]; 
int adjacencyMatrix[7][7] = {0}; 
int useableConnections; 
int e; 
int totConnections = 0; 
int openRoom; 
int file_descriptor; 

char* roomNames[] = { 

    "cleveland", 
    "columbus", 
    "dallas", 
    "toledo", 
    "miami", 
    "sarasota", 
    "boston", 
    "chicago", 
    "denver", 
    "phoenix" 


}; 

int connections[10] = { 
    0, 
    0, 
    0, 
    0, 
    0, 
    0, 
    0, 
    0, 
    0, 
    0 
}; 


void makeDirectory() { 

    processId = getpid(); 
    sprintf(directoryName, "%s%d", baseDirectory,processId); 
    //printf("%s\n", directoryName); 
    mkdir(directoryName, 777); 

} 

void makeRooms() { 

    /* Initializes random number generator */ 
    srand((unsigned) time(&t)); 

    /* Create 7 rooms */ 
    for(i = 0; i < NUM_USED_ROOMS;){ 

     /* Generate random number between 0 to 10 */ 
     int randomNumber = rand() % NUM_ROOMS; 

     /* Loop as long the array does not contain any connections at the index */ 
     while(connections[randomNumber] == 0) { 

      /* Append the room path to the end of my ONID path */ 
      sprintf(roomFilePath,"%s/%s", directoryName, roomNames[randomNumber]); 
      printf("%s\n",roomFilePath); 

      /* Create file */ 
      FILE *filePointer; 

      /* Open file to append*/ 
      //filePointer = open(roomFilePath, O_WRONLY | O_CREAT, 0600); 


      //!!!Returning -1 
      file_descriptor = open(roomFilePath, O_APPEND, 0600); 
      printf("%d\n",file_descriptor); 
      if (file_descriptor == -1) 
      { 
       printf("open() failed on \"%s\"\n", roomFilePath); 
       perror("In createRooms()"); 
       exit(1); 
      } 


      /*if (filePointer == NULL)  
      { 
       fprintf(stderr, "Error Creating File\n"); 
       printf("something went wrong with read()! %s\n", strerror(errno)); 
      }*/ 
      /* Print the room name in the file */ 
      /* SEG FAULT HERE!!!! */ 
      fprintf(filePointer, "ROOM NAME: %s\n", roomNames[randomNumber]); 

      /* Close the file */ 
      //fclose(filePointer); 
      usedRooms[i] = roomNames[randomNumber]; 
      connections[randomNumber] = 1; 

      //printf("Room %d is %s \n", i+1, roomNames[randomNumber]); 
      i++; 
     } 

    } 

} 
+0

尝试以管理员身份运行您的代码(.exe)。 –

回答

0

首先,你在这一行缺少必需的flags参数部分open()

 file_descriptor = open(roomFilePath, O_APPEND, 0600); 

你传递的唯一open()标志O_APPEND,但open()还需要至少一个的O_RDONLY,O_WRONLYO_RDWR,O_EXECO_SEARCH标志。 (最后两个很少使用。)

你的代码应该是这样的

 file_descriptor = open(roomFilePath, O_RDONLY | O_APPEND, 0600); 

the POSIX standard for open()

提要

#include <sys/stat.h> #include <fcntl.h> 

int open(const char *path, int oflag, ...); 

...

oflag的值由以下列表中的标志 的按位或值构成,该列表在<fcntl.h>中定义。申请应 指定的oflag值的前五个值(文件存取方式)以下 的只有一个:

了O_EXEC

公开赛只执行(非目录文件)。如果将此标志应用于目录,则结果为 未指定。

O_RDONLY

仅供阅读。

O_RDWR

可供阅读和写作。如果将此标志 应用于FIFO,则结果不确定。

O_SEARCH

打开的目录仅用于搜索。如果将此 标志应用于非目录文件,结果未指定。

O_WRONLY

仅供打印。

在你贴的代码,当然你在该行获得分割故障:

 /* SEG FAULT HERE!!!! */ 
     fprintf(filePointer, "ROOM NAME: %s\n", roomNames[randomNumber]); 

filePointer变量在你发布的代码没有被初始化,所以使用值导致分段错误。

相关问题