提问人:Sealpup 提问时间:11/10/2023 最后编辑:Some programmer dudeSealpup 更新时间:11/10/2023 访问量:46
C 我有一个数据库项目,由于错误“zsh:非法硬件指令 ./main”而尚未运行
C I have a database project that hasn't been running due to the error "zsh: illegal hardware instructions ./main"
问:
/* 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 并且如果它不稳定,则不能
答: 暂无答案
评论