Asp.net C library file operations related functions

Source: Internet
Author: User

file *fopen(const char *filename, const char *mode)
Filename open file name mode open mode return file pointer

 

size_t fread(void *ptr, size_t size, size_t, file *stream)
Reads n fields from the file pointed to by stream. Each field is size byte, And the read data is placed in the character array indicated by ptr. The actual number of bytes read is returned.

 

size_t fwrite(const void *ptr, size_t size, size_t n, file *stream)
Write n fields from the array referred to by the buffer ptr to the file pointed to by stream. Each field is in size and returns the number of actually written fields.
int fgetc(file *stream)
Reads a character from a specified file.
Example 1:

 

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
file *fp;
char ch;
if((fp = fopen("test.txt","rt")) == null)
{
printf("file open error!");
exit(1);
}
ch = fgetc(fp);
while(ch != eof)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
}

 

int fputc(int c, file *stream)
Write a character to the specified file.
Example 2:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
file *fp;
char ch;
if((fp = fopen("test.txt","wt+")) == null)
{
printf("file open error!");
exit(1);
}
printf("please enter a stringn");
ch = getchar();
while(ch != 'n')
{
fputc(ch, fp);
ch = getchar();
}
fclose(fp);
}


fscanf(file *stream, char *format [,argument...])
Format input from a stream
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Int I;
Printf ("please enter an interger :");
If (fscanf (stdin, "% d", & I) // read from the standard input stream
Printf ("the interger is: % dn", I );
Return 0;
}

int fprintf(file *stream, char *format[,argument...])
Format the output to a stream
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
file *stream;
int i = 10;
double fp = 1.5;
char s[] = "hello world";
char c = 'n';
stream = fopen("fprint.out", "w");
fprintf(stream, "%s%c", s, c);
fprintf(stream, "%dn", i);
fprintf(stream, "%fn", fp);
fclose(stream);
return 0;
}

int fseek(file *stream, long offset, int whence)
Set the file pointer whence: seek_set: Search seek_cur from the beginning of the file: Search seek_end from the current position: search from the end of the file

Related Article

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.