Subtask 1: read the contents of a text file line by line and print out (15min)
Input: text file: text1.txt. The contents of this document are:
hellp hello hello world and sym and sym
Output:
hellp hello hello world and sym and sym
Relevant knowledge:
1. Use fopen function to open a FILE and return a pointer to the FILE structure
fopen prototype:
FILE *fopen(const char *path, const char *mode); The library function is a function in the C language standard library. The prototype is declared in the file < stdio. H >. Therefore, the header file < stdio. H > needs to be included before using the fopen function. The fopen function opens a file whose name is given by the function parameter path. The parameter mode points to a string, which contains the way to access the file. It has the following functions:
"r": open the file as read-only, the file must exist.
"w": open the write only file. If the file exists, the file length will be cleared to 0, that is, the file content will disappear. If the file does not exist, the file is created.
"r +": to open a file in read-write mode, the file must exist.
"w +": open the read-write file. If the file exists, the file length will be cleared to zero, that is, the file content will disappear. If the file does not exist, the file is created.
"a": open the write only file as an attachment. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
"A +": open a read-write file in an additional way. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
If the FILE is opened successfully, the fopen function returns a pointer to the FILE structure. Otherwise, it returns NULL and stores the error code in errno.
FILE is a structure that contains all the information required to manage FILE streams, including FILE descriptors, pointers to buffers, etc.
2.getline library function
Prototype: size_t getline(char **lineptr, size_t *n, FILE *stream);getline reads a line of string from the file stream and stores the address of the string in the pointer pointed to by lineptr. The returned string ends with the null character '\ 0' and contains a newline character. The parameter n represents the maximum number of characters that can be read at one time.
If * lineptr is set to NULL and * n is set to 0 before calling getline, getline will allocate a buffer to store a line of read string. The buffer should be released by the user program. Release is performed by calling the free function.
Application example code of getline function:
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; char *line = NULL; size_t len = 0; ssize_t read; fp = fopen("/etc/motd", "r"); if (fp == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, fp)) != -1) { printf("Retrieved line of length %zu :\n", read); printf("%s", line); } free(line); exit(EXIT_SUCCESS); }
3.int fclose(FILE* stream) closes a stream. Using the fclose() function, you can output the last remaining data in the buffer to the disk file, and release the file pointer and related buffer.
Question code
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; char *line = NULL; size_t len = 0; ssize_t read;// ssize_t is equivalent to int fp = fopen("text1.txt", "r"); if (fp == NULL) exit(EXIT_FAILURE);//Represents an abnormal exit while ((read = getline(&line, &len, fp)) != -1) { printf("%s", line); } free(line); exit(EXIT_SUCCESS);//Represents safe exit. } ~
size_t type is essentially unsigned int, an unsigned integer
Method 2
#include <stdio.h> #Include < stdlib. H > / / PERROR is included in this file #include <string.h> int main() { char buf[1024]; FILE *fp; int len; //The file path here can be modified as needed. It is in the current directory by default if((fp = fopen("text1.txt","r")) == NULL) { perror("open failed!");//The perror function just takes some of the information you enter //Output together with the error corresponding to the current error exit (1) ;//Abnormal execution causes the program to exit } while(fgets(buf, 1024, fp) != NULL) { printf("%s",buf); } putchar('\n'); fclose(fp); return 0; }
Equivalent to
#include <stdlib.h> #include<stdio.h> #include <string.h> int main() { char buf[1024]; FILE *fp; int len; //The file path here can be modified as needed. It is in the current directory by default fp=fopen("text1.txt","r"); while(fgets(buf, 1024, fp) != NULL) { printf("%s",buf); } putchar('\n'); fclose(fp); return 0; }
2. String array exercise
Task description:
Create a string array using a two-dimensional array, store multiple input strings in the string array, and finally print out the string array
main.c:
#include<stdio.h> #include<string.h> #define N 88 int main() { char a[N][N]; int n; scanf("%d\n", &n); for (int i=0;i<n;i++) { gets(a[i]); } for(int i=0;i<n;i++) { puts(a[i]); } return 0; }
Subtask 3: string array exercise. Input multiple strings from the terminal, create a string array using malloc function, store the input multiple strings in the string array, and finally print out the string array.
Input format:
5 (Number of strings entered,Next is a five line string.) hellp hello hello world and sym
Output:
hellp hello hello world and sym
Relevant knowledge:
1. Memory allocation function malloc function prototype: void malloc(size_t size);
Header file: stdlib.h this function allocates size bytes and returns a pointer to this memory. If the allocation fails, a NULL pointer is returned. Void indicates a pointer of undetermined type, and void * can point to any type of data.
Example code: allocate a character array to store strings.
#include <stdio.h> #include <stdlib.h> int main() { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "helloworld"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); }
2 string array
char ** lines = (char **)malloc(3*sizeof(char *). Then lines in memory are expressed as:
Allocate a memory space with malloc, including three pointers to characters, and return a pointer to the first address of the memory space lines
3. String copy function: strcpy function prototype: char strcpy(char dest, const char *src); Header file: #include < string. H >
Copy the string starting from the src address and containing the NULL terminator to the address space starting with dest. The memory area referred to by src and dest cannot overlap, and dest must have enough space to accommodate the src string. Returns a pointer to dest.
Example code:
#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); }
4. Memory release function free
Function prototype: void free(void *ptr)
Header file: stdlib.h
Free the storage space pointed to by ptr. The freed space is usually sent to the available storage pool, which can be reallocated later by calling malloc, realloc and calloc functions. For the memory allocated by malloc, you need to call the free function to release the memory. Otherwise, memory leakage will occur.
int * p = (int*)malloc(5*sizeof(int)); free(p);
Title code
#include<stdlib.h> #include<string.h> #define SIZE 20 int main(void) { char **str=NULL; char buf[SIZE]; int i,strnum; scanf("%d",&strnum); str=(char **)malloc(strnum*sizeof(char *)); for(i=0;i<strnum;i++){ str[i]=(char *)malloc(SIZE*sizeof(char)); memset(buf,'\0',sizeof(buf)); scanf("%s",buf); strcpy(str[i],buf); } printf("your input string is\n"); for(i=0;i<strnum;i++){ printf("%s\n",str[i]); free(str[i]); } free(str); }
There is a problem with the code above
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 88 int main() { char **a; int n; scanf("%d\n", &n); a = (char**) malloc(sizeof(char*) * n); for (int i=0;i<n;i++) { a[i] = (char*)malloc(sizeof(char)*N); gets(a[i]); } printf("output:\n"); for (int i=0;i<n;i++) { puts(a[i]); } for (int i=0;i<n;i++) { free(a[i]); } free(a); return 0; }
Subtask 4: be familiar with sorting integer arrays from small to large using qsort library function
Input n lines by the terminal, and each line contains an integer. Sort the N integers from small to large, and then output the sorted integers.
input
5 100 84 123 -13 0
Output:
-13 0 84 100 123
Relevant knowledge:
1. Quick sort function qsort
Function prototype: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *);
Header file: stdlib.h
Parameters:
base: the first address of the array to be sorted
nelem: number of elements to be sorted in the array
width: the size of the array element
fcmp: function pointer, used to compare the size of two elements in the array and determine the order of the two elements. This function needs to be defined by itself. During the execution of qsort, this function will be called to compare the sizes of the two elements and determine the order of the two elements.
The function type pointed to by fcmp is:
int (*fcmp)(const void *,const void *);
Application example: sort integer arrays from small to large
#include <stdio.h> #include <stdlib.h> int values[] = { 88, 56, 100, 2, 25 }; int cmpfunc (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main() { int n; printf("Before sorting the list is: \n"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } qsort(values, 5, sizeof(int), cmpfunc); printf("\nAfter sorting the list is: \n"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } return(0); }
Program running results:
Before sorting the list is: 88 56 100 2 25 After sorting the list is: 2 25 56 88 100
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function( const void *a, const void *b); int main(void) { int a,i,n;int num[N]; scanf("%d",&a); printf("before sorting the number is:\n"); for(i=0;i<a;i++) scanf("%d",&num[i]); //n=strlen(num); for(i=0;i<a;i++) printf("%d\n",num[i]); qsort((void *)num,a,sizeof(int),sort_function); printf("after sorting the number is:\n"); for(i=0;i<a;i++) printf("%d\n",num[i]); return 0; } int sort_function( const void *a, const void *b) { return(*(int*)a-*(int*)b); }
code
#include<stdio.h> #include<stdlib.h> #define N 100 int cmp(const void *a, const void *b) { return *((int*)a) - *((int*)b); } int main() { int n; int a[N]; scanf("%d", &n); for (int i=0;i<n;i++) { scanf("%d", &a[i]); } qsort(a, n, sizeof(int), cmp); printf("result: \n"); for (int i=0;i<n;i++) { printf("%d\n", a[i]); } return 0; }
Subtask 5: sort the strings in each line of a text file in alphabetical order, and print out the sorted strings in each line (30min)
Input: a text file with 6 lines of text: test1.txt, as follows:
hellp hello hello world and sym and sym
Output:
and sym and sym hello hello hellp world
Relevant knowledge:
1. Alphabetical order:
Sort by the size of the letter ASCII code. Compare the ASCII code size of letters in each position from left to right. If there are different letters, you can determine the order of the two strings. If a string is a subset of another string, the shorter string comes first. The ASCII code of 26 English letters increases from small to large.
For example: "a" < "b", "abca" < "abdd", "abc" < "abcd"
2. String comparison function strcmp
Prototype: int strcmp(const char *s1,const char * s2);
Header file: string.h
Function: compare strings s1 and s2.
General form: StrCmp (string 1, string 2)
explain:
When s1<s2 Returns a negative number When s1=s2 Return value when= 0 When s1>s2 Returns a positive number
That is, the two strings are compared character by character from left to right (compared according to the size of ASCII value) until different characters appear or '\ 0' is encountered. For example:
"A"<"B" "a">"A" "computer">"compare"
Code example:
#include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strcmp(str1, str2); if(ret > 0) { printf("str1 is less than str2"); } else if(ret < 0) { printf("str2 is less than str1"); } else { printf("str1 is equal to str2"); } return(0); }
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function(const void *a, const void *b); int main(int argc,char *argv[]) { FILE *fp; char *line=NULL; size_t len=0; ssize_t read; char buf[N][N]; int i=0,a=0,b=0; fp=fopen("test1.txt","r"); if(fp==NULL){ perror("argv error\n"); exit(1); } printf("before sorting the string is:\n"); while((read=getline(&line,&len,fp))!=-1){ strcpy(buf[i],line); printf("%s",line); i++; } qsort(buf,i,sizeof(buf[0]),sort_function); printf("after sorting the string is:\n"); for(a=0;a<i;a++) printf("%s",buf[a]); free(line); return 0; } int sort_function(const void *a, const void *b) { char *str1 = (char *)a; char *str2 = (char *)b; return strcmp(str1,str2); }
code
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<errno.h> #define N 100 int cmp(const void *a, const void *b) { char *aa = (char*)a; char *bb = (char*)b; return strcmp(aa,bb); } int main() { FILE *fp; char *line = NULL; char s[N][N]; size_t len = 0; fp = fopen("./test1.txt", "r"); if (fp == NULL) { printf("%s\n", strerror(errno)); } int k = 0; while ((getline(&line, &len, fp)) != -1) { strcpy(s[k++], line); } qsort(s, k, sizeof(s[0]), cmp); for (int i=0;i<k;i++) { printf("%s", s[i]); } fclose(fp); return 0; }
Subtask 6: delete duplicate lines in the text file and save the results to a new file. (only one line is reserved for repeated lines, and the text lines in the new file are arranged in alphabetical order, keeping the contents of the original file unchanged) (30min)
Input: text file test1.txt, as follows:
hellp hello hello world and sym and sym
Output: a new text file with the following contents:
and sym hello hellp world
Relevant knowledge:
1. Use qsort to sort the files, find out the duplicate lines in the file, and write the non duplicate lines to the new file. (completed on the basis of subtask 2)
2. Use the fopen function to create a new file to save the results
Using "w" mode, a new file is created when the file does not exist.
3. Use the fwrite function to write non duplicate lines to a new file
Function prototype: size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
(1) buffer: is a pointer. For fwrite, it is the address to obtain data;
(2) Size: the size of each element in the buffer
(3) count: the number of data items to be written;
(4) stream: target file pointer;
(5) Returns the number of data items actually written count.
5. Use sizeof to find the number of elements in the array
sizeof is an operator in C/C + +. In short, its function is to return the number of memory bytes occupied by an object or type.
For an array array, use sizeof to find the size of all elements of the array, and then use sizeof to find the size of each element in the array. In this way, you can find the number of elements in the array.
array array Number of elements in = sizeof(array) / sizeof(array[0])
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function(const void *a, const void *b); int main(int argc,char *argv[]) { FILE *fp1,*fp2; char *line=NULL; size_t len=0; ssize_t read; char buf[N][N]; int i=0,a=0,b=0; fp1=fopen("test1.txt","r"); fp2=fopen("test2.txt","w"); while((read=getline(&line,&len,fp1))!=-1){ strcpy(buf[i],line); i++; } qsort(buf,i,sizeof(buf[0]),sort_function); fwrite(buf[0],1,strlen(buf[0]),fp2); //a=i; for(a=1;a<i;a++){ if(strcmp(buf[a],buf[a-1])==0) continue; else fwrite(buf[a],1,strlen(buf[a]),fp2); } free(line); return 0; } int sort_function(const void *a, const void *b) { char *str1 = (char *)a; char *str2 = (char *)b; return strcmp(str1,str2); }
code
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<errno.h> #define N 100 int cmp(const void *a, const void *b) { char *aa = (char*)a; char *bb = (char*)b; return strcmp(aa,bb); } int main() { FILE *fp; char *line = NULL; char s[N][N]; size_t len = 0; fp = fopen("test1.txt", "r"); if (fp == NULL) { printf("%s\n", strerror(errno)); } int k = 0; while ((getline(&line, &len, fp)) != -1) { strcpy(s[k++], line); } qsort(s, k, sizeof(s[0]), cmp); FILE *fp1 = fopen("test2.txt", "w"); fwrite(s[0], sizeof(char), strlen(s[0]), fp1); for (int i=1;i<k;i++) { if (strcmp(s[i-1],s[i])) { fwrite(s[i], sizeof(char), strlen(s[i]), fp1); } } fclose(fp); fclose(fp1); return 0; }
Subtask 7: find the union of two text files that have been arranged in alphabetical order and have no duplicate lines, and write the results to a new file (the new file also needs to be arranged in alphabetical order and have no duplicate lines). (30min)
Input: two text files, test1.txxt and test2.txt, with the contents as follows:
text1.txt:
and sym hello hellp world
test2.txt:
and sym hello shijie
Output: Union file, file1_b_file2, as follows:
and sym hello hellp shijie world
Relevant knowledge:
1. This subtask is similar to finding the union of two sets. You can refer to the implementation of the union of two sets to complete this subtask. Reference example:
Array A=[1,2,3]
Array B=[2,3,5]
Array C[10]: used to store the results of union.
Find the union of A and B:
int i = 0, j = 0, k = 0; while( i < 3 && j < 3){ // Judge the size of the two elements and store the smaller elements in array C if(A[i] < B[j]){ C[k++] = A[i]; i++ } else if(A[i] > B[j] { C[k++] = B[j]; j++; } else { //The two elements are the same, and only one of them is placed in array C C[k++] = A[i]; i++; j++; } } // If there is an array that has not been traversed, all the elements in the array are placed in array C. while(i < 3){ //Judge whether array A has been traversed C[k++] = A[i]; i++; } while(j < 3){ //Judge whether the B array has been traversed C[k++] = B[j]; j++; }
Run result: C array: 1 2 3 5
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function( const void *a, const void *b); int main(int argc,char *argv[]) { FILE *fp1,*fp2,*fp3; char *line1=NULL,*line2=NULL; size_t len1=0,len2=0; ssize_t read; int i=0,cmprel,num1,num2; fp1=fopen("test1.txt","r"); fp2=fopen("test2.txt","r"); fp3=fopen("test3.txt","w"); num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); while(num1!=-1&&num2!=-1){ cmprel=strcmp(line1,line2); if(cmprel<0){ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); } else if(cmprel>0){ fwrite(line2,1,strlen(line2),fp3); num2=getline(&line2,&len2,fp2); }else{ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); } } while(num1!=-1){ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); } while(num2!=-1){ fwrite(line2,1,strlen(line2),fp3); num2=getline(&line2,&len2,fp2); } free(line1);free(line2); fclose(fp1);fclose(fp2);fclose(fp3); return 0; }
code
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #define N 100 int main() { char s1[N][N]; char s2[N][N]; char str[N][N]; char *line = NULL; size_t len = 0; int k1 = 0; int k2 = 0; int k = 0; FILE *fp1 = fopen("./text1.txt", "r"); FILE *fp2 = fopen("./text2.txt", "r"); if (fp1 == NULL) { printf("%s\n", strerror(errno)); exit(1); } if (fp2 == NULL) { printf("%s\n", strerror(errno)); exit(1); } while ((getline(&line, &len, fp1)) != -1) { strcpy(s1[k1++], line); } while ((getline(&line, &len, fp2)) != -1) { strcpy(s2[k2++], line); } int i = 0, j = 0; while (i<k1 && j<k2) { if (strcmp(s1[i], s2[j]) > 0) { strcpy(str[k++], s2[j++]); } else if (strcmp(s1[i], s2[j]) < 0) { strcpy(str[k++], s1[i++]); } else { strcpy(str[k++], s1[i++]); j++; } } while (i<k1) { strcpy(str[k++], s1[i++]); } while (j<k2) { strcpy(str[k++], s2[j++]); } FILE *fp = fopen("file1_b_file2", "w"); for (int i=0;i<k;i++) { fwrite(str[i], sizeof(char), strlen(str[i]), fp); } fclose(fp1); fclose(fp2); fclose(fp); return 0; }
Subtask 8: find the intersection of two text files that have been arranged in alphabetical order and have no duplicate lines, and write the results to a new file (the new file also needs to be arranged in alphabetical order and have no duplicate lines). (30min)
Input: two text files, test1.txxt and test2.txt, with the contents as follows:
text1.txt:
and sym hello hellp world
test2.txt:
and sym hello shijie
Output: intersection file, file1_j_file2, as follows:
and sym hello
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function( const void *a, const void *b); int main(int argc,char *argv[]) { FILE *fp1,*fp2,*fp3; char *line1=NULL,*line2=NULL; size_t len1=0,len2=0; ssize_t read; int i=0,cmprel,num1,num2; fp1=fopen("test1.txt","r"); fp2=fopen("test2.txt","r"); fp3=fopen("test3.txt","w"); num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); while(num1!=-1&&num2!=-1){ cmprel=strcmp(line1,line2); if(cmprel<0) num1=getline(&line1,&len1,fp1); else if(cmprel>0) num2=getline(&line2,&len2,fp2); else{ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); } } while(num1!=-1) num1=getline(&line1,&len1,fp1); while(num2!=-1) num2=getline(&line2,&len2,fp2); free(line1);free(line2); fclose(fp1);fclose(fp2);fclose(fp3); return 0; }
code
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #define N 100 int main() { char s1[N][N]; char s2[N][N]; char str[N][N]; char *line = NULL; size_t len = 0; int k1 = 0; int k2 = 0; int k = 0; FILE *fp1 = fopen("./text1.txt", "r"); FILE *fp2 = fopen("./text2.txt", "r"); if (fp1 == NULL) { printf("%s\n", strerror(errno)); exit(1); } if (fp2 == NULL) { printf("%s\n", strerror(errno)); exit(1); } while ((getline(&line, &len, fp1)) != -1) { strcpy(s1[k1++], line); } while ((getline(&line, &len, fp2)) != -1) { strcpy(s2[k2++], line); } int i = 0, j = 0; while (i<k1 && j<k2) { if (strcmp(s1[i], s2[j]) > 0) { j++; } else if (strcmp(s1[i], s2[j]) < 0) { i++; } else { strcpy(str[k++], s1[i++]); j++; } } FILE *fp = fopen("file1_i_file2", "w"); for (int i=0;i<k;i++) { fwrite(str[i], sizeof(char), strlen(str[i]), fp); } fclose(fp1); fclose(fp2); fclose(fp); return 0; }
Subtask 9: find the difference set of two text files that have been arranged in alphabetical order and have no duplicate lines, and write the results to the new file (the new file also needs to be arranged in alphabetical order and have no duplicate lines). (30min)
Input: two text files, test1.txxt and test2.txt, with the contents as follows:
text1.txt:
and sym hello hellp world
test2.txt:
and sym hello shijie
Output: difference set file, test1.txt – test2.txt, file1_b_file2, as follows:
hellp world
Relevant knowledge:
In general, remember that A and B are two sets, then the set composed of all elements belonging to A and not belonging to B is called set A minus set B.
If A={0,1,2,3}, B={1,2,4}, then A - B = {0,3}. B - A = {4}
Question code
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 50 int sort_function( const void *a, const void *b); int main(int argc,char *argv[]) { FILE *fp1,*fp2,*fp3; char *line1=NULL,*line2=NULL; size_t len1=0,len2=0; ssize_t read; int i=0,cmprel,num1,num2; fp1=fopen("test1.txt","r"); fp2=fopen("test2.txt","r"); fp3=fopen("test3.txt","w"); num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); while(num1!=-1&&num2!=-1){ cmprel=strcmp(line1,line2); if(cmprel<0){ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); }else if(cmprel>0) num2=getline(&line2,&len2,fp2); else{ num1=getline(&line1,&len1,fp1); num2=getline(&line2,&len2,fp2); } } while(num1!=-1){ fwrite(line1,1,strlen(line1),fp3); num1=getline(&line1,&len1,fp1); } while(num2!=-1) num2=getline(&line2,&len2,fp2); free(line1);free(line2); fclose(fp1);fclose(fp2);fclose(fp3); return 0; }
code
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #define N 100 int main() { char s1[N][N]; char s2[N][N]; char str[N][N]; char *line = NULL; size_t len = 0; int k1 = 0; int k2 = 0; int k = 0; FILE *fp1 = fopen("./text1.txt", "r"); FILE *fp2 = fopen("./text2.txt", "r"); if (fp1 == NULL) { printf("%s\n", strerror(errno)); exit(1); } if (fp2 == NULL) { printf("%s\n", strerror(errno)); exit(1); } while ((getline(&line, &len, fp1)) != -1) { strcpy(s1[k1++], line); } while ((getline(&line, &len, fp2)) != -1) { strcpy(s2[k2++], line); } int i = 0, j = 0; while (i<k1 && j<k2) { if (strcmp(s1[i], s2[j]) > 0) { j++; } else if (strcmp(s1[i], s2[j]) < 0) { strcpy(str[k++], s1[i++]); } else { i++; j++; } } while (i < k1) { strcpy(str[k++], s1[i++]); } FILE *fp = fopen("test1.txt-test2.txt", "w"); for (int i=0;i<k;i++) { fwrite(str[i], sizeof(char), strlen(str[i]), fp); } fclose(fp1); fclose(fp2); fclose(fp); return 0; }
Subtask 10 file copying
Description:
Pass in two file name parameters a.txt b.txt, create a new file b.txt, copy the contents of the file a.txt to b.txt, and use the C library function to operate the file
#include<stdio.h> int main() { char ch; FILE* pfr = fopen("a.txt", "r");//Change the path to the path of your file FILE* pfw = fopen("b.txt", "w");//Change the path to the path of your file if (NULL == pfw) { perror("open file test2.txt"); } if (NULL == pfr) { perror("open file test1.txt"); } //Continue to read characters from the source file and write them to the destination file until EOF is encountered while ((ch=fgetc(pfr))!=EOF)//EOF is the end of file flag { fputc(ch,pfw); } //Close the flow. Remember to close the flow after using it to avoid occupying resources fclose(pfr); fclose(pfw); pfr = NULL; pfw = NULL; return 0; }