I'm working with code that grabs strings from a file and then works with said strings in different ways.
I would like to check if the first character in a char* is a #:
Code:
FILE* file = file_open(filename);
char* fileline = malloc(160);
file_getline(fileline,file);
printf("Test: %s\n", &fileline[0]);
Where the two methods are:
Code:
/***************
* These functions are just some handy file functions.
* We have not yet covered opening and reading from files in C,
* so we're saving you the pain of dealing with it, for now.
*******/
FILE* file_open(char* filename) {
FILE* fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "Error opening file %s, abort.\n", filename);
exit(1);
}
return fp;
}
/*******************
* To use this function properly, create a char* and malloc
* 160 bytes for it. Then pass that char* in as the argument
******************/
char* file_getline(char* buffer, FILE* fp) {
buffer = fgets(buffer, 160, fp);
return buffer;
}
But when I reference the text with &fileline[0], it prints out the entire string.
Moreover, why is this string suddenly a char** when I attempt to print it? If i just do printf("Test: %s\n", filename) the code works, but if i try printf("Test: %s\n", filename[0]) it yells at me.
What am I missing here?