logo
down
shadow

Is it possible to read in a string of unknown size in C, without having to put it in a pre-allocated fixed length buffer


Is it possible to read in a string of unknown size in C, without having to put it in a pre-allocated fixed length buffer

By : user3851235
Date : October 17 2020, 11:12 AM
To fix the issue you can do At the point that you read the data, your buffer is going to have a fixed size -- that's unavoidable.
What you can do, however, is read the data using fgets, and check whether the last character is a '\n', (or you've reached the end of file) and if not, realloc your buffer, and read more.
code :


Share : facebook icon twitter icon
Read CString from buffer with unknown length?

Read CString from buffer with unknown length?


By : Tangzy Isemin
Date : March 29 2020, 07:55 AM
around this issue Let's say I have a file. I read all the bytes into an unsigned char buffer. From there I'm trying to read a c string (null terminated) without knowing it's length. , Check this:
code :
#include <cstring>
#include <iostream>

class   A
{
  unsigned char buffer[4096];
  int   position;

public:
  A() : position(0)
  {
    memset(buffer, 0, 4096);
    char        *pos = reinterpret_cast<char*>(&(this->buffer[50]));
    strcpy(pos, "String");
    pos = reinterpret_cast<char*>(&(this->buffer[100]));
    strcpy(pos, "An other string");
  }

   const char *ReadString()
  {
    if (this->position != 4096)
      {
        while (std::isalpha(this->buffer[this->position]) == false && this->position != 4096)
               this->position++;
        if (this->position == 4096)
          return 0;
        void    *tmp = &(this->buffer[this->position]);
        char    *str  = static_cast<char *>(tmp);
        this->position += strlen(str);
        return (str);
      }
    return 0;
  }

};
int     main()
{
  A     test;

  std::cout << test.ReadString() << std::endl;
  std::cout << test.ReadString() << std::endl;
  std::cout << test.ReadString() << std::endl;
}
Does total packets length in tcp buffer can exceed allocated buffer size?

Does total packets length in tcp buffer can exceed allocated buffer size?


By : K. Mark
Date : March 29 2020, 07:55 AM
help you fix your problem Since 512K actually gets allocated, buffering 294879 bytes is not that surprising. On linux, when you set the SO_SNDBUF, the kernel simply doubles that amount.
If you want the socket buffer to be 256k, call setsockopt() with 128k.
Read (os.read) FIFO non-blockingly without fixed buffer size

Read (os.read) FIFO non-blockingly without fixed buffer size


By : DJ Kavas
Date : March 29 2020, 07:55 AM
Does that help Assuming that your delimiter is a you can read multiple variable length strings in a non-blocking manner, as shown in this program which counts while receiving output from a named pipe.
code :
import os
import time
import errno
import sys

io = os.open(expanduser("~/named_pipes/cob_input"), os.O_RDONLY | os.O_NONBLOCK)

# For implementing non-blocking IO
def read_pipe_non_blocking(input_pipe, size):
    try:
        in_buffer = os.read(input_pipe, size)
    except OSError as err:
        if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
            in_buffer = None
        else:
            raise  # something else has happened -- better reraise
    return in_buffer

def get_azimuth(input_pipe):
    in_buffer = read_pipe_non_blocking(input_pipe, 1)
    print(in_buffer)
    if(in_buffer is None):
        sys.stderr.write("n")
        return ""
    else:
        tmp_buffer = None
        while(tmp_buffer != "a"):
            sys.stderr.write("m")
            time.sleep(0.1)
            tmp_buffer = read_pipe_non_blocking(input_pipe, 1)
            if(tmp_buffer != None and tmp_buffer != "a"):
                in_buffer += tmp_buffer
        read_pipe_non_blocking(input_pipe, 1) #Read in the newline character and the toss it
        sys.stderr.write("\nReturning \{%s\}" %in_buffer)
        return in_buffer


i = 0
while 1:
    print i
    time.sleep(1)
    i += 1
    get_azimuth(io)
XmlReader to read from fixed length buffer

XmlReader to read from fixed length buffer


By : leonce lowet
Date : March 29 2020, 07:55 AM
should help you out Are your 1024-byte buffers coming from one of the standard, concrete implementations of System.IO.Stream? If t they are, you can just create your XmlTextReader around the base stream:
Get C string length of a 16 or 32-byte fixed-size buffer? (XMM or YMM register width)

Get C string length of a 16 or 32-byte fixed-size buffer? (XMM or YMM register width)


By : Amit Singh
Date : October 12 2020, 07:00 AM
help you fix your problem This is exactly how you implement strlen or memchr with AVX2. (For a fixed-size buffer where you know there will be a match somewhere in the buffer.)
(Except now you have a redundant and).
Related Posts Related Posts :
  • Finding a substring, using pointers
  • How to get metadata from a file in c
  • How to define a struct of several strings in it?
  • Reading columns of strings and integers from file
  • Flex/bison keeps on giving me syntax error
  • Linked list of chars in C
  • Why does this small C program crash on ESP32?
  • How do I limit the input of scanf to integers and floats(numbers in general)
  • printf is not giving output when using system
  • Running a particular c file in linux
  • C error expected specifier-qualifier-list before ‘time_t’
  • How to format integer correctly in c?
  • Dynamic relocation of code section
  • Efficiently Traverse Directory Tree with opendir(), readdir() and closedir()
  • How am I able to access a static variable from another file?
  • srandom( time( NULL ) )
  • Looping threads accessing pthread mutex
  • sYSMALLOc: Assertion failed - any ideas how to debug effectively?
  • Auto generate header files for a C source file in an IDE
  • printf and ++ operator
  • feeding a mouse driver alternate data in linux
  • Random int 1-255 to character in C
  • Problems with File I/o when porting old 'C' libraries from 32-bit to 64-bit
  • Static / global variables in shared library in C language
  • Is there a tool to list global variables used and output by a C function?
  • functions returning char pointer
  • How many elements are full in a C array
  • Resources for writing kernel in C
  • OpenGL: How do I avoid rounding errors when specifying UV co-ordinates
  • Basic skeleton for a C OpenGL program on OSX
  • Float versus integer data type calculation issues in C
  • What have you used sysv/posix message queues for?
  • Recursive harmonic function returns NaN
  • How to fix "unable to open stdio.h in Turbo C" error?
  • C programming structure passed as cast to char *?
  • Do I need to #include <omp.h> in my C/C++ sources?
  • Find original owning process of a Linux socket
  • How can I send e-mail in C?
  • How is the lagged fibonacci generator random?
  • Trouble with dangling pointers and character arrays in C
  • Trying to compile with cl.exe
  • C code compilation failure
  • Gtk Draw Bitmap
  • booting from a disk/cd/usb
  • Due to Shared memory, when MS Visual C 6.0 DLL crashes it also causes VB 6 EXE to crash
  • Error receiving in UDP: Connection refused
  • How to get the port number from addrinfo in unix c
  • Should I declare the expected size of an array passed as function argument?
  • How to use pcap_breakloop?
  • C Programming linux , read system inputs like ping or ls -l
  • Unsigned Short to Unsigned Long assignment
  • C: strtok on pointer inside a struct
  • How to put bits into a character Array
  • C moving the cursor to the beginning of the current line
  • libgtk version issue on ubuntu
  • creating a list of queues
  • Pointer to a specific fixed address
  • How to parse HTTP responses in C?
  • Use strtol or strtok to parse a string in C?
  • fread/fwrite string in C
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org