Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fast string manipulation

Status
Not open for further replies.

jnpoyser

IS-IT--Management
Sep 13, 2002
8
GB
Hi all, my C's a bit rusty so I'm after a little help.

I need to read in a text file of fixed length records and locate the various fields in the records.

For example, one file has a record size of 937 bytes, the first field is 1 byte, the second is 25, the third is 3, etc.

What is the fastest way to load the record and access the required bytes?

Thanks - James.
 
Hi,

the C language is not the perfect one for this purpose,
COBOL may be better, but, ... !

Define a structure that maps exactly the record .
Then use a "gets" or "read" calls to put data in your
strucrure ; make attention to carriagereturn-line feed
if there is on the file, or if your C implementation
automatically create them (it) reading file (wich OS? ).

The problems come now; you cannot use standard string
routines, becouse as you know, they suppose there is
an extra byte in string (null char) to terminate them.
In record you have not it and if you write on record, you
cannot put them.

I suggest to use memcpy for each field, using the exact
number of byte of it. To use standard function as printf
or other, use dummy variable, define macros as

char dummy[256];
#define STRCPY(d,s,n) memcpy(d,s,n); d[n]=0;

and use

STRCPY( dummy, record.name, 25 ) ;
printf( "%s\n", dummy ) ;

or similar...



bye


 
I haven't thought this through, let alone tested it.
HIH ;-)


FILE *instream;
char infile[40]={"yourfile.txt"};
typedef struct
{
char item1[3];
char item2[23];
char item3[223];
char item4[33];
char item5[13];
char item6[6];
} mydeftype;

mydeftype merecord;

char *xitem1;
char *xitem2;
char *xitem3;
char *xitem4;
char *xitem5;
char *xitem6;

/* Open the input file */
if ((instream = fopen(infile, "r")) != NULL)
{
while (!feof(instream))
{
fgets(merecord,sizeof(merecord),instream);

/* Following VictorV's advice */
strcpy (xitem1,merecord.item1);
strcpy (xitem2,merecord.item2);
strcpy (xitem3,merecord.item3);
strcpy (xitem4,merecord.item4);
strcpy (xitem5,merecord.item5);
strcpy (xitem6,merecord.item6);
do whatever here with now null-terminated strings.......
}
} Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi,

dickiebird

your program has the right structure.

There are some precision that I want say on it:

1) we are not sure that the fgets attaches a NL
after each record: they may be contiguos in file
(eg n*937); in such case we have to put a
fread

2) when you have correctly loaded mrecord, you have not
NULL char at the end of 1st field, so if you emit a

strcpy( mybuf, merecord.item1);

the function does not know how many bytes has to copy
from src to dst.
C compiler is not COBOL FORTRAN PASCAL:
for it "merecord.item1" is just an address and does
not know how long is the variable.

at minimum you have to write:

strncpy( mybuf, merecord.item1, sizeof(merecord.item1));
^
| look at n .

3) you cannot write:

char *item1 ;
.....
strcpy( item1, ..... )

item1 does not allocate space for data: it is just
a pointer to a character buffer,

A (char *) is not a dynamic area to store string as
a MYSTRING$ in basic o a CString in C++, or other:
item1 must point to a sufficent area before use it;


eg:

char *item1, buffer[80] ;
......
item1 = buffer ;
strcpy( item1, "ABC" ) ;

This cpoies 3 char ABC from a constant area
of the program to the area pointed by item1.
(you can printf(buffer) now);

bye.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top