文件操作函數(讀寫)

文件文本排序:

?

數組冒泡:

#include<stdio.h>void swap(int *a,int *b)
{int temp = *a;*a = *b;*b = temp;
}void bubble(int *p,int n)
{int i;int j;for(i = 0; i < n; i++){for(j = 1; j < n - i; j++){if(p[j - 1] > p[j]){swap(&p[j-1],&p[j]);}}}
}int main()
{int arr[10] = {4,6,3,7,8,5,12,67,34,56};bubble(arr,10);int i;for(i = 0; i < 10 ; i++){printf("%d ",arr[i]);}printf("\n");return 0;
}

文件數據排序

#include<stdio.h>
#include<string.h>void swap(int *a,int *b)
{int temp = *a;*a = *b;*b = temp;
}void bubble(int *p,int n)
{int i;int j;for(i = 0; i < n; i++){for(j = 1; j < n - i; j++){if(p[j - 1] > p[j]){swap(&p[j-1],&p[j]);}}}
}int main()
{int array[10] = {0};int i;char buf[100];        int index = 0;FILE* p =fopen("./file.txt","r");if(NULL == p){printf("error\n");}else{while(!feof(p)){memset(buf,0,sizeof(buf));fgets(buf,sizeof(buf),p);array[index] = atoi(buf);index++;}fclose(p);}index--;bubble(array,index);p = fopen("./b.txt","w");printf("index = %d\n",index);for(i = 0; i < index ; i++){memset(buf,0,sizeof(buf));sprintf(buf,"%d\n",array[i]);fputs(buf,p);//printf("%d ",array[i]);
        }fclose(p);printf("i = %d\n",i);return 0;
}

如果文件數據過大,就不能在棧中建立一個數組,考慮使用堆

#include<stdio.h>
#include<string.h>
#include<stdlib.h>void swap(int *a,int *b)
{int temp = *a;*a = *b;*b = temp;
}void bubble(int *p,int n)
{int i;int j;for(i = 0; i < n; i++){for(j = 1; j < n - i; j++){if(p[j - 1] > p[j]){swap(&p[j-1],&p[j]);}}}
}int main()
{int i;char buf[100];        int index = 0;FILE* p =fopen("./file.txt","r");if(NULL == p){printf("error\n");}//else
        {while(!feof(p)){memset(buf,0,sizeof(buf));fgets(buf,sizeof(buf),p);index++;}fclose(p);}int *array = calloc(sizeof(int),index);index = 0;p =fopen("./file.txt","r");if(NULL == p){printf("error\n");}//else
        {while(!feof(p)){memset(buf,0,sizeof(buf));fgets(buf,sizeof(buf),p);array[index] = atoi(buf);index++;}fclose(p);}index--;bubble(array,index);p = fopen("./b.txt","w");printf("index = %d\n",index);for(i = 0; i < index ; i++){memset(buf,0,sizeof(buf));sprintf(buf,"%d\n",array[i]);fputs(buf,p);//printf("%d ",array[i]);
        }fclose(p);printf("i = %d\n",i);return 0;
}

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/11207012.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384972.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384972.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384972.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

文件操作(升級)

計算字符串“25 32 ” #include<stdio.h> #include<string.h>int calc_string(char *s) {char buf1[100] {0};char oper 0;char buf2[100] {0};int len strlen(s);int i;for(i 0; i < len; i){if( s[i] || - s[i] || * s[i] || / s[i] ){strncpy…

C語言指針轉換為intptr_t類型

C語言指針轉換為intptr_t類型 1、前言 今天在看代碼時&#xff0c;發現將之一個指針賦值給一個intptr_t類型的變量。由于之前沒有見過intptr_t這樣數據類型&#xff0c;憑感覺認為intptr_t是int類型的指針。感覺很奇怪&#xff0c;為何要將一個指針這樣做呢&#xff1f;如是果…

nginx epoll詳解

nginx epoll 事件模型 nginx做為一個異步高效的事件驅動型web服務器&#xff0c;在linux平臺中當系統支持epoll時nginx默認采用epoll來高效的處理事件。nginx中使用ngx_event_t結構來表示一個事件&#xff0c;先介紹下ngx_event_t結構體中成員的含義&#xff1a; struct ngx_ev…

Inotify機制

描述 Inotify API用于檢測文件系統變化的機制。Inotify可用于檢測單個文件&#xff0c;也可以檢測整個目錄。當檢測的對象是一個目錄的時候&#xff0c;目錄本身和目錄里的內容都會成為檢測的對象。 此種機制的出現的目的是當內核空間發生某種事件之后&#xff0c;可以立即通…

文件操作(二進制文件加密解密)

加密 #include<stdio.h> #include<string.h>void code(char *p,size_t n) {size_t i;for(i 0; i < n; i){p[i] 3;} }int main() {FILE *p1 fopen("./a.txt","r");FILE *p2 fopen("./b.txt","w");char buf[1024] {…

北京加密機現場select問題

問題描述 北京項目通過調用我們提供的庫libsigxt.a與加密機通信&#xff0c;c/s架構&#xff0c;客戶端啟用多個線程&#xff0c;每個線程流程有以下三步&#xff0c;連接加密機&#xff0c;簽名&#xff0c;關閉鏈接。在正常運行一段時間后會出現不能連接加密機服務問題。 連…

拼接字符串(帶參程序)

1.用strcat拼接函數可以實現 #include<stdio.h> #include<string.h>int main(int argc,char ** argv) {char str[100] {0};int i;for( i 1; i < argc; i){strcat(str,argv[i]);}printf("str %s\n",str);return 0; } 2.用sprintf函數也可以實現 #in…

詳細解釋signal和sigaction以及SIG_BLOCK

signal&#xff0c;此函數相對簡單一些&#xff0c;給定一個信號&#xff0c;給出信號處理函數則可&#xff0c;當然&#xff0c;函數簡單&#xff0c;其功能也相對簡單許多&#xff0c;簡單給出個函數例子如下&#xff1a; [cpp] view plain copy 1 #include <signal.h>…

處理SIGCHLD信號

在上一講中&#xff0c;我們使用fork函數得到了一個簡單的并發服務器。然而&#xff0c;這樣的程序有一個問題&#xff0c;就是當子進程終止時&#xff0c;會向父進程發送一個SIGCHLD信號&#xff0c;父進程默認忽略&#xff0c;導致子進程變成一個僵尸進程。僵尸進程一定要處理…

文件操作(stat)

/*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h>int main() {struct stat st {0}; //定義一個結構體&#xff0c;名字叫ststat("./a.txt",&st); //調用完stat函數之后&…

nginx源碼閱讀(一).綜述

前言 nginx作為一款開源的輕量級高性能web服務器,是非常值得立志從事服務端開發方向的人學習的。現今nginx的最新版本是nginx-1.13.6,代碼量也日漸龐大,但是由于其核心思想并沒改變,為了降低閱讀難度,我選擇的是nginx-1.0.15版本,并且由于時間和水平有限,重點關注的是nginx的啟…

文件操作(stat函數)

stat函數可以獲取文件信息 /*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h>int main() {struct stat st {0}; //定義一個結構體&#xff0c;名字叫ststat("./a.txt",&st); …

文件操作(結構體)

將結構體內容寫入到文件中 #include<stdio.h> #include<string.h>struct student {char name[100];int age; };int main() {struct student st {"wangqinghe",30};FILE * p fopen("./c.txt","wb");fwrite(&st,sizeof(st),1,p…

nginx源碼閱讀(二).初始化:main函數及ngx_init_cycle函數

前言 在分析源碼時,我們可以先把握主干,然后其他部分再挨個分析就行了。接下來我們先看看nginx的main函數干了些什么。 main函數 這里先介紹一些下面會遇到的變量類型: ngx_int_t: typedef intptr_t ngx_int_t; 64位機器上,intptr_t為long int, 即typedef long int intptr_…

EAGAIN、EWOULDBLOCK、EINTR與非阻塞

EWOULDBLOCK&#xff1a;用于非阻塞模式&#xff0c;不需要重新讀或者寫 EINTR&#xff1a;指操作被中斷喚醒&#xff0c;需要重新讀/寫   在Linux環境下開發經常會碰到很多錯誤(設置errno)&#xff0c;其中EAGAIN是其中比較常見的一個錯誤(比如用在非阻塞操作中)。 從字面上…

文件操作(排序)

文本文件&#xff0c;每行代表一個整數&#xff0c;范圍在0~512之間&#xff1b; 要求&#xff1a;對文件排序&#xff0c;不使用堆空間&#xff0c;只使用棧空間。 用srand()和rand()函數生成一定量的隨機數 /*** file.c ***/ #include<stdio.h> #include<string.h&g…

Linux下send錯誤代碼32

問題描述&#xff1a;今天寫程序&#xff0c;socket后send出現這個問題&#xff0c;send的返回值為-1&#xff0c;而errno為32&#xff0c;這個錯誤代碼為broken pipe&#xff0c;即管道破裂。 問題形成原因&#xff1a;后來通過排查研究&#xff0c;發現出現該種問題出現的可能…

系統級性能分析工具perf的介紹與使用

系統級性能優化通常包括兩個階段&#xff1a;性能剖析&#xff08;performance profiling&#xff09;和代碼優化。 性能剖析的目標是尋找性能瓶頸&#xff0c;查找引發性能問題的原因及熱點代碼。 代碼優化的目標是針對具體性能問題而優化代碼或編譯選項&#xff0c;以改善軟…

linux C線程

一個應用程序可以啟動若干個線程&#xff1b;線程&#xff0c;是程序執行的最小單位&#xff1b;一般一個最簡單的程序最少有一個線程&#xff0c;就是程序本身&#xff0c;也是主函數&#xff1b;一個線程阻塞不會影響另一個線程&#xff1b;多線程的進程可以盡可能多的利用系…

fseek函數

fseek函數&#xff1a; int fseek(FILE *_FILE, long _Offset, int _Origin); 函數設置文件指針stream的位置&#xff0c;如果執行成功&#xff0c;stream將指向以fromwhere為基準&#xff0c;偏移量offset&#xff08;指針偏移量&#xff09;個字節的位置&#xff0c;函數返回…