質問:私はATM1というファイルを持っていて、これは例えば文字列で埋められています(これは、 ):O ilan 123 456
- Oはオープンアカウントを表し、ilanはユーザー名、123はパスワード、456は私の銀行口座の初期金額を意味します。Unix - ファイルからchar * varに行を書き込んだ後、整数に保存する
ファイルを開いた後、whileループwhileループwhile(((ret_in = read (in1, &buffer1, BUF_SIZE)) > 0))
を実行した後、その行の詳細を取得して適切な変数に格納します。たとえば、最初の文字はletterまたはmsg [0]と呼ばれる変数に格納され、その後にスペースがあり、次にユーザー名、パスワード、および残高などのオプションがあります。送金目的)。
すべてのATMマシンはスレッドでなければなりません。それは独自のファイルを持っています。少なくとも1つのファイルに対して最初から動作したいので、ただ1つのファイル "ATM1"です。
現在の問題:OpenFile
機能で セグメンテーションフォールト。私はまだ適切な変数に行の値を格納することはできませんよと口座を開くためのswitch文と呼ばれる、など
ここでは、現在のコードです:いずれかの
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#define BUF_SIZE 8192
sem_t log;
void OpenNewAccount(int acc,int pw,int amount){
}
struct Account{
int number;
int password;
int balance;
};
//*Opens file for every ATM
void* openFile(void* args){
//To add later: while(true) { sleep(100); do next file's line }
//Open file
int* aargs = args;
int acc;
int pw;
int amount;
int target_acc;
int ret_in, in1,file;
char buffer1[BUF_SIZE];
int count = 0;
int i = 0;
char fn[5] = "ATM1";
char* msg;
file = open(fn,O_RDONLY|O_CREAT,0644);
while(((ret_in = read (file, &buffer1, BUF_SIZE)) > 0))
{
for(i; i<ret_in; i++)
{
if(buffer1[i]!='\n')
msg[i] = buffer1[i];
/* else{
if(count == 0){
count++;
break;
}
else
{
count = i + 1;
break;
}
}
*/
}
}
printf("%s", msg); //TEST: check msg
//Here we translate the message
/*
//Here we call the relevant function of the msg
switch (msg[count - 1]){
case 'O': OpenNewAccount(acc,pw,amount);
case 'D': Deposit(acc,pw,amount);
case 'W': Withdrawl(acc,pw,amount);
case 'B': Balance(acc,pw);
case 'Q': CloseAccount(acc,pw);
case 'T': Transfer(acc,pw,target_acc,amount);
}
*/
}
//*finish: Update log.txt and lock it
void WriteLog(char* msg){
int file;
char logName[8] = "log.txt";
sem_wait(&log);
file = open(logName,O_WRONLY|O_CREAT,0644);
strcat(msg,"\n");
write(file,&msg,strlen(msg));
close(file);
sem_post(&log);
}
int main(void)
{
int i,n,a;
sem_init(&log, 0, 1);
printf("Please enter the number of ATMs you want: \n");
scanf("%d", &n);
int bank; //bank with n ATMs
pthread_t thread[3];
printf("test\n"); //TEST: check msg
for(i = 0; i < 3; i++)
pthread_create (&thread[i] , NULL , openFile , &i);
scanf("%d",&a);
}
'msg [i] = buffer1 [i];' msgは何も指しません。 – wildplasser
@wildplasser char * msg; –
アクションがOの場合、コードはオープン、デポジット、引き出し、残高、クローズ、転送を順番に実行します。この一連の操作を防ぐには、各ケースの後に 'break;'が必要です。 –