file-pointer

Opening a file in 'a+ 'mode

烈酒焚心 提交于 2019-11-28 07:23:23
问题 If a file is opened using the following command: FILE *f1=fopen("test.dat","a+"); The man page reads: a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. So does f1 have 2 separate offset pointers, one for read & another for write? 回答1: No. There is just one pointer which initially is at the start of the file but when a

What's the difference between a file descriptor and file pointer?

霸气de小男生 提交于 2019-11-26 16:52:56
I want to know the difference between a file descriptor and file pointer. Also, in what scenario would you use one instead of the other? A file descriptor is a low-level integer "handle" used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass "naked" file descriptors to actual Unix calls, such as read() , write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the file descriptor, and adds buffering and other features to make I/O easier. You pass FILE pointers to

Program doesn't wait for user input with scanf(“%c”,&yn);

独自空忆成欢 提交于 2019-11-26 10:35:56
This is the basic code to a program I am writing to practise using files in C. I am trying to detect whether the output file already exists and if it does exist I want to ask the user if they would like to overwrite it or not. This is the reason that I have first opened the outfilename file in with fopen(outfilename,"r"); as opposed to fopen(outfilename,"w");. It detects the case of the file not existing, however, if it does exist it executes the printf("Output file already exists, overwrite (y/n):"); statement but completely ignores the scanf("%c",&yn); statement! The printf at the end of the

What's the difference between a file descriptor and file pointer?

醉酒当歌 提交于 2019-11-26 04:30:58
问题 I want to know the difference between a file descriptor and file pointer. Also, in what scenario would you use one instead of the other? 回答1: A file descriptor is a low-level integer "handle" used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass "naked" file descriptors to actual Unix calls, such as read() , write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps

Program doesn't wait for user input with scanf(“%c”,&yn);

99封情书 提交于 2019-11-26 02:24:22
问题 This is the basic code to a program I am writing to practise using files in C. I am trying to detect whether the output file already exists and if it does exist I want to ask the user if they would like to overwrite it or not. This is the reason that I have first opened the outfilename file in with fopen(outfilename,\"r\"); as opposed to fopen(outfilename,\"w\");. It detects the case of the file not existing, however, if it does exist it executes the printf(\"Output file already exists,