Code:
int main()
{
char filename[20];
// Allocate a string to store filename - does PC interpret whitespace at end of filename?
printf("Please enter the filename");
scanf("%s", &filename);
// Request name of file to open, store in filename
FILE *fp;
fp = fopen(filename,"r");
// Create file pointer, open file stored in string filename
char filetext[1024];
// Create string 1024 cells long to hold text from file
int i = 0;
// Compiler doesn't like my fore loops, explicitly create i
for(i = 0; i <= 1023; i++) {
filetext[i] = fgetc(filename);
// Place each character of the file into a different string cell, one after the other
tolower(filetext[i]);
// Move through string, converting text to lowercase
}
printf("%s",filetext);
// Display the contents of the file on screen, but...
if(i == ' ') {
// If the string cell is blank, i.e a space
printf("\n");
// move onto a new line
}
}
Now, let's see what you are doing here:
Code:
scanf("%s", &filename);
You are passing scanf the address to the pointer of the filename buffer.
Code:
fp = fopen(filename,"r");
Doh!
Code:
for(i = 0; i <= 1023; i++) {
filetext[1023] is the last byte of the filetext buffer, so if you don't set the last byte to 0, then you will see a buffer overrun(overread?).
Code:
tolower(filetext[i]);
tolower() returns lowercase filetext[i], but... the return value is not used.
If i is 32, it enters that if. (It is always 1024 after the loop)
Your indentation is buggy (last 2 lines).