Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to read single line and character from a file?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 42
    Comment on it

    Read Single Line: fgets()

    To read a single line from a file fgets() function is used. For example: Suppose we have a file 'demo.txt'. The script given below results the first line of the file:

    <?php
    $filename = fopen("demo.txt", "r") or die("Unable to open file!");
    echo fgets($filename);
    fclose($filename);
    ?>
    
    

    After calling fgets() function, the file pointer moves to the next line.

    Read Single Character: fgetc()

    To read a single character from a file fgetc() function is used. For example: Suppose we have a file 'demo.txt'. The script given below reads the file 'demo.txt' character by character until end-of-file:

    <?php
    $filename = fopen("demo.txt", "r") or die("Unable to open file!");
    // Output one character until end-of-file
    while(!feof($filename)) {
      echo fgetc($filename);
    }
    fclose($filename);
    ?>
    
    

    After calling fgetc() function, the file pointer moves to the next character.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: