Go Down

Topic: Simple CHAR concat? (Read 1 time) previous topic - next topic

point5

Hi, I have been fumbling around with this code without any joy.... anyone able to put me right?

I have a char "loggerName" and I want to add ".CSV" to make a new char "fileName.CSV"  (needs to stay as a CHAR).

I have tried lots of options but keen to keep it simple but just cannot figure out why this will not compile?

Code: [Select]
char loggerName[10] = "AAAA_01";
char fileName[15] = (loggerName += ".CSV");

Arrch

Use strcat()

Quote
I have tried lots of options but keen to keep it simple but just cannot figure out why this will not compile?


Because there is no += operater for char arrays.

point5

Thanks Arrch, I have tried using that but it produces a string which is not accepted by the next bit in the code:

Code: [Select]
SD.open(fileName, FILE_WRITE);

wildbill

What does a serial.print tell you is in your filename variable? Also, post your code.

point5

Not compiling is the issue I'm having.... below are the relevant code snippets and compile error:

Code:
Code: [Select]

char loggerName[10] = "AMAS_01";
char fileName[15] = (loggerName += ".CSV");

File dataFile = SD.open(fileName, FILE_WRITE);


Code: [Select]

invalid operands of types 'char[10]' and 'const char [5] to binary 'operator+'

wildbill

It's telling you you can't add character arrays together like that. Here's one way to do it:
Code: [Select]

char loggerName[10] = "AMAS_01";
char fileName[15];

strcpy(filename, loggerName);
strcat(filename,".CSV");

File dataFile = SD.open(fileName, FILE_WRITE);

point5

Thanks - still getting a compile error?

This message:

Code: [Select]
expected cpnstructor, destructor, or type conversion before '(' token

This line highlighted:

Code: [Select]
strcpy(filename, loggerName);

Tom Carpenter

there was a typo in that code. The variable has a capital N

Code: [Select]
char loggerName[10] = "AMAS_01";
char fileName[15];

strcpy(fileName, loggerName);
strcat(fileName,".CSV");
~Tom~

point5

ooops... should have spotted that one :-)

Unfortunatley still getting the same compile problem (Arduino 0.22) for both the strcpy & strcat lines?

point5

Ahhh.... got it.

I had the strcpy & strcat in the bit before void setup () (whatever that section is called?) - moved it to setup and it all works nicely.... thanks all for your help, appreciated :-)

Arrch


Ahhh.... got it.

I had the strcpy & strcat in the bit before void setup () (whatever that section is called?) - moved it to setup and it all works nicely.... thanks all for your help, appreciated :-)


A good example of why it's best to post ALL your code.

alinosa

Just wanted to send a big thank you out to wildbill and Tom Carpenter et al for their help here. It FINALLY allowed me to manipulate my char arrays (4 of different sizes into one long string for my loop to run through) so I could display them on a serLCD from sparkfun, 16 chars at a time. Basically, simulating a scrolling effect on just a single line.

Thanks again, y'all rock.

Go Up