sepanta
sepanta

Reputation: 27

read entire text file into one-dimensional array

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

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215387

char buf[1025];
size_t len = fread(buf, 1, sizeof buf - 1, f);
buf[len] = 0;

Upvotes: 2

JaredPar
JaredPar

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

asaelr
asaelr

Reputation: 5456

char buf[1025];
int i=read(fd,buf,1024);
if (i>=0) buf[i]=0;

Upvotes: 0

Related Questions