Linux C Programming File Operation jobs

Source: Internet
Author: User
Tags parse string

The contents of the A.txt file are as follows:
32
45
65
32
67
454
89
54
24
75
3
67
890
32
1
1, write a program to read the A.txt file, the file content number from small to large sorting, and the results of the sorting to write B.txt

#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>void sort (int *a,    int n) {int i, j, temp; for (j = 0, J < n-1; J + +) {for (i = 0; i < n-1-J; i++) {if (A[i] > a[i + 1])                array element size in ascending order {temp = A[i];                A[i] = a[i + 1];            A[i + 1] = temp;    }}}}int Main () {//read a.txt FILE *FPA = NULL;    FPA = fopen ("A.txt", "R");        if (FPA = = NULL) {printf ("FPA open error:%s", strerror (errno));    return-1;    } int buf[100];    memset (buf, 0, sizeof (BUF));    Char val[100];    int ilen = 0;        while (1) {memset (val, 0, sizeof (Val));        if (Fgets (Val, sizeof (Val), FPA) = = NULL) {break;        } Buf[ilen] = Atoi (val);        printf ("buf[%d]=%d\n", Ilen, Buf[ilen]);    ilen++;    } fclose (FPA);    Sorts sort (buf, ilen);    Write b.txt FILE *FPB = NULL; FPB = fopen ("B.txt", "w");        if (FPB = = NULL) {printf ("FPB open error:%s", strerror (errno));    return-1;    } int loop = 0;        for (loop = 0; loop < Ilen; loop++) {memset (val, 0, sizeof (Val));        sprintf (Val, "%d\n", Buf[loop]);    Fputs (Val, FPB);    } fclose (FPB); return 0;}

2, by reading the relevant files under the/proc, get the system CPU information, memory information.

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h># Include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include < stdlib.h>/* string parsing *//* Open file, the string to be found in the file, the returned data address *///char *start;char *parse_string (char *file, char *string) {static C    Har resultstr[100];    memset (resultstr, 0, sizeof (RESULTSTR));    int FD;    Char pbuf[1024*10];    int nread;    Char *ptmp;    Char *start;    Char *end;    if (!file | |!string) {return NULL;    }/* Open file */fd = open (file, o_rdonly);        if (FD < 0) {printf ("Open%s file error!\n", file);    return NULL; }/* Reads the file into the buffer */nread = read (fd, pbuf, sizeof (PBUF));//The contents of the file are stored in Pbuf if (Nread < 0) {printf ("Rea        D%s File error! ", file);    return NULL;    }/* Find the string in the file */ptmp = Strstr (Pbuf, String); Ptmp + = strlen (string);//skips the found string position while ((*ptmp = = ") | | (*ptmp = = ': ') | | (*pTMP = = ' \ t ')) ptmp++;    start = ptmp;    End = STRCHR (Ptmp, ");//Find the required data *end = ' + ';    memcpy (ResultStr, start, End-start); return resultstr;}    int main () {printf ("%s\n", Parse_string ("/proc/meminfo", "Memfree")); return 0;}

The contents of the C.txt file are as follows index1 = 45index2 = 36index3 = 231index4 = 43index5 = 100index6 = 123INDEX7 = 51
3. Read the C.txt file contents by reading the Equals right value, and print the right value maximum, minimum and average value to the screen.

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <    Errno.h>char *TRIMSTRR (char *srcstr)//Remove the space to the right of the string {if (srcstr = = null) return null;    int ilen = strlen (SRCSTR);    int i;        for (i = (iLen-1); I >= 0; i--) {if (srcstr[i] = = ") Srcstr[i] = 0;    else break; } return srcstr;}    Char *trimstrl (char *srcstr)//Remove the space at the left of the string {if (srcstr = = null) return null;    if (srcstr[0]! = ") return srcstr;    int ilen = strlen (SRCSTR);    if (Ilen = = 0) return srcstr;    Char *stmp = (char *) malloc (Ilen + 1);    memset (sTmp, 0, Ilen + 1);    memcpy (STMP, Srcstr, Ilen);    int i;            for (i = 0; i < Ilen; i++) {if (Srcstr[i]! = ") {strcpy (sTmp, srcstr + i);        Break    }} strcpy (Srcstr, STMP);    Free (STMP); return SRCSTR;} void Parsevaluestr (char *deststr, char *srcstr)//parse string with equals sign and remove word Nginx trailing space {intIlen = strlen (SRCSTR);    if (Ilen = = 0) return; if ((srcstr[ilen-1] = = ' \ n ') | | (srcstr[ilen-1] = = ' \ r '))    is compatible with the Windows format text file, so resolves the \ r character Srcstr[ilen-1] = ' + '; if (Ilen > 1) {if ((srcstr[ilen-2] = = ' \ n ') | | (Srcstr[ilen-2] = = ' \ r '))    is compatible with the Windows format text file, so resolves the \ r character Srcstr[ilen-2] = ' + '; } TRIMSTRR (SRCSTR);    Remove trailing space int i;            for (i = 0; i < Ilen; i++) {if ((*srcstr) = = ' = ') {strcpy (deststr, ++SRCSTR);        Break    } ++srcstr; } Trimstrl (DESTSTR);    Remove the header space}int max (const int *BUF, const int bufsize)//calculates the maximum value in the array buf, the parameter bufsize the number of elements buf the element {int tmp = buf[0];    int i = 0;    for (; i < bufsize; i++) {if (tmp <= buf[i]) tmp = Buf[i]; } return TMP;    int min (const int *BUF, const int bufsize)//calculates the minimum value in the array buf, and the parameter bufsize the number of elements buf the element {int tmp = buf[0];    int i = 0; for (; i < bufsize; i++) {if (tmp >= buf[i]) tmp =Buf[i]; } return TMP;    float AVG (const int *BUF, const int bufsize)//calculates the average value in the array buf, and the parameter bufsize the number of elements buf the element {float sum = 0;    int i = 0;    for (; i < bufsize; i++) {sum + = Buf[i]; } return sum/bufsize;}    int main (int arg, char *args[]) {const char *filename = "C.txt";    FILE *f = fopen (filename, "R");        if (f = = NULL)//file C.txt open error {printf ("Open%s failed%s\n", filename, strerror (errno));    return-1;    } Char svalue[100];    Char buf[100];    int ivalue[1024];    memset (ivalue, 0, sizeof (ivalue));    int i = 0; while (!) (        Feof (f)))//loop through each line of the file {memset (svalue, 0, sizeof (svalue));        memset (buf, 0, sizeof (BUF)); Fgets (svalue, sizeof (svalue), f); Reads a row of Parsevaluestr (buf, svalue) from the C.txt file;        Parse the string with equal sign and remove the word nginx trailing space ivalue[i] = atoi (BUF); i++;    Accumulator, which records the total number of rows in the C.txt file} fclose (f);    printf ("Maximum value is%d\n", Max (Ivalue, i));    printf ("Minimum value is%d\n", min (Ivalue, i));  printf ("Average value is%f\n", avg (Ivalue, i));  return 0;} 


Linux C Programming File Operation jobs

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.