日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件I/0

發(fā)布時(shí)間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件I/0 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? 一個(gè)新的的或則一個(gè)存在的文件的流能夠通過I/0流的fopen函數(shù)。這個(gè)fopen函數(shù)的取文件名,和打開模式作為參數(shù),和返回對于這流的指針。

? ? FILE* fopen(const char* filename,const char* opentype);

? ? 對于這個(gè)fopen函數(shù)的第二個(gè)參數(shù),這個(gè)打開類型,是一個(gè)字符串控制文件怎樣被打開。它應(yīng)該是以下的打開類型之一:

? ? ? r:作為只讀的打開一個(gè)存在的文件。

? ? ?w:打開一個(gè)只寫的文件。如果這個(gè)文件已經(jīng)存在,它被裁減為0長度。

? ? ?a:打開文件在附加模式。文件內(nèi)容是被保存,和這新的輸出附加到文件的末尾。如果文件存在,一個(gè)新文件被打開。

? ? r+:打開一個(gè)讀寫文件。

? ? w+:打開一個(gè)讀寫文件模式。如果這個(gè)文件已經(jīng)存在,它被裁減為0長度。

? ?a+:打開這個(gè)文件讀和附加。這文件初始化被設(shè)置到讀的開始和附加文件的末尾。

? ?如果文件不能用需要的模式的不能被打開,這fopen函數(shù)返回一個(gè)NULL指針。這例子下,一個(gè)流指針,一個(gè)FILE指針,為和流的通信被返回,如下:

? ?#include <stdio.h>
...
FILE* stream = fopen("/data/data/com.example.hellojni/test.txt", "w");
if (NULL == stream)
{
/* File could not be opened for writing. */
}
else
{
/* Use the stream. */
/* Close the stream. */
}

寫到流:

? ?Stream I/0提供是個(gè)函數(shù)來寫到流。這個(gè)部分將簡要的瀏覽這些函數(shù)。

? ?寫塊數(shù)據(jù)到流:

? ?這fwrite函數(shù)能夠被用來寫塊的數(shù)據(jù)到流:

? ?size_t fwrite(const void* data, size_t size, size_t count, FILE* stream);

? ?fwite函數(shù)寫移動(dòng)數(shù)量的元素到給定的流。

? ?= fwrite(data, sizeof(char), count, stream))

寫字符序列到流:

? ?沒有終止符號(hào)的序列使用fputs函數(shù)寫到一個(gè)流。

??int fputs(const char* data, FILE* stream);

??/* Writing character sequence to stream. */
if (EOF == fputs("hello\n", stream))
{
/* Error occured while writing to the stream. */
}

不能被寫到流,fputs函數(shù)將返回EOF。

寫一個(gè)單獨(dú)字符到流:

? 一個(gè)單獨(dú)的字符或字節(jié)被寫到一個(gè)流使用fputc函數(shù):

??int fputc(int c, FILE* stream);

? 如下:這個(gè)fputc函數(shù)去這單獨(dú)的字符作為一個(gè)整形數(shù)和轉(zhuǎn)化它到一個(gè)無符號(hào)的字符寫到這個(gè)給定的流,命名的Stream。

? ?char c = 'c';
/* Writing a single character to stream. */
if (c ! = fputc(c, stream))
{
/* Error occured while writing character to string.
}

如果不能被寫到流,fputs函數(shù)將返回EOF,否則將返回這字符自己。

寫指定的格式的數(shù)據(jù)到流中:

? ?這個(gè)fprintf函數(shù)能夠被用來格式化和輸出變量的參數(shù)的的數(shù)量到給定的流.

? ?int fprintf(FILE* stream,const char* format,...);

? ?它去一個(gè)指針到流,這個(gè)格式化字符串,和參數(shù)的的變量數(shù)目用指定的格式被引用。這個(gè)格式字符串有普通的字符串的和 指定格式。普通的格式字符被傳遞不能改到流。指定格式引發(fā)這個(gè)fprintf函數(shù)到個(gè)格式和寫這給定的參數(shù)到流。這最頻繁使用的自定符如下:

? ? %d,%i:整形參數(shù)。%u:無符號(hào)。%o:無符號(hào)的octal。等

??/* Writes the formatted data. */
if (0 > fprintf(stream, "The %s is %d.", "number", 2))
{
/* Error occurred while writing formatted data. */
}

I/o流提供這個(gè)fflush函數(shù)使應(yīng)用程序到自動(dòng)刷新緩存區(qū)被需要。

int fflush(FILE* stream);

如下,這個(gè)ffluse函數(shù)采用流指針和刷新輸出緩存區(qū)的。

? ?char data[] = { 'h', 'e', 'l', 'l', 'o', '\n' };
size_t count = sizeof(data) / sizeof(data[0]);
/* Write data to stream. */
fwrite(data, sizeof(char), count, stream);
/* Flush the output buffer. */
if (EOF == fflush(stream))
{
/* Error occured while flushing the buffer. */
}

從流中讀:

? ?和寫相似,I/0提供了四個(gè)函數(shù)讀從流中。

? 從流中讀塊的數(shù)據(jù)

?這fread函數(shù)能夠被用來讀塊數(shù)據(jù)從這個(gè)流。

size_t fread(void* data,size_t size,size_t count,FILE* stream);

例如:

??char buffer[5];
size_t count = 4;
/* Read 4 characters from the stream. */
if (count ! = fread(buffer, sizeof(char), count, stream))
{
/* Error occured while reading from the stream. */
}
else
{
/* Null terminate. */
buffer[4] = NULL;
/* Output buffer. */
MY_LOG_INFO("read: %s", buffer);
}

在這個(gè)例子,返回的元素?cái)?shù)量被傳遞給的值得數(shù)量相等。

