0

I would like to know how can I read the charaters into a buffer in Python? In C code, I can easy decralare the buffer character like char buffer[256];:

void read_char(int x, char buffer[], int *flag_stop) {
int i, length;
char character;

i = 0;
bzero(buffer, 256);

do {
    if ((length = read(x, &character, 1)) <= 0) 
    {
        *flag_stop = 1;
        break;
    }
    buffer[i] = character;
    i++;
}
while(c != 0x0A);

}

But I don't know how to do in Python so the code is something like this:

def read_char(x,buffer,**flag_stop):
i = 0  
buffer = np.array([], dtype='S64')  
while True:  
    if os.read(x, character, 1) <= 0: 
        **flag_stop == 1
        break
    buffer[i] = str(character)
    i=i+1
    if(character != 0x0A):  
        break

I have tried with numpy.chararray but I did not work. Any idea for this problem? thank you very much!

fpga
  • 11
  • 1
  • 4
  • `flag_stop` doesn't seem to do anything in your function, what is its intended use? Also, the `buffer` array as you declare it has a length of 0 - you can use something like [`numpy.zeros`](https://numpy.org/doc/stable/reference/generated/numpy.zeros.html) or [`numy.empty`](https://numpy.org/doc/stable/reference/generated/numpy.empty.html) to declare an array of your desired size (a list would be better if you want to dynamically change its size)... also you're overriding the argument to your function `buffer` with the local variable... – AJ Biffl Oct 27 '21 at 08:02
  • flag_stop is used in my main prog, if flag_stop = 1 the program will stop. I will tried with numpy.zeros and numpy.empty as you said. Thanks – fpga Oct 27 '21 at 08:11
  • I don't know how to decrare the flag in Python so I put ** before the flag_stop, is it correct? – fpga Oct 27 '21 at 08:12
  • ** is used to denote a dictionary of keyword arguments - see [here](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) or [here](https://www.geeksforgeeks.org/args-kwargs-python/) . As far as I know, there is no way to explicitly set underlying C exit flags. If I understand what you want to do, instead you can raise an [exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions) or call [`sys.exit`](https://docs.python.org/3/library/sys.html#sys.exit) or something similar to exit the program – AJ Biffl Oct 27 '21 at 08:22
  • The flag_stop has been removed in the main prog. But I have another question about the declaration variable, for instance in the C prog, I can do in the function. However, in Python I don't need to declaire it. But when I compile and run it, there is a error: NameError: global name 'character' is not defined. Sorry if I posed stupid question – fpga Oct 27 '21 at 08:39
  • you have to use `=` inistead of `==`. But Python can't assing integer value to external variable and it treats `flag` as local variable. You woull have to rather use `return flag` and `flag = read_char(x, buffer)` – furas Oct 27 '21 at 10:56
  • you shoud create `buffer` outside `read_char` and send it as variable - or you should use `return buffer, flag` and `buffer, flag = read_char(x)` – furas Oct 27 '21 at 10:59
  • as for `character` - you don't have to declare it if you want to assign value to `character` but you want to get value from `character` in `os.read()`. But main problem is that you try to use `os.read()` exactly like in C `read()` but it works different - `character = os.read(x, 1)` – furas Oct 27 '21 at 11:04
  • Yes, you're correct; Infact I want to get the value from `character` which are defined in the main program (their name can be changed). – fpga Oct 27 '21 at 13:00

1 Answers1

0

Your main problem is that you want in Python write code exactly like in C.

Python would need something like this

def read_char(x):
    flag_stop = False  # or 0
    i = 0
    buffer = np.array([], dtype='S64')
    
    while True:
        character = os.read(x, 1)
        if not character:   # if len(character) < 0
            flag_stop = True # or 1
            break
        buffer[i] = character
        i += 1
        if character != 0x0A:  
           break

    return buffer, flag_stop

# ---

buffer, flag_stop = read_char(file_handler)

I don't like str() in your code - you may get single byte which is part of mulit-bytes code (like utf-8) and converting to str() can only mess it.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank @furas for your answer, there is still problem with os.read(x,1) which make function returned interger value but basically it worked like the function in C. Do you have a solution to replace os.read? – fpga Oct 27 '21 at 12:40