Pages: [1]   Go Down
Author Topic: Simple CHAR concat?  (Read 1592 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char loggerName[10] = "AAAA_01";
char fileName[15] = (loggerName += ".CSV");
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
SD.open(fileName, FILE_WRITE);
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3698
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3698
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's telling you you can't add character arrays together like that. Here's one way to do it:
Code:
char loggerName[10] = "AMAS_01";
char fileName[15];

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

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

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks - still getting a compile error?

This message:

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

This line highlighted:

Code:
strcpy(filename, loggerName);
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

strcpy(fileName, loggerName);
strcat(fileName,".CSV");
Logged

~Tom~

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-)
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

SATX
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: