Xcode read-only file mode use custom working directory

product->scheme->edit scheme

run->options->use custom working directory: small folder selection 

string filename;
cin>>filename;//文件名
FILE *fpin;
if((fpin=fopen(filename,"r"))!=NULL)//只读
{
    break;
}
else
    cout<<"文件输入错误!请输入源文件名:";

FILE *fpin;

fpin=fopen("xx.txt","r");//Read only

Define the function 
  FILE * fopen(const char * path, const char * mode); 
  function description The 
  parameter path string contains the file path and file name to be opened, and the parameter mode string represents the stream form. 
  Mode has the following types of strings: 
  r Open a read-only file, the file must exist. 
  r+ Open a readable and writable file, the file must exist. 
  w Open the write-only file. If the file exists, the file length is cleared to 0, that is, the content of the file will disappear. If the file does not exist, create the file. 
  w+ Open a readable and writable file. If the file exists, the file length is cleared to zero, that is, the content of the file will disappear. If the file does not exist, create the file. 
  a Open the write-only file in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. 
  a+ Open readable and writable files in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. 
  All of the above-mentioned morphological strings can be added with a b character, such as rb, w+b or ab+. The b character is added to tell the library to open the file as a binary file instead of a plain text file. However, in POSIX systems, including Linux will ignore this character. The new file created by fopen() will have S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666) permissions, and the file permissions will also refer to the umask value. 
  Return value After the 
  file is successfully opened, the file pointer to the stream will be returned. If the file fails to open, NULL is returned and the error code is stored in errno. 

Reference: https://blog.csdn.net/cainiao000001/article/details/80473979?biz_id=102&utm_term=fopen&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-1-80473979&spm=1018.2118. 3001.4450

Or put the txt file in the debug folder and use the freopen() function to open

Reference: https://blog.csdn.net/qq_41730082/article/details/104570756?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160447268219724835820934%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog .%2522%257D&request_id=160447268219724835820934&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v1~rank_blog_v1-13-104570756.pc_v1_rank_blog_v1&utm_term=4501018.2 file input&s300pm=4501018.2

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/109496508