Coding a Solution to an Ugly Character Mess

  • Thread starter Thread starter Ja4Coltrane
  • Start date Start date
  • Tags Tags
    Coding
Click For Summary
SUMMARY

This discussion focuses on developing a C program to manage and translate integers from a complex character file format used in controlling oscilloscopes and function generators. Key functions mentioned include fread() for file input and fseek() for navigating within the file. The use of memchr() for fast character searching and malloc() for memory allocation is emphasized, with specific memory sizes discussed for optimal performance. The conversation highlights the importance of correctly allocating memory to avoid errors in file handling.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with file I/O operations in C
  • Knowledge of memory management using malloc()
  • Experience with character searching functions like memchr()
NEXT STEPS
  • Explore advanced file handling techniques in C
  • Learn about memory allocation strategies and optimization
  • Investigate the use of fseek() for efficient file navigation
  • Study character searching algorithms for performance improvements
USEFUL FOR

Software developers working with C, particularly those involved in hardware control, file processing, and memory management. This discussion is beneficial for anyone looking to optimize file input/output operations in their applications.

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
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K