C 我有一个数据库项目由于错误“zsh:非法硬件指令./main”而尚未运行

问题描述 投票:0回答:1
/* databaseLogic.c */

#include <stdlib.h>
#include <stdio.h>

const int MAX_STR_LEN = 20;

int roomAvailability(int roomChecked)
{
    FILE* fileAvailable;

    char filename[46];
    sprintf(filename, "../../databaseManagement/roomParent/room%d.txt", roomChecked);
    fileAvailable = fopen(filename, "r");
    if(fileAvailable == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    fclose(fileAvailable);
}

int roomDataIn(char userData[10][MAX_STR_LEN], int roomRequested)
{
    FILE *openRoom;
    char dataInBuffer[MAX_STR_LEN];
    char filename[46];
    sprintf(filename, "../../databaseManagement/roomParent/room%d.txt", roomRequested);
    openRoom = fopen(filename, "r");
    fclose(openRoom);
    if(openRoom == NULL)
    {
        openRoom = fopen(filename, "w");
        for(int i=0;i<9;i++)
        {
            sprintf(dataInBuffer, "%s\n", userData[i]);
            fputs(dataInBuffer, openRoom);
        }
    }
    else
    {
        printf("not found");
        return -1;
    }
    fclose(openRoom);
    return 0;
}
char  *roomDataOut(int roomRequested, int requestedLine)
{
    
    FILE *openRoom;
    char filename[46];
    char *pulledData = (char *)malloc(MAX_STR_LEN*8);

    sprintf(filename, "../../databaseManagement/roomParent/room%d.txt", roomRequested);
    openRoom = fopen(filename, "r");
    if(openRoom != NULL)
    {
        
        char tempStorage[MAX_STR_LEN];
        for(int i=0;i<requestedLine-1;i++)
        {
            fgets(tempStorage, 20, openRoom);
        }
        fgets(pulledData, 20, openRoom);
        fclose(openRoom);
        return pulledData;
    }
    printf("failure");
    return "failure";
    
}
/* databaseLogic.h */

extern int roomDataIn(char *userData, int requestedRoom);

extern int roomAvailability(int requestedRoom);

extern char *roomDataOut(int requestedRoom, int requestedLine);
/* main.c */

#include <stdio.h>
#include "checkIn.h"
#include "checkOut.h"
#include "databaseLogic.h"
const char num1[2];

int main() 
{
    char test[10][10] = {"1234567890", "1234567890", "1234567890", "1234567890", "1234567890", "1234567890", "1234567890", "1234567890", "1234567890", "1234567890"};
    printf("%d", roomAvailability(3));
    printf("%d", roomDataIn(*test, 3));
    printf("%s", roomDataOut(3, 3));

    return 0;
}

进入我输入的终端:

clang main.c checkIn.c checkOut.c databaseLogic.c -o main
./main

我期望它使用 roomAvailability 检测到那里没有文件,使用 roomDataIn 创建文件,然后使用 roomDataOut 从文件中读取,但我从终端收到错误消息:“zsh:非法硬件指令 ./main”

我在开始使用 malloc 后才开始收到此错误,但这是必要的,因为我需要将字符串传递回 main,并且如果其易失性则不能

c macos malloc zsh file-handling
1个回答
0
投票

我的朋友告诉了我答案,所以将其发布在这里以帮助其他人。 它的 malloc 太大了,因为它需要以字节为单位而不是位的大小,这就是我现在的想法,因为它的 malloc (最大字符串长度 + 2)它可以工作 :)

© www.soinside.com 2019 - 2024. All rights reserved.