Coding a Solution to an Ugly Character Mess

  • Thread starter Thread starter Ja4Coltrane
  • Start date Start date
  • Tags Tags
    Coding
AI Thread Summary
The discussion centers on developing software to control oscilloscopes and function generators, specifically addressing the challenge of translating integers into a complex file format. The user seeks advice on handling a file that contains a mix of characters, including whitespace, and requires a program in C to locate and output specific integers. Suggestions include using C standard library functions like fread for file input and fseek for navigating the file both forwards and backwards. Additionally, the use of memchr for fast character searching is recommended, along with memory allocation strategies to handle potentially large files. Overall, the focus is on efficient file handling and character searching in C programming.
Ja4Coltrane
Messages
224
Reaction score
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
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:
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;
        }
    }
 
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top