Coding a Solution to an Ugly Character Mess

  • Thread starter Ja4Coltrane
  • Start date
  • Tags
    Coding
In summary: Mem1 ) { printf("Could not allocate memory for %d", sizeof(abNext)); return 1; }if ( ! pMem2 ) { printf("Could not allocate memory for %d", sizeof(abNext)); return 2; }if ( ! pMem0 ) { printf("Could not allocate memory for %d", sizeof(abNext)); return 0; }while ( 1 ) { if ( pMem1 == 0 )
  • #1
Ja4Coltrane
225
0
Hello!

I am working on some software to control oscilloscopes and function generators in a physics lab. The situation I am faced with at the moment is to be able to translate integers into a form which can be loaded into a device.

So basically I have a file which looks like this:

(1)**NEXT**(2)**NEXT**...**NEXT**(16383)

where (n) is actually something really crazy (possibly including whitespace). (The actual file starts like "^A**NEXT**^B**NEXT**...).

I am trying to write a program in C which will let me input any int n with 1<=n and n<=16383 and which will go through the file to find (n) and output it.

So basically it is a problem of dealing with an ugly mess of characters. Do you all have any suggestions or know of any built in functions which might be useful for this?

Also, when using file input in C, I tend to run into the problem where I can only move forward in the file. Is there an easy way to move backwards as well? What about a way of searching for the next "**NEXT**"? Anyway, sorry about the long question. Any advice at all would be nice!

Thanks!
 
Technology news on Phys.org
  • #2
Hello,

If this is a binary file containing integers , you can use
http://www.cplusplus.com/reference/clibrary/cstdio/fread/" functions for file I/O

in addition to that,
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/" function allows you to move file pointer forward or backwards (as you want).
 
Last edited by a moderator:
  • #3
If this is being implemnted on PC, chances are that your file will easily fit into memory, so you might as well read the entire file in a sufficiently large allocated block of memory.

If the CPU is intel based, there's a fast search for character option, memchr().

Code:
BYTE  abNext = "**NEXT**"
PBYTE pMem;
PBYTE pCurrent;
PBYTE pSearch;
int   iCount;
int   iSkip;

    pMem = malloc(0x100000)   // allocate 16mb ram

//  read file, store # of bytes in iCount

    pCurrent = pMem;

    while(1){
        if( (iCount <= 0) ||
            ((PBYTE)0 == pSearch = (PBYTE) memchr(pCurrent, '*', iCount))){
            // ... end of file
        break;
        }
        if(0 == memcmp(pCurrent, abNext, sizeof(abNext){
            // ... match found
            iSkip += (pSearch-pCurrent) + sizeof(abNext) + ...;
            pCurrent += iSkip;
            iCount -= iSkip;
        }
    }
 
  • #4
Jeff Reid said:
pMem = malloc( 0x100000) // allocate 16mb ram

Allocated size was wrong, that was 1mb

Code:
    pMem0 = malloc(  0x100000)   // allocate   1mb ram
    pMem1 = malloc( 0x1000000)   // allocate  16mb ram
    pMem2 = malloc(0x10000000)   // allocate 256mb ram
 

Related to Coding a Solution to an Ugly Character Mess

1. What is "Coding a Solution to an Ugly Character Mess"?

"Coding a Solution to an Ugly Character Mess" refers to the process of writing and implementing a computer program or script to clean up and fix any issues caused by incorrect or malformed characters in a piece of text or code.

2. Why is it important to fix an "Ugly Character Mess"?

An "Ugly Character Mess" can cause various problems and errors in a program, such as incorrect output, crashes, or even security vulnerabilities. Therefore, it is important to fix these issues to ensure the proper functioning and safety of the code.

3. What are some common causes of an "Ugly Character Mess"?

Some common causes of an "Ugly Character Mess" include using the wrong character encoding, copying and pasting text from different sources with different encodings, and using special characters that are not recognized by the program or system.

4. How do you approach coding a solution to an "Ugly Character Mess"?

The approach to coding a solution to an "Ugly Character Mess" may vary depending on the specific issue and programming language being used. However, some common steps include identifying the cause of the problem, researching and understanding the proper character encoding for the text, and implementing a solution that can handle and convert the characters correctly.

5. Are there any tools or resources that can assist with coding a solution to an "Ugly Character Mess"?

Yes, there are various tools and resources available that can assist with coding a solution to an "Ugly Character Mess". These include online character encoding converters, libraries or functions specific to handling special characters in programming languages, and forums or communities where other developers may have shared their solutions to similar issues.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
359
  • Programming and Computer Science
Replies
2
Views
423
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
757
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top