Read and write in C

Source: Internet
Author: User

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 T He given stream.
Parameters

PTR the pointer to the array of elements to be written.

The size this is the size in bytes of each element to be written.

Nmemb This is the number of elements and each one with a size of size bytes.

Stream this is the pointer to a FILE object, specifies an output stream.

Return Value

This function returns the total number of elements successfully returned as a size_t object, which are an integral data typ E. If this number is differs from the NMEMB parameter, it'll show an error.
Example

The following example shows the usage of the 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 is 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 was set, EOF shall be returned, and errno shall are set to Indica Te 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 T He array pointed to, from ptr to the given stream.

Parameters
    • ptr 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, and each one with a size of size bytes.

    • Stream The pointer to a FILE object, specifies an output stream.

Return Value

This function returns the total number of elements successfully returned as a size_t object, which are an integral data typ E. If this number is differs from the NMEMB parameter, it'll show an error.

Fread

Like fwrite (they, 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. Thisnumber equals the number of bytes transferred only if size is 1. If An error occurs, or the end of the file is reached, the return value is a shortitem count (or zero).

fread () does not distinguish between End-of-file and error, and callers must use feof(3) and ferror (3) to determine whichoccurred.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.