Reputation: 19
I have this
fprintf(emailFile, '%s' , fname);
fprintf(emailFile, '%s' , lname);
fprintf(emailFile, '%i' , id);
fprintf(emailFile, '%s\n' , dept);
I need to make it so that the email file shows up [email protected] but I don't know what to do for the concatenation.
Upvotes: 0
Views: 113
Reputation: 46435
As Ben Voigt indicated in his comment, your easiest bet to concatenate parts of the email address with the right separator would be to use the fact that fprintf
and its cousin sprintf
take arguments that control both the formatting and the actual contents of the string.
You can for example create a single string with the complete email address as follows:
completeAddress = sprintf('%s.%s.%i@%s.edu', fname, lname, id, dept);
As you can see, some of the characters control "insert next string argument here", while other characters in the formatting string just get copied to the output string. Note also that if your ID is an integer that could be up to four characters long, and you want to zero-pad for small numbers (0876
instead of 876
) you could use e.g. %04i
for the formatting.
Writing to an intermediate string (rather than directly to the file) allows you to confirm you have the string you want; you can then write it to the file with a single statement (assuming that emailFile
is a valid file ID, of course)
fprintf(emailFile, '%s\n', completeAddress);
Upvotes: 2