讀字符序列從流中:

? 這個(gè)fgets函數(shù)被用來讀一個(gè)換行終止符號(hào)的字符字符串來自給定的流。

? char* fgets(char* buffer,int count,FILE* stream);

這個(gè)fgets函數(shù)讀最多count-1個(gè)字符和包含這個(gè)換行符在字符數(shù)組從給定的命名流中。

char buffer[1024];
/* Read newline terminated character sequence from the stream. */
if (NULL == fgets(buffer, 1024, stream))
{
/* Error occured while reading the stream. */
}
else
{
MY_LOG_INFO("read: %s", buffer);


讀單個(gè)字符:

unsigned char ch;
int result;
/* Read a single character from the stream. */
result = fgetc(stream);
if (EOF == result)
{
/* Error occured while reading from the stream. */
}
else
{
/* Get the actual character. */
ch = (unsigned char) result;
}
If end-of-file indicator for the stream is set, it returns EOF

讀指定格式的字符:

char s[5];
int i;
/* Stream has "The number is 2" */
/* Reads the formatted data. */
if (2 ! = fscanf(stream, "The %s is %d", s, &i))
{
/* Error occured while reading formatted data. */
}

檢查文件的末尾:

當(dāng)從一個(gè)流中讀時(shí),這個(gè)feof的函數(shù)被用來檢查如果文件結(jié)束指定符對于流被設(shè)定。

? int feof(FILE* stream);

char buffer[1024];
/* Until the end of the file. */
while (0 == feof(stream))
{
/* Read and output string. */
fgets(buffer, 1024, stream);
MY_LOG_INFO("read: %s", buffer);

fseek函數(shù):

fseek函數(shù)使用流指針,這個(gè)相似的offset,和這作為這引用點(diǎn):

? ?SEEK_SET:流的開始。

? ?SEEK_CUR:現(xiàn)在的位置

? SEEK_END:流的結(jié)束。

? 例子代碼如下,寫4個(gè)字符,重定位返回流的4個(gè)字節(jié)和重寫他們使用一個(gè)不同的字符。/* Write to the stream. */
fputs("abcd", stream);
/* Rewind for 4 bytes. */
fseek(stream, -4, SEEK_CUR);
/* Overwrite abcd with efgh. */
fputs("efgh", stream);

錯(cuò)誤檢測在這個(gè)例子代碼中被忽略。這個(gè)fseek函數(shù)將返回0,如果這個(gè)操作室成功了;否則一個(gè)非零的值指示了失敗。

檢測錯(cuò)誤:

? ?大部分的I/0流函數(shù)將返回EOF來指示錯(cuò)誤和報(bào)告文件末尾。這個(gè)ferror函數(shù)能夠被用來檢測如果一個(gè)錯(cuò)誤被發(fā)生了在上一個(gè)操作中。

? int ferror(FILE* stream);


/* Check for the errors. */
if (0 ! = ferror(stream))
{
/* Error occured on the previous request. */
}

關(guān)閉流:

? 流可以被關(guān)閉使用fclose函數(shù)。任何緩存的輸出被寫到流,和任何緩存的輸入被拋棄的。

int fclose(FILE* stream);


if (0 ! = fclose(stream))
{
/* Error occured while closing the stream. */
}

這個(gè)錯(cuò)誤指示這緩存的輸出不能寫到流中因?yàn)樵诖疟P上沒有足夠的空間。很好的習(xí)慣來檢測fclose函數(shù)的返回值。

總結(jié)

以上是生活随笔為你收集整理的文件I/0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。