C Language Fundamentals: String Assignment and File Operations
1.0 String Assignment
In C programming, strings are not a built-in data type. Instead, they can be represented using arrays of characters or pointers. Let’s explore the correct ways to assign strings in C.
// Correct string assignment using a pointer
char *s = "good";
*s = "good"; // Incorrect, as it will only copy the first character of the string
// Correct string assignment using an array
char s1[10];
s1 = "good"; // Incorrect, as it will only copy the first 5 characters of the string
// Correct string assignment using a pointer
char *s3 = "good";
*s3 = "good"; // Correct, as it will copy the entire string
2.0 Function: Comparing Strings
Here’s an example function that compares two strings using a while loop:
int f(char *a, char *b) {
while (*a++ == *b++) {
// If the strings are equal, return 0
return 0;
}
// If the strings are not equal, return 1
return 1;
}
3.0 String Incrementation
When incrementing a string using *aa++, the ++ operator has higher precedence than the dereference operator *. Therefore, only the first character of the string will be incremented:
char *aa = "123456";
printf("%c\n", *aa++); // Output: 1
4.0 Merging Two Files
Let’s discuss how to merge two files, A and B, into a new file C, in alphabetical order. We’ll use the C language to read the strings from both files, store them in a character array, and then write the sorted array to the new file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char *ch, int n) {
int i, j;
char min, temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (ch[j] < ch[min]) {
min = j;
}
}
if (min != i) {
temp = ch[i];
ch[i] = ch[min];
ch[min] = temp;
}
}
}
int main() {
FILE *fp, *fp1, *fp2;
int len;
char *p;
if ((fp1 = fopen("a.txt", "r+")) == NULL) {
printf("Open file A error");
exit(0);
}
if ((fp2 = fopen("b.txt", "r+")) == NULL) {
printf("Open file B error");
exit(0);
}
len = longFife(fp1) + longFife(fp2);
printf("File A: ");
rewind(fp1);
display(fp1);
printf("\n");
printf("File B: ");
rewind(fp2);
display(fp2);
printf("\n");
printf("Array length = %d\n", len);
p = (char *)malloc(sizeof(char) * (len + 1));
if (p != NULL) {
printf("Malloc array is succeed\n");
}
arry(fp1, fp2, p);
printf("\n");
printf("Before sort array: %s\n", p);
sort(p, len);
printf("After sort array: %s\n", p);
if ((fp = fopen("c.txt", "w+")) == NULL) {
printf("Open file C error");
exit(0);
}
fputs(p, fp);
printf("Write file is succeed\n");
printf("Read this file: ");
rewind(fp);
display(fp);
free(p);
fclose(fp);
fclose(fp1);
fclose(fp2);
fflush(fp);
fclose(fp);
free(p);
return 0;
}
void arry(FILE *fp1, FILE *fp2, char *ch) {
rewind(fp1);
rewind(fp2);
int n1 = longFife(fp1);
int n2 = longFife(fp2) + n1;
int i, j;
rewind(fp1);
for (i = 0; i < n1; i++) {
ch[i] = fgetc(fp1);
}
rewind(fp2);
for (j = n1; j < n2; j++) {
ch[j] = fgetc(fp2);
}
ch[n2] = '\0';
printf("Char array is succeed\n");
}
int longFife(FILE *fp) {
int len = 0;
char ch;
ch = fgetc(fp);
while (!feof(fp)) {
ch = fgetc(fp);
len++;
}
return len;
}
void display(FILE *fp) {
char ch;
while (!feof(fp)) {
ch = fgetc(fp);
putchar(ch);
}
}
5.0 FILE Structure and Operations
In C, the FILE structure represents a file. The fopen function opens a file, and the fclose function closes a file. The fseek function sets the current position to the read-write offset at.
FILE *fp;
fp = fopen("file.txt", "r+");
fseek(fp, 1, SEEK_CUR); // Move the current position 1 byte forward
fseek(fp, -1, SEEK_CUR); // Move the current position 1 byte backward
Knowledge Points
- Strings in C are not a built-in data type, but can be represented using arrays of characters or pointers.
- The
fopenfunction opens a file, and thefclosefunction closes a file. - The
fseekfunction sets the current position to the read-write offset at. - The
fgetcfunction reads a character from a file, and thefputsfunction writes a string to a file. - The
feoffunction checks if the end of the file has been reached.
Conclusion
In this article, we’ve covered the basics of string assignment and file operations in C. We’ve explored how to compare strings using a while loop, merge two files into a new file, and use the FILE structure to perform file operations. We’ve also discussed how to use the fseek function to set the current position to the read-write offset at. With these knowledge points, you’ll be able to write more efficient and effective C programs.