Unix-将文件中的一行写入char * var,然后将其保存为整数 [英] Unix - Writing a line from a file to a char* var, then saving it in integers

查看:109
本文介绍了Unix-将文件中的一行写入char * var,然后将其保存为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一个名为ATM1的文件,其中充满了字符串,例如(这是每行的格式):O ilan 123 456这意味着-O代表未结帐户, ilan是用户名,123是密码,456是我的银行帐户中的初始金额.

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.

打开文件后,使用while循环while(((ret_in = read (in1, &buffer1, BUF_SIZE)) > 0))进行迭代,我想获取该行的详细信息并将其存储在适当的变量中.例如,第一个字符将存储在一个名为letter或msg [0]的变量中,这对您来说更方便,然后是一个空格,然后是用户名,然后是密码,以及诸如余额之类的可选内容,或者另一个帐户ID(用于汇款目的.)

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).

每台ATM机都应该是一个线程,它有自己的文件,因为我希望它至少在一开始就可以工作,所以现在它只是一个文件"ATM1".

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.

当前问题: OpenFile函数中的分段错误.我仍然无法将行的值存储在适当的变量中,并调用了switch语句来开设帐户等.

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.

这是当前代码:

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

推荐答案

好吧,有一个例子,您无需将i用作数组索引就可以对其进行初始化.这很容易导致SEGFAULT.

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.

这篇关于Unix-将文件中的一行写入char * var,然后将其保存为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