Thread: Function to Parse a Text field into 100 Character fields

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    26

    Function to Parse a Text field into 100 Character fields

    Hi All,

    I'm a novice when it comes to C but am familiar with Oracle JD Edwards C functions.

    As the title states, I need to parse a text field that will be about 1500 characters into columns of about 120 characters.

    Are there any examples here just to get me started?

    Thank you,
    FrankCLT

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what separates one field from another?

    A comma
    A tab
    The fact that it's exactly 120 chars wide
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    I would say a blank since it could be a paragraph as below. In JD Edwards this would be stored in what we call a Media Object it would be 1 continues text field that I would need to parsed into columns, I could have put them in 50, 100, or 120 chars' columns.


    Sample:
    "Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph is defined as “a group of sentences or a single sentence that forms a unit” (Lunsford and Connors 116). Length and appearance do not determine whether a section in a paper is a paragraph. For instance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sentence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. In this handout, we will refer to this as the “controlling idea,” because it controls what happens in the rest of the paragraph."
    Last edited by FrankCLT; 02-16-2024 at 03:36 PM.

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    I don't know about the string types in this API. Assuming they are null-terminated strings of char and also assuming that you just want to divide it into chunks to save it and concatenate it again later on (where it wouldn't even matter if a blank is the separator as long as you keep the maximum length).
    Perhaps this gives you an idea of how to achieve it ...
    Code:
    #include <assert.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef void (*chunk_callback_t)(const char *);
    void iterate_chunks(chunk_callback_t chunkproc, const char *string, size_t maxlength);
    void print_chunk(const char *chunk);
    
    
    int main(void)
    {
      static const char str[] =
        "Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph is defined as \"a group of sentences or a single sentence that forms a unit\" (Lunsford and Connors 116). Length and appearance do not determine whether a section in a paper is a paragraph. For instance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sentence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. In this handout, we will refer to this as the \"controlling idea,\" because it controls what happens in the rest of the paragraph.";
    
    
      iterate_chunks(&print_chunk, str, 100);
      return 0;
    }
    
    
    void print_chunk(const char *chunk)
    {
      puts(chunk);
    }
    
    
    void iterate_chunks(chunk_callback_t chunkproc, const char *string, size_t maxlength)
    {
      assert(chunkproc);
      assert(string);
      assert(maxlength);
    
    
      const size_t totalchars = strlen(string);
      if (!totalchars)
        return;
    
    
      char *buffer = malloc(maxlength + 1);
      assert(buffer);
    
    
      buffer[maxlength] = '\0';
      const size_t nchunks = totalchars / maxlength;
      const size_t nchars = nchunks * maxlength;
      const size_t remchars = totalchars - nchars;
    
    
      if (nchunks)
        for (const char *cursor = string, *const end = cursor + nchars; cursor < end; cursor += maxlength)
          chunkproc((const char *)memcpy(buffer, cursor, maxlength));
    
    
      if (remchars)
        chunkproc((const char *)memcpy(buffer, string + nchars, remchars + 1));
    
    
      free(buffer);
    }
    Last edited by aGerman; 02-16-2024 at 04:35 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like any of these?
    Code:
    $ xsel | fold -w 50
    Paragraphs are the building blocks of papers. Many
     students define paragraphs in terms of length: a 
    paragraph is a group of at least five sentences, a
     paragraph is half a page long, etc. In reality, t
    hough, the unity and coherence of ideas among sent
    ences is what constitutes a paragraph. A paragraph
     is defined as “a group of sentences or a single
     sentence that forms a unit” (Lunsford and Conno
    rs 116). Length and appearance do not determine wh
    ether a section in a paper is a paragraph. For ins
    tance, in some styles of writing, particularly jou
    rnalistic styles, a paragraph can be just one sent
    ence long. Ultimately, a paragraph is a sentence o
    r group of sentences that support one main idea. I
    n this handout, we will refer to this as the “co
    ntrolling idea,” because it controls what happen
    s in the rest of the paragraph.
    
    $ xsel | fold -w 100
    Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a 
    paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, t
    hough, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph
     is defined as “a group of sentences or a single sentence that forms a unit” (Lunsford and Conno
    rs 116). Length and appearance do not determine whether a section in a paper is a paragraph. For ins
    tance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sent
    ence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. I
    n this handout, we will refer to this as the “controlling idea,” because it controls what happen
    s in the rest of the paragraph.
    
    $ xsel | fold -w 120
    Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group
     of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas 
    among sentences is what constitutes a paragraph. A paragraph is defined as “a group of sentences or a single sentence 
    that forms a unit” (Lunsford and Connors 116). Length and appearance do not determine whether a section in a paper is 
    a paragraph. For instance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sent
    ence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. In this handout, we w
    ill refer to this as the “controlling idea,” because it controls what happens in the rest of the paragraph.
    But there's no regard for punctuation or natural word breaks.
    It literally just chops the input at a given number of characters.

    If that's good, all you need is the size of buffer you want, and fgets/fread in a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    Also after reading twice it might be that we are trying to work around an XY problem. Terms like "text field" indicate it's about a kind of database. If so, isn't there something like "varchar" or "blob" types to put the whole string at once?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's all very vague.

    We have an expected input, but no idea about the output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in fields of text from a .txt file.
    By goldfish in forum C++ Programming
    Replies: 17
    Last Post: 12-26-2012, 12:16 PM
  2. Replies: 9
    Last Post: 12-08-2008, 10:27 AM
  3. Cleaning all Text fields
    By publikum in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2005, 12:46 AM
  4. Entering character in integer field
    By ToxicLove in forum C Programming
    Replies: 4
    Last Post: 05-23-2004, 04:51 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

Tags for this Thread