我必须创建一个程序,将file1.txt
的内容复制到file2.txt
,但它不会只复制粘贴" file1.txt
到file2.txt
例如,file1.txt
包含:
Name: Katrina , Age: 19 , Hobbies: Coding
Name: Karl, Age: 21, Hobbies: Guitar
file2.txt
会复制file1.txt
的内容,但仅限年龄。因此file2.txt
将包含:
Name: Katrina, Age: 19
Name: Karl, Age: 21
起初,我的思考过程是:
将", Hobbies"
放入字符串
计算file1.txt
中的字符数,直至其显示", Hobbies"
(计数器)
计数器将复制每个字符的file1.txt字符,计数次数。
这是我的代码:
FILE *ptr1;
FILE *ptr2;
char c[100];
int count = 1;
char end[100];
char k;
strcpy(end, ", Hobbies");
ptr1= fopen("file1.txt", "r");
ptr2 = fopen("file2.txt", "a");
while((k = fgetc(ptr1) != end)
{
if (k == ' ')
count++;
}
还有其他想法吗?谢谢。
答案 0 :(得分:1)
只要您对文件格式不会发生变化这一事实感到满意,您就可以使用一种非常简单的方法:
1读取行。
2在行中查找结束字符串并将其剪掉。
3将行复制到第二个文件。
#include <stdio.h>
#include <string.h>
int main()
{
char line[100];
FILE *ptr1 = fopen("file1.txt", "r");
FILE *ptr2 = fopen("file2.txt", "a");
// While no errors and no end of file when reading a line.
while (fgets(line, sizeof(line), ptr1))
{
// If you happen to have read empty lines from your file, re-read line.
if (line[0] == '\n') continue;
// Get position of end string.
char *lineEnd = strstr(line, ", Hobbies");
// Cut off the rest of the line with a null terminator.
*lineEnd = '\0';
// Write cut line into file2.txt.
fprintf(ptr2, "%s\n", line);
}
// Close files.
fclose(ptr1);
fclose(ptr2);
return 0;
}
或者如果你知道你没有空行:
#include <stdio.h>
#include <string.h>
int main()
{
char line[100];
FILE *ptr1 = fopen("file1.txt", "r");
FILE *ptr2 = fopen("file2.txt", "a");
while (fgets(line, sizeof(line), ptr1))
{
char *lineEnd = strstr(line, ", Hobbies");
*lineEnd = '\0';
fprintf(ptr2, "%s\n", line);
}
fclose(ptr1);
fclose(ptr2);
return 0;
}
答案 1 :(得分:1)
strtok使用分隔符将字符串分解为一系列标记。在这里你可以使用&#34;,&#34;作为你的分隔符
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp1;
FILE *fp2;
char line[100];
char *token;
const char s[2] = ",";
fp1 = fopen("file1.txt", "r");
fp2 = fopen("file2.txt", "a");
while (fgets(line, sizeof(line), fp1))
{
token = strtok(line,s);
fprintf(fp2,"%s",token);
token = strtok(NULL, s);
fprintf(fp2,"%s\n",token);
}
fclose(fp1);
fclose(fp2);
return 0;
}
答案 2 :(得分:0)
您可以使用strtok
查找分隔符。阅读它的手册页然后你应该能够自己弄清楚其余部分。
一个提示:对于您阅读的每一行,您只需拨打strtok
一次。
答案 3 :(得分:0)
您可以使用单个bash命令执行此操作,但如果您必须拥有c程序,则一种解决方案是一次将一行复制到缓冲区中,然后在缓冲区中搜索分隔符的出现位置。在所需的出现次数下,用空字符替换分隔符。将字符串写入第二个文件时,它将停止在空字符处。然后添加换行符。像这样:
FILE *ptr1;
FILE *ptr2;
char c[100];
char delim = ',';
ptr1 = fopen("file1.txt", "r");
ptr2 = fopen("file2.txt", "a");
while(fgets(c, 100, ptr1))
{
int i = 0;
//find first delimiter
while (c[i] != delim && c[i] != '\0') i++;
//only advance index if we are not at the end of the string
if(c[i] != '\0') i++;
//find second delimeter
while (c[i] != delim && c[i] != '\0') i++;
//replace delimeter with null byte
c[i] = '\0';
//write out to second file
fputs(c, ptr2);
//fgets will contain newline at end of string,
// so only print newline to file if we did not print the whole string
if (c[i-1] != '\n') fputc('\n', ptr2);
}
此外,您的上述代码尚未完成 - 您没有包含文件且没有主要功能。另外,不要忘记在循环后关闭文件指针。
答案 4 :(得分:0)
Awk很擅长这个。以下是您需要的命令:
awk -F, '{OFS=",";print $1,$2}' file1.txt > file2.txt
答案 5 :(得分:0)
这是使用@Bodo Thiesen最初建议的strtok的另一种解决方案。这个版本确实增加了一些检查。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILE_NAME "input.txt"
#define OUTPUT_FILE_NAME "output.txt"
#define TOKEN_STR ","
int main(void)
{
FILE *inputFile,
*outputFile;
char *curLine = NULL,
*token;
size_t len = 0;
ssize_t read;
inputFile = fopen(INPUT_FILE_NAME, "r");
if (NULL == inputFile) {
printf("Could not open the input file '%s'\n", INPUT_FILE_NAME);
exit(EXIT_FAILURE);
}
outputFile = fopen(OUTPUT_FILE_NAME, "w");
if (NULL == outputFile) {
printf("Could not open the output file '%s'\n", OUTPUT_FILE_NAME);
fclose(inputFile);
exit(EXIT_FAILURE);
}
while ((read = getline(&curLine, &len, inputFile)) != -1) {
printf("%s\n", curLine);
token = strtok(curLine, TOKEN_STR);
fprintf(outputFile, "%s", token);
fprintf(outputFile, ", ");
token = strtok(NULL, TOKEN_STR);
fprintf(outputFile, "%s\n", token);
}
fclose(outputFile);
fclose(inputFile);
exit(EXIT_SUCCESS);
}