[英]Combining two text files into third one and keeping track of data
我遇到了找不到的問題解決方案。
我將兩個文本文件合並為第三個文件,並且希望跟蹤正在移動的數據。 到目前為止,代碼只做一件事,而完全忽略了另一件事。
這是代碼:
// enable standard c i/o functions
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Open two files to be merged
FILE *fp1 = fopen("test1.txt", "r");
FILE *fp2 = fopen("test2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("results.txt", "w+");
char c;
int count2ch = 0; // count meter for characters
int count1ch = 0; // count meter for characters
int totalch = 0; // holds number of total ammount of characters
int count1wd = 0; // count meter for words
int count2wd = 0; // count meter for words
int totalWD = 0;// holds total ammount of words
// Check files
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open file");
exit(0);
}
// COUNTING CHARACTERS
// count characters file one
while (1)
{
c = fgetc(fp1);
if (c == EOF)
break;
count1ch++;
}
// count characters file two
while (1)
{
c = fgetc(fp2);
if (c == EOF)
break;
count2ch++;
}
//MERGING FILES
// Copy contents of first file to file3.txt
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
// Copy contents of second file to file3.txt
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
// COUNTING WORDS
//count words file one
while ((c = fgetc(fp1)) != EOF)
{
if (c == ' ')
count1wd++;
}
//count words file two
while ((c = fgetc(fp2)) != EOF)
{
if (c == ' ')
count2wd++;
}
// count total ammount of words
totalWD = count1wd + count2wd;
// count total ammount of characters
totalch = count1ch + count2ch;
printf("Merged file1.txt and file2.txt into file3.txt \n");
printf("Total number of characters moved: %d\n", totalch);
printf("The ammount of chars in your first file is : %d\n", count1ch);
printf("The ammount of chars in your second file is : %d\n", count2ch);
printf("Total number of words moved: %d\n", totalWD);
printf("The ammount of words in your fist file is : %d\n", count1wd);
printf("The ammount of words in your second file is : %d\n", count2wd);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
現在,它只是將兩個文件合並為第三個文件,僅此而已。 如果將計數單詞或字符部分移至合並部分上方,則代碼將執行首先出現的操作。
這是您的代碼優化。 唯一的區別是簡化的變量名稱,將打印語句移至函數,最重要的是,增加了rewind()
函數以在使用后倒回文件指針。 格式也是我的風格,因此,如果您要更改格式,請放心。
#include <stdio.h>
#include <stdlib.h>
void printCtrs(int, int, int, int, int, int);
void printCtrs(int ttlCh, int c1ch, int c2ch, int ttlWD, int c1wd, int c2wd){
printf("Merged file1.txt and file2.txt into file3.txt \n");
printf("Total number of characters moved: %d\n", ttlCh);
printf("The ammount of chars in your first file is : %d\n", c1ch);
printf("The ammount of chars in your second file is : %d\n", c2ch);
printf("Total number of words moved: %d\n", ttlWD);
printf("The ammount of words in your fist file is : %d\n", c1wd);
printf("The ammount of words in your second file is : %d\n", c2wd);
}
int main(){
// Open files for reading/writing
FILE *fp1, *fp2, *fp3;
fp1 = fopen("test1.txt", "r");
fp2 = fopen("test2.txt", "r");
fp3 = fopen("results.txt", "w+");
// Declaring counters
char c;
int ttlCh = 0, c1ch = 0, c2ch = 0,
ttlWD = 0, c1wd = 0, c2wd = 0;
// If any files fail to open, abort
if (fp1 == NULL){
puts("Could not open file fp1");
exit(0);
}
if (fp2 == NULL){
puts("Could not open file fp2");
exit(0);
}
if (fp3 == NULL){
puts("Could not open file fp3");
exit(0);
}
// Read through files 1 and 2 and count characters
while (c = fgetc(fp1) != EOF){ c1ch++; }
rewind(fp1); // Reset file pointer 1 to start of file
while (c = fgetc(fp2) != EOF){ c2ch++; }
rewind(fp2); // Reset file pointer 2 to start of file
// Write file 1 and 2 to 3
while ((c = fgetc(fp1)) != EOF){ fprintf(fp3, "%c", c); }
while ((c = fgetc(fp2)) != EOF){ fprintf(fp3, "%c", c); }
// Count total words in 1 and 2
while ((c = fgetc(fp1)) != EOF){
if (c == ' '){ c1wd++; }
}
while ((c = fgetc(fp2)) != EOF){
if (c == ' '){ c2wd++; }
}
ttlWD = c1wd + c2wd;
ttlCh = c1ch + c2ch;
// Print counters
printCtrs(ttlCh, c1ch, c2ch, ttlWD, c1wd, c2wd);
// Close all files after use
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.