read and write in C
2016-03-10 06:04
591 查看
can use chmod to change the permission of a file
chmod a+r myfile: a means add,r means read
write:
fwrite: http://www.tutorialspoint.com/c_standard_library/c_function_fwrite.htm size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.
Parameters
ptr This is the pointer to the array of elements to be written.
size This is the size in bytes of each element to be written.
nmemb This is the number of elements, each one with a size of size bytes.
stream This is the pointer to a FILE object that specifies an output stream.
Return Value
This function returns the total number of elements successfully returned as a size_t object, which is an integral data type. If this number differs from the nmemb parameter, it will show an error.
Example
The following example shows the usage of fwrite() function.
#include<stdio.h>
int main ()
{
FILE *fp;
char str[] = "This is tutorialspoint.com";
fp = fopen( "file.txt" , "w" );
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
return(0);
}
feof():
Now let's see the content of the above file using the following program
#include <stdio.h>
int main ()
{
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
fgets and fscanf
The function fgets read until a newline (and also stores it). Fscanf with the %s specifier reads until any blank space and doesn't store it.
better not use fscanf read binary file
As a side note, you're not specifying the size of the buffer in scanf and it's unsafe. Try:
fscanf(ptr, "%9s", str)
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error
write to text file
for(i=0;i<n;i++)
{
fprintf(fp1,"%d",v1[i]);
}
to see content in shell:cat filename
write to binary file
int *v;
v=malloc(n * sizeof(int));//define vector with demension of n
//cz v is int,so we should set size "int",otherwise we can't store correctly
fwrite(v,sizeof(int),n,fp);
The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.
size This is the size in bytes of each element to be written.
nmemb This is the number of elements, each one with a size of size bytes.
stream This is the pointer to a FILE object that specifies an output stream.
returned as a size_t object, which is an integral data type. If this
number differs from the nmemb parameter, it will show an error.
fread
like fwrite(they two are binary input/output)
number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short
item count (or zero).
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which
occurred.
while((fread(&c,sizeof(int),1,fp2)))//when not reach the end,it is >0
or use if(feof(fp2))
break;
Rewind
if we want to restart reading the file from the beginning:
rewind(fp);
or
fseek(fp,0,SEEK_SET);
Understand clearly about each parameters(sometimes can even cause segment
core or dump error cz of access forbidden memory).
chmod a+r myfile: a means add,r means read
write:
fwrite: http://www.tutorialspoint.com/c_standard_library/c_function_fwrite.htm size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.
Parameters
ptr This is the pointer to the array of elements to be written.
size This is the size in bytes of each element to be written.
nmemb This is the number of elements, each one with a size of size bytes.
stream This is the pointer to a FILE object that specifies an output stream.
Return Value
This function returns the total number of elements successfully returned as a size_t object, which is an integral data type. If this number differs from the nmemb parameter, it will show an error.
Example
The following example shows the usage of fwrite() function.
#include<stdio.h>
int main ()
{
FILE *fp;
char str[] = "This is tutorialspoint.com";
fp = fopen( "file.txt" , "w" );
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
return(0);
}
feof():
Now let's see the content of the above file using the following program
#include <stdio.h>
int main ()
{
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
fgets and fscanf
The function fgets read until a newline (and also stores it). Fscanf with the %s specifier reads until any blank space and doesn't store it.
better not use fscanf read binary file
As a side note, you're not specifying the size of the buffer in scanf and it's unsafe. Try:
fscanf(ptr, "%9s", str)
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error
write to text file
#include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, ...);char c=' ';//for separating numbers by blank space
for(i=0;i<n;i++)
{
fprintf(fp1,"%d",v1[i]);
}
to see content in shell:cat filename
write to binary file
int *v;
v=malloc(n * sizeof(int));//define vector with demension of n
//cz v is int,so we should set size "int",otherwise we can't store correctly
fwrite(v,sizeof(int),n,fp);
The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.
Parameters
ptr This is the pointer to the array of elements to be written.size This is the size in bytes of each element to be written.
nmemb This is the number of elements, each one with a size of size bytes.
stream This is the pointer to a FILE object that specifies an output stream.
Return Value
This function returns the total number of elements successfullyreturned as a size_t object, which is an integral data type. If this
number differs from the nmemb parameter, it will show an error.
fread
like fwrite(they two are binary input/output)
size_t fread(void *ptr, size_t size, size_t nmembFILE *" stream );On success, fread() and fwrite() return the number of items read or written. This
number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short
item count (or zero).
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which
occurred.
while((fread(&c,sizeof(int),1,fp2)))//when not reach the end,it is >0
or use if(feof(fp2))
break;
Rewind
if we want to restart reading the file from the beginning:
rewind(fp);
or
fseek(fp,0,SEEK_SET);
Understand clearly about each parameters(sometimes can even cause segment
core or dump error cz of access forbidden memory).
相关文章推荐
- Java IO与NIO的一些文件拷贝测试
- Stream、WshShell、WshUrlShortcut对象及Shell.Application的参数与使用
- jquery中change()用法实例分析
- Node.js中的流(Stream)介绍
- php中stream(流)的用法
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- Serv-U 550 Permission denied 的解决办法
- FTP 550 Permission denied 只能建文件夹,没法删除及上传文件的原因说明
- js change,propertychange,input事件小议
- javascript的onchange事件与jQuery的change()方法比较
- C# Stream 和 byte[] 之间的转换
- PHP使用Session遇到的一个Permission denied Notice解决办法
- Apache启动错误Permission denied: httpd: could not open error log file解决方法
- 浅析Node.js 中 Stream API 的使用
- Android中Permission权限机制的具体使用
- Android中的Permission权限机制介绍
- 利用stream实现一个简单的http下载器
- 浅谈PHP中Stream(流)
- php常用Stream函数集介绍
- apache You don't have permission to access /test.php on this server解决方法