C language reading and writing exercises under Linux (read keyboard input)

Source: Internet
Author: User

C Language Exercises:

1. Read 10 characters from the keyboard and display the 10 characters (need to use the read and write functions)

2. Write 5 characters into a text file

Question 1. C language once involved in the file operation problem, in fact, the biggest problem is the problem of pointers. Due to the fact that the pointer is still at the end of the file after writing, you need to manually return the pointer to the

#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include < string.h> #include <time.h> #include <sys/types.h>int main () {int Fd,fd1,fd2,n,m,i,len;char C;char buff[ 10];char Buf[10];fd=open ("./test1.txt", o_rdwr| o_creat,755); Fd1=open ("./test2.txt", o_rdwr| o_trunc| o_creat,755); N=write (fd, "Hello", 5);p rintf ("Please input string:") scanf ("%s", buff); Len=strlen (buff); if (len>10 {perror ("The string is error restart");} M=write (Fd1,buff,len);p rintf ("%d\n", M), M=lseek (fd1,-m,seek_cur);p rintf ("%d\n", m); Read (fd1,buf,10);p rintf ("We Get output string:%s ", buf); close (FD); close (FD1); return 0;}

When manipulating files read and write, be sure to pay attention to where the pointer is pointing, otherwise you will not be able to read the data correctly

C language reading and writing exercises under Linux (read keyboard input)

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.