Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
LINUX系統程式設計 第二版
by
Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from
LINUX系統程式設計 第二版
80
|
第三章
讀取自一個串流
知道如何開啟及關閉串流後,接著讓我們來做一些有用的事情:首先是從串流讀取資
料,然後是將資料寫到串流。
標準
C
程式庫實作了數個函式(有些常用到,有些不常用到)可用於讀取一個已開啟的
串流。本節將介紹三種最常見的讀取操作:一次讀取一個字符、一次讀取一整列,以及
讀取二元資料。欲讀取自一個串流,必須以適當的模式開啟一個輸入串流;也就是,
w
或
a
除外之任何有效的模式。
一次讀取一個字符
通常,理想的
I/O
操作方式,就是一次讀取一個字符。函式
fgetc()
可用於從一個串流
讀取一個字符:
#include <stdio.h>
int fgetc (FILE *stream);
此函式會從
stream
讀取下一個字符,把它從
unsigned char
轉型成
int
並予以傳回。轉
型的目的,在於提供足夠的空間給
end-of-file
或錯誤通知:此情況下,傳回值為
EOF
。
fgetc()
的傳回值必須被存放在
int
變數中。把它存放在
char
變數中,是一種常見但危
險的錯誤,因為你將會失去偵測錯誤的能力。
下面的範例程式碼會從
stream
讀取一個字符,檢查錯誤,然後以
char
資料型別印出
結果:
int c;
c = fgetc (stream);
if (c == EOF)
/*
錯誤
*/
else
printf ("c=%c\n", (char) c);
由
stream
所指向的串流必須被開啟以備讀取。
把字符還回去
標準
I/O
提供了一個函式,可用於把一個字符推回串流,這讓你得以“偷看一下”串 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial
You might also like
優雅的SciPy|Python科學研究的美學
Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
成為卓越程式設計師的38項必修法則
Pete Goodliffe
深入理解運算原理|從簡單的機器到無所不能的程式
Tom Stuart
C++语言导学(原书第2版)
本贾尼 斯特劳斯特鲁普
Publisher Resources
ISBN: 9789862769812