read and write in C
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了read and write in C相关的知识,希望对你有一定的参考价值。
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).
以上是关于read and write in C的主要内容,如果未能解决你的问题,请参考以下文章
数据分析文摘:Reading and Writing JSON to a File in Python