Getting strange characters in my first print statement, but on none of the others. Also, code will not progress past the getName() call in line 58. Unable to figure out either of these issues.
/* PROJECT Guitar tab writer
* Author Chris Bowers
* Version 1.0 May 22, 2015
*/
#include <stdio.h>
#include <stdlib.h>
#define TRIAL_SIZE 16
#define NUM_STRINGS 5
typedefstruct {
char* text;
int length;
int size;
} Tab;
void initializeTabSize(Tab* t) {
t->text = malloc(sizeof(char) * TRIAL_SIZE);
t->size = TRIAL_SIZE;
if (t->text)
printf("String successfully allocated\n");
else
printf("That didn't work\n");
}
void tabTemplate(Tab* t) {
int i = 0;
t->text[0] = '|';
t->text[t->size] = '|';
for (i = 1; i < (t->size); i++)
t->text[i] = '-';
printf("%s\n", t->text);
}
char* getName(void) {
int i = 0;
char* fileName;
printf("Enter name of file >> ");
while (scanf("%c", &fileName[i]))
i++;
return fileName;
}
void writeTabToFile(FILE* tabFP, Tab* t, int lines) {
int i = 0;
for (i = 0; i < lines; i++)
fprintf(tabFP, "%s", t->text);
}
int main(void) {
int i = 0;
FILE* tabFP;
Tab tab1[NUM_STRINGS]; // configure for number of strings in later version
for (i = 0; i < NUM_STRINGS; i++)
initializeTabSize(&tab1[i]);
for (i = 0; i < NUM_STRINGS; i++)
tabTemplate(&tab1[i]);
tabFP = fopen(getName(), "a");
for (i = 0; i < NUM_STRINGS; i++)
writeTabToFile(tabFP, &tab1[i], NUM_STRINGS);
fclose(tabFP);
return EXIT_SUCCESS;
}
C strings (char buffers) must be null-terminated to be printed correctly. When you make your own string allocated from a char* malloc, you have to make the last index be the null character '\0'.
1 2 3 4 5 6 7 8
char* getName(void) {
int i = 0;
char* fileName;
printf("Enter name of file >> ");
while (scanf("%c", &fileName[i]))
i++;
return fileName;
}
You never initialize (allocate) fileName. This will either result in junk or a crash (undefined behavior).
Actually, I have something strange with my file name. It comes out as 3 bad characters despite receiving the name well.
I get a warning: function return address of local variable
Research makes it seem I need to allocate memory to the function, but this was not covered in the class I took. If you could point me to a resource for this concept, I would be grateful.