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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top