pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Truncate a string in C

Fri Aug 07, 2015 2:08 pm

I'm trying to adapt a program written in Borland C (the Locus data parser for the Adafruit MTK3339 GPS module) to compile with gcc and I'm down to one last problem. I need to change the .bin extension on the input file name to .loc. Scratching around in my memory for what I learned about C in the 1980's, it seemed the simplest way would be to find the last "." in the input filename string, truncate it at the next character by writing '\0' to it, then concatenating it with "loc" thus:

Code: Select all

index = strrchr(InputDataFileName, '.');
_sInputDATAName = InputDataFileName;
strncpy(_sOutputLogName, InputDataFileName, 120);
_sOutputLogName[offset] = "\0";
strncat(_sOutputLogName, "loc", 120);
This gives me an error on the penultimate line as it doesn't seem to like me subscripting a char* unless I set the permissive flag.

I tried replacing that line with

Code: Select all

index = index - InputDataFileName + _sOutputLogName; 
&index = "\0";
with no improvement. Perhaps I should throw away my K&R...

Can someone from the 21st Century help me please?

Regards - Philip

User avatar
joan
Posts: 14885
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Truncate a string in C

Fri Aug 07, 2015 2:13 pm

I haven't looked carefully but I expect you want to set characters, e.g. '\0' rather than strings "\0".

I.e. try

_sOutputLogName[offset] = '\0';

or

_sOutputLogName[offset] = 0;

rather than

_sOutputLogName[offset] = "\0";

User avatar
PeterO
Posts: 5828
Joined: Sun Jul 22, 2012 4:14 pm

Re: Truncate a string in C

Fri Aug 07, 2015 2:27 pm

pleriche wrote:Perhaps I should throw away my K&R...
Yes, because you don't seem to have ever read it !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Re: Truncate a string in C

Fri Aug 07, 2015 5:10 pm

Err, yes (red face) - stoopid typo (kicks self in the butt). Brain still in Python mode.

Regards - Philip
PS Peter - I read it 30 years ago. Can you remember all the books you read 30 years ago?

User avatar
PeterO
Posts: 5828
Joined: Sun Jul 22, 2012 4:14 pm

Re: Truncate a string in C

Fri Aug 07, 2015 8:04 pm

pleriche wrote: PS Peter - I read it 30 years ago. Can you remember all the books you read 30 years ago?
No, but why is that important ? The point is that you could have read your copy of K&R today and found that despite the first edition being 35 years old it still held the answer to your problem ! Your flipant "Throw it away" remark suggests you don't appreciate what an important book it was then and still is today.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

MWhite
Posts: 3
Joined: Mon Dec 08, 2014 9:22 pm

Re: Truncate a string in C

Sat Aug 08, 2015 6:28 pm

It appears to me you are putting the position of the '.' in the variable index

Then when you go to blank out that '.', you are using the variable offset.

Is there some reason you expect both of those variables to have the same value?

pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Re: Truncate a string in C

Sat Aug 08, 2015 6:37 pm

Yeah, incomplete code fragment. It now compiles cleanly and all works apart from a few refinements.

Regards - Philip

User avatar
stephj
Posts: 80
Joined: Thu Jun 21, 2012 1:20 pm
Location: Lancashire, UK

Re: Truncate a string in C

Thu Aug 13, 2015 10:20 am

You can use a combination of strstr() and sprintf() and do it in one go.

Code: Select all

#include <stdio.h>
#include <string.h>

char  inputfile[256] = "/mydir/myfilename.bin";
char outputfile[256];

char *p;

int main()
{
	strcpy(outputfile,inputfile);
	
	if((p=strstr(outputfile,".bin"))!=NULL)
		sprintf(p,".loc");
	
	printf("Input =  %s\n",inputfile);
	printf("Output = %s\n",outputfile);
	return 0;
}

Code: Select all

C:\Temp>test
Input =  /mydir/myfilename.bin
Output = /mydir/myfilename.loc
As strstr() will return NULL if ".bin" is not present in outputfile, the if statement prevents a write to a NULL pointer, (not usually a good idea!).

Return to “C/C++”