Reputation: 27
I want to read a text file including a string of minimum length 0 and maximum length 1024.Then, I want to put it into an array to process the characters of that string. What is the most efficient way to do it?
Upvotes: 0
Views: 473
Reputation: 215387
char buf[1025];
size_t len = fread(buf, 1, sizeof buf - 1, f);
buf[len] = 0;
Upvotes: 2
Reputation: 755141
Since you know the maximum length you can just declare an array of the appropriate size and use fread
to read the string.
FILE* theFilePointer = ...;
char text[1024];
fread(text, sizeof(char), 1024, theFilePointer);
Upvotes: 2