您的位置:首页 > 其它

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

#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 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.
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).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  change permission stream