fgets()
Returns a string from a file.
Synopsis
int fgets(string &s, int count, file f);
Parameters
Parameter | Meaning |
---|---|
s | string |
count | Maximum length of the string |
f | File from which is read |
Return value
The function returns the number of characters read or 0 in the event of errors.
Error
Description
Reads a number of maximum count characters to the string s from the file f up to a line or file end. The line feed character ("Newline") is also imported to the string. The function fgets()differs from the function of the ANSI C standard with the same name in that it reads out count-1 characters and returns the read string.
Example
main()
{
string fname="C:/TEMP/TEST.TXT",st;
file f;
int i;
i=access(fname,F_OK); // Does the file already exist ?
if (i!=0) // File does not exist yet
{
f=fopen(fname,"w+"); // Create a new file
for (i=0;i<=10;i++)
fputs("Zeile "+i+"/n",f); // 10 lines with endl
rewind(f); // Back to the beginning
while (feof(f)==0) // as long as it is not at the end of the file
{
fgets(st,20,f); // Import string again
Debug(st); // ...and output it
}
fclose(f); // close file
}
else DebugN("File already exists!");
}
Assignment
File function
Availability
CTRL