Coding a Solution to an Ugly Character Mess

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

Discussion Overview

The discussion revolves around coding solutions for handling file input in C, specifically for controlling oscilloscopes and function generators in a physics lab. Participants explore methods for translating integers into a specific format from a complex file structure, as well as strategies for navigating through the file, including searching for specific strings and moving the file pointer both forward and backward.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes a file format that includes integers and complex characters, seeking advice on how to parse this data in C.
  • Another participant suggests using the fread() function for file input and fseek() for moving the file pointer.
  • A different participant proposes reading the entire file into memory for easier manipulation, mentioning the use of memchr() for fast character searching on Intel-based CPUs.
  • There is a correction regarding memory allocation sizes, with one participant noting that the initially suggested allocation was incorrect and providing various sizes for memory allocation.

Areas of Agreement / Disagreement

Participants present multiple approaches and suggestions without reaching a consensus on the best method for handling the file input and parsing. There are varying opinions on memory allocation and file handling techniques.

Contextual Notes

Some participants express uncertainty about the file's structure and the implications of reading it into memory, as well as the correct handling of memory allocation sizes. The discussion includes unresolved technical details regarding the implementation.

Who May Find This Useful

Individuals interested in programming in C, particularly those working with file I/O, memory management, and data parsing in scientific 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
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K