fopen () function R r+ What difference does it finally have?

Source: Internet
Author: User
fopen () function R r+ What's the difference?
fopen (), followed by the argument,
For example: R
r+
A read-only mode, a read-write mode, exactly what is the difference Ah, why create a back this mode?
Can you pass an example to let me know?
------to solve the idea----------------------
R, can only read fread, cannot write fwrite
R+, both can, and overwrite write
------to solve the idea----------------------
$FN = ' abc.txt '; File name to be manipulated

File_put_contents ($FN, ' 12345 '); Writing test data
ReadFile ($FN); Look, the content is 12345.

R Read-only mode
$fp = fopen ($FN, ' R ');
$c = fgetc ($FP);
Echo $c;
Echo fputs ($fp, ' a '); 0 No characters are written
Fclose ($FP);
ReadFile ($FN); Look, the content is 12345 unchanged.

r+ Read/write mode
$fp = fopen ($fn, ' r+ ');
$c = fgetc ($FP);
Echo $c;
Echo fputs ($fp, ' a '); 1 Write 1 characters
Fclose ($FP);
ReadFile ($FN); Look again, the content is 1a345


------to solve the idea----------------------
R can only read the content inside, no permission to write in the file
R+ can write it inside.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.