Thread: Help with manipulating data read from memory

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    41

    Help with manipulating data read from memory

    Hello,
    It's been quite awhile since I have been away from programming and need some help please.

    I need to add functionality to existing code to read from memory (I am assuming there will be a pre-existing function to do the read) to read data in the following format:

    001:015;0030:0045;0060:0070#

    these numbers represent ranges, for example 1-15, 30-45, and 60-70.

    I am supposed to read in the data and stop when I get to the #
    and then check to see if my value falls in any of those ranges.

    Like I said, it's been quite awhile since I did coding and would appreciate any help to get started in implementing this.

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Generally, we expect you to put forth some effort and show any code you've tried so far. If you're having trouble even getting started with a hello world type program, check out some basic tutorials.

    Pretend you're the computer. Describe in English what it is you would do to solve this as the computer. Something like:
    Grab all the text up to the # character
    Split it up at each ;
    ...

    Then, convert each of those steps to code, one at a time, and test your code at each step. Make sure it works perfectly before moving on to the next step.

    Some functions you might find helpful:
    strtok/strtok_r would be good for splitting up a line at certain characters (like ; ) into chunks that have the same format (i.e. pairs or ranges of numbers).
    strchr to find a character within a given string.
    strtol to convert a string of digits to an integer
    sscanf to parse a string of a given format and extract formatted data, like numbers.

    Give it your best shot, and post your attempt here, in [code][/code] tags, and please make sure it's well-formatted since it makes it easier for us to help you .

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    Generally, we expect you to put forth some effort and show any code you've tried so far. If you're having trouble even getting started with a hello world type program, check out some basic tutorials.

    Pretend you're the computer. Describe in English what it is you would do to solve this as the computer. Something like:
    Grab all the text up to the # character
    Split it up at each ;
    ...

    Then, convert each of those steps to code, one at a time, and test your code at each step. Make sure it works perfectly before moving on to the next step.

    Some functions you might find helpful:
    strtok/strtok_r would be good for splitting up a line at certain characters (like ; ) into chunks that have the same format (i.e. pairs or ranges of numbers).
    strchr to find a character within a given string.
    strtol to convert a string of digits to an integer
    sscanf to parse a string of a given format and extract formatted data, like numbers.

    Give it your best shot, and post your attempt here, in [code][/code] tags, and please make sure it's well-formatted since it makes it easier for us to help you .
    Thank you for the feedback. I will put some code together today and request your review when I am done. thanks again.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    OK, so since I last asked about the memory reading/parsing; the design changed a bit for what I am supposed to be implementing, and I have some new questions I need some help please.

    So I am reading data in one word at a time and the format of the data being read is now going to be like this:
    0001::::0015;;;;0030::::0045;;;;0060::::0070####
    so that each bit of information is read one word at a time, and I am storing each "value" ( 0001 or :::: for example) as uint32 word.

    We are also going to trust the format of the data is going to be correct such that it will be:

    low_range, delimeter, high_range, delimeter, low_range, delimeter, high_range, delimeter,low_range, delimeter, high_range, end_delimeter,

    although the length of the list could be variable but no less than 3 ranges and no more than 20 words total.

    My questions:
    1. These values are obviously in ASCII format, so if I then want to store them as integer , what function can I use? Is it strtol?

    2. I am having a hard time wrapping my mind around how to format my loop to stop evalutaing when I get to #### , so any ideas here?

    3. How would I break out of the loop when a match has been found?
    For example:

    Code:
    struct limits {uint32_type low, 
                       uint32_type high};
    my_function()
    {
    uint32_type data_read[20];
    uint32_type words_to_read = 20;
    my_value = 50;
    int i = 0; 
    int j = 0; 
    int k = 0;
    struct limits ranges[3];
    memory_read_function ( start_address,
                                        words_to_read,
                                        data_read);  // output from function
    while (data_read[i] != "####") // 1 - Is this loop ok?
     {
      data_read[j].low = data_read[i];   // 2- DO I NEED TO DO A CONVERSION HERE if I want data_read[0] to have a integer value stored in it?
      data.read[j].high = data_read[i+2];
      j++;
      i= i+4;
     }
    }
    for ( k=0; K<=j; k++)
    {
      if ((my_value >= data_read[k].low) && (my_value <= data_read[k].high))
      {
         match_found = TRUE; // 3. How would I break out of the loop since a match has been found?
      }
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Please don't send PMs asking for replies. Most of us have jobs, school, families and a life in general to attend to. We volunteer our time here, and do our best to help when and where we can. You waited 5 or 6 days to respond to my initial post, and you didn't even wait 2 hours before sending a second PM asking me to review your code and answer your questions. I'll get to this when I have a chance. In the mean time, somebody else may come along and offer help.

    Also, you seem to have some serious knowledge gaps, which is okay and normal for a beginner or somebody who has been away for very long. While you wait, you should be studying (text books, class notes, online tutorials) and practicing basic coding examples. We have some tutorials here for starters.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    Please don't send PMs asking for replies. Most of us have jobs, school, families and a life in general to attend to. We volunteer our time here, and do our best to help when and where we can. You waited 5 or 6 days to respond to my initial post, and you didn't even wait 2 hours before sending a second PM asking me to review your code and answer your questions. I'll get to this when I have a chance. In the mean time, somebody else may come along and offer help.

    Also, you seem to have some serious knowledge gaps, which is okay and normal for a beginner or somebody who has been away for very long. While you wait, you should be studying (text books, class notes, online tutorials) and practicing basic coding examples. We have some tutorials here for starters.
    My apologies for the duplicate PM, I thought the first one hadn't gone through because I didn't see it in my sent box . Also, the delay in my response was because I was waiting on a decision on the redesign . I PMed you because you were kind enough to respond to my original post and wanted to let you know that I now had something for you to review .

    You are right , as I mentioned before , I have been away from coding for a long time and from time get assigned such tasks . I do appreciate you taking the time to respond to me and would appreciate any feedback that you could give me on my code .
    Thanks in advance .

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Can you show the prototype of
    Code:
    memory_read_function ( start_address,words_to_read,data_read);
    or implementation of the function to know the type of data_read as you have declared it uint32 but you are doing illegal comparison
    Code:
    data_read[i] != "####"
    Is it possible to post the complete code?

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    To answer question 3:

    You just make the loop condition false. A loop will only continue to iterate
    as long as the continuation condition is true. In the case of your segment,
    try placing an independent test that breaks out of the loop when the
    pre-determine condition is met.
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    Quote Originally Posted by Satya View Post
    Can you show the prototype of
    Code:
    memory_read_function ( start_address,words_to_read,data_read);
    or implementation of the function to know the type of data_read as you have declared it uint32 but you are doing illegal comparison
    Code:
    data_read[i] != "####"
    Is it possible to post the complete code?
    the prototype for the function is as follows:

    memory_read_function ( uint32_type start_address,
    uint32_type
    words_to_read,
    uint32_type const * data_read);

    Can you please tell me how I can/should store the memory read to be able to access it as a string to read for the delimeters and also to be able to compare the values as a integer?
    Thanks in advance.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Can we get a full problem statement? Is there a requirements doc or a homework assignment sheet or something? Why do you now use :::: ;;;; and #### ? Also, such a document may give important information, like where the data is coming from, and more details about the format, so we can suggest the best options.

    The data you are reading is text/string data, so store it as a string (a char array), not as some type of integer. :::: is not an integer, neither is ;;;; or ####. You have a few common methods for reading strings, such as fgets, scanf/fscanf or getc/fgetc. This should be covered by any basic tutorial that covers strings and input/output (if getting data from the user on the command line) or file handling (if getting data from a file).

    "####" is a string. You can't compare strings with == or !=, you must use the strcmp function. However, I don't think you need to do this in your program, especially if you can change your format back to the old way.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    Can we get a full problem statement? Is there a requirements doc or a homework assignment sheet or something? Why do you now use :::: ;;;; and #### ? Also, such a document may give important information, like where the data is coming from, and more details about the format, so we can suggest the best options.

    The data you are reading is text/string data, so store it as a string (a char array), not as some type of integer. :::: is not an integer, neither is ;;;; or ####. You have a few common methods for reading strings, such as fgets, scanf/fscanf or getc/fgetc. This should be covered by any basic tutorial that covers strings and input/output (if getting data from the user on the command line) or file handling (if getting data from a file).

    "####" is a string. You can't compare strings with == or !=, you must use the strcmp function. However, I don't think you need to do this in your program, especially if you can change your format back to the old way.

    There is no formal requirements doc/ assignment sheet.

    The basic summary is that I will be reading a int value from memory (using a pre-exisiting function where I pass in the memory address of where to read from and a pointer to an int to where to store my value.

    Then I will be using another pre-existing function to read from memory "the exceptions" list, the prototype for that function is as I posted:
    memory_read_function ( uint32_type start_address,
    uint32_type
    words_to_read,
    uint32_type const * data_read);

    The data is read back 1 word at a time, but I am passing an array of words to read 20 words ( my data_read[20] variable).

    Since the function reads one word at a time, they changed the format to each piece of info whether being a range value or a delimiter to be 1 word long.

    The data will be stored as ASCII in the following format:
    0001::::0015;;;;0030::::0045;;;;0060::::0070####

    I then have to see if my original value from the first read function is any of the ranges from the read section function. This is only a portion of the whole project. I feel confident that I did rest of the project ok, but it's been about a decade since I did much coding and as you have pointed out, I am really weak at I/O data reading/manipulation. So I would appreciate any help I could get in regards to how to store the data ( the list) read in per the prototype above and then how to deal with it when I have to determine/store it as a number and also when dealing with it as a delimeter.

    Thank you for taking the time to help me.
    Last edited by knik653; 08-04-2016 at 12:15 PM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    Can we get a full problem statement? Is there a requirements doc or a homework assignment sheet or something? Why do you now use :::: ;;;; and #### ? Also, such a document may give important information, like where the data is coming from, and more details about the format, so we can suggest the best options.

    The data you are reading is text/string data, so store it as a string (a char array), not as some type of integer. :::: is not an integer, neither is ;;;; or ####. You have a few common methods for reading strings, such as fgets, scanf/fscanf or getc/fgetc. This should be covered by any basic tutorial that covers strings and input/output (if getting data from the user on the command line) or file handling (if getting data from a file).

    "####" is a string. You can't compare strings with == or !=, you must use the strcmp function. However, I don't think you need to do this in your program, especially if you can change your format back to the old way.
    I wasn't able to further edit my last post, so I am posting another one here with 2 additional questions ( as another design change was just mentioned to me) and the list will be in ASCII format. If I can have the anwer to these questions, I think I should be set.

    The list will now look like this with a TYPE word added at the front and remove the end delimter ####:

    TYPE0001::::0015;;;;0030::::0045;;;;0060::::0070

    1. So again, if TYPE is a string and I am reading it into a uint32 word, what function do I call to convert it into a string format or do I even need to convert it if it's in ASCII format and/or how I do compare it against a string?
    ie

    Code:
    // set data[0] equal to the first word in the buffer
    if strmp (data[0], "TYP1")  // DOES THIS WORK or do I need to do some conversion on data[0] first?
    {
    ..
    }
    2. Again if the data read in is in ASCII formatt , if I need to compare the data to a integer, do I need to do any conversion on the data first?

    For example checking to see if my_value is less than a value read in from the buffer
    Code:
    my_value = 50;
    
    if ( my_value <= data[1]) // if the value in data[1] is in ASCII , do I need to convert to get it's literal value ( like 0040 would be 40), to do my comparison? and if so how do I do the conversion?
    {
    ...
    }

    Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating data in memory
    By congi in forum C Programming
    Replies: 9
    Last Post: 02-11-2015, 03:20 PM
  2. Manipulating data in a FILE
    By Joemar P. Gava in forum C Programming
    Replies: 5
    Last Post: 09-28-2014, 02:44 AM
  3. Manipulating a read-in file in C
    By JoshD75 in forum C Programming
    Replies: 55
    Last Post: 05-02-2011, 09:12 AM
  4. Manipulating data in an array...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 03:23 PM
  5. allocating memory screws up data being read in from file
    By Shadow12345 in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2002, 03:23 PM

Tags for this Thread