Unix - Writing a line from a file to a char* var,

2019-09-27 00:21发布

Question: I have a file, called ATM1, and it is filled with strings, for example(this is the format for everyline): O ilan 123 456 Which means - O stands for open account, ilan is username, 123 is password, and 456 is initial amount in my bank account.

After opening the file, iterating with a while loop while(((ret_in = read (in1, &buffer1, BUF_SIZE)) > 0)), I want to get the line's details and store them in the appropriate variables. for example the first char will be stored in a variable called letter or msg[0] whatever is more convenient for you, then there is a space and then username, then password, and optional stuff like balance, or maybe another account id (for transfer money purposes).

Every ATM machine should be a thread, it has its own file, for now it is just one file "ATM1" because I want it to work in the beginning for at least one file.

Current Problem: Segmentation fault in the OpenFile function. I'm still not able to store the line's values in the appropriate variables and called the switch statement for opening account, etc.

Here is the current code:

#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);
}

1条回答
贼婆χ
2楼-- · 2019-09-27 00:46

Well, for one, you use i as an array index without ever initializing it. That could easily cause a SEGFAULT.

But honestly this whole thing is a mess. Your function names don't do what they say they do. You appear to be thrashing around almost randomly. I suggest you rethink your design from the beginning. Go through the "top down" design process you should have learned and figure out how to factor your code. Only then should you proceed.

查看更多
登录 后发表回答