如何从文件中读取并在c中写入另一个
How to read from file and write another in c
我有一个这样的输入文件:
This is 1nd Line.
This is 2nd Line.
This 3rd Line.
我需要输出类似
的文件
OddLines.txt:
This is 1nd Line.
This 3rd Line.
EvenLines.txt:
This is 2nd Line.
这是我的代码。并没有按照我的意愿工作。
char buf[256];
int ch;
int lines;
lines = 1;
FILE *myFile = fopen(argv[1], "r");
if (myFile == NULL) {
printf("Open error \n");
exit(-1);
}
FILE *outFile = fopen("oddlines.txt", "w");
FILE *outFile1 = fopen("evenlines.txt", "w");
while (fgets(buf, sizeof(buf), myFile) != NULL) {
if (ch == '\n')
lines++;
else
if ((lines % 2) == 0)
fputs(buf, outFile1);
else
fputs(buf, outFile);
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
}
if (ch == '\n')
这段代码没有意义。你想找到输入字符吗?如果是,您可以使用 strcspn
函数:
int pos = strcspn ( buf, "\n" );
lines
应该由 0
初始化,而不是 1
:
int lines = 0;
while(fgets(buf, sizeof(buf),myFile))
{
lines++;
int pos = strcspn ( buf, "\n" ); // find the enter character
buf[pos] = ' '; // add space at the end of string
buf[pos+1] = '[=12=]';
if ((lines%2)==0) // if even
fputs (buf,outFile1);
else // if odd
fputs (buf,outFile);
}
您应该检查 fopen
函数:
FILE *myFile = fopen(argv[1], "r");
if(!myFile) {return -1;}
FILE *outFile = fopen("oddlines.txt", "w");
if(!outFile) {return -1;}
FILE *outFile1 = fopen("evenlines.txt", "w");
if(!outFile1) {return -1;}
以及命令行条件:
if(argc < 2 ) {
printf("usage: %s <input_filename>", argv[0]);
return-1;
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char buf[256];
int lines = 0;
if(argc < 2 ) {
printf("usage: %s <input_filename>", argv[0]);
return-1;
}
FILE *myFile = fopen(argv[1], "r");
if(!myFile) {return -1;}
FILE *outFile = fopen("oddlines.txt", "w");
if(!outFile) {return -1;}
FILE *outFile1 = fopen("evenlines.txt", "w");
if(!outFile1) {return -1;}
while(fgets(buf, sizeof(buf),myFile)!=NULL)
{
lines++;
int pos = strcspn ( buf, "\n" );
buf[pos] = ' ';
buf[pos+1] = '[=15=]';
if ((lines%2)==0)
fputs (buf,outFile1);
else
fputs (buf,outFile);
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
}
输入文件:
This is 1nd Line.
This is 2nd Line.
This is 3rd Line.
This is 4th Line.
This is 5th Line.
This is 6th Line.
输出文件:
$cat evenlines.txt
This is 2nd Line. This is 4th Line. This is 6th Line.
$cat oddlines.txt
This is 1nd Line. This is 3rd Line. This is 5th Line.
if (ch == '\n')
问题出在这一行。
#include <stdio.h>
#include<string.h>
void main()
{
int line=1;
char buf[256];
FILE *fp, *fp_Odd, *fp_Even;
fp=fopen("USERS.TXT", "r");
fp_Odd=fopen("ODD.TXT", "a");
fp_Even=fopen("EVEN.TXT", "a");
if(fp == NULL)
{
printf("Open error \n");
exit(-1);
}
while(fgets(buf, sizeof(buf), fp)!=NULL)
{
if(strcmp(buf, "\n")==0)
line++;
else if(line%2 != 0)
fprintf(fp_Odd, "%s", buf);
else
fprintf(fp_Even, "%s", buf);
line++;
}
fclose(fp);
}
您可以查看此代码。我已经进行了必要的更改。希望你明白。
不清楚您是希望输出文件包含输入文件中的行,还是必须连接这些行,去除换行符。
您的代码无法运行,因为测试 if (ch == '\n')
具有未定义的行为,因为 ch
未初始化。因此,行计数器未正确更新,所有行都进入其中一个文件。
计算行数实际上并不像计算循环迭代那么简单,因为输入文件中的某些行可能比 fgets()
使用的数组长度长。您应该在写入后更新行计数器,方法是测试刚刚写入的行是否实际包含换行符。
这是修改后的版本:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char buf[256];
int lines = 1;
FILE *myFile, *outFile, *outFile1;
if (argc < 2) {
printf("Missing argument\n");
return 1;
}
if ((myFile = fopen(argv[1], "r")) == NULL) {
printf("Open error for %s\n", argv[1]);
return 1;
}
if ((outFile = fopen("oddlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "oddlines.txt");
return 1;
}
if ((outFile1 = fopen("evenlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "evenlines.txt");
return 1;
}
while (fgets(buf, sizeof(buf), myFile) != NULL) {
if (lines % 2 == 0)
fputs(buf, outFile1);
else
fputs(buf, outFile);
/* if a full line was read, increment the line number */
if (strchr(buf, '\n')
lines++;
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
return 0;
}
这里是一个不使用fgets()
的更简单的版本:
#include <stdio.h>
int main(int argc, char *argv[]) {
int c, out;
FILE *myFile, *outFile[2];
if (argc < 2) {
printf("Missing argument\n");
return 1;
}
if ((myFile = fopen(argv[1], "r")) == NULL) {
printf("Open error for %s\n", argv[1]);
return 1;
}
if ((outFile[0] = fopen("oddlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "oddlines.txt");
return 1;
}
if ((outFile[1] = fopen("evenlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "evenlines.txt");
return 1;
}
out = 0; /* current output file is "oddlines.txt" */
while ((c = getc(myFile)) != EOF) {
putc(c, outFile[out]);
if (c == '\n')
out = 1 - out; /* change current output file */
}
fclose(myFile);
fclose(outFile[0]);
fclose(outFile[1]);
return 0;
}
如果您希望从第三个输出文件中去除元音,只需包含 <string.h>
,打开该文件至 FILE *noVowelFile
并在 while
循环中添加一个测试:
out = 0; /* current output file is "oddlines.txt" */
while ((c = getc(myFile)) != EOF) {
if (strchr("aeiouAEIOU", c) == NULL) {
putc(c, noVowelFile);
}
putc(c, outFile[out]);
if (c == '\n')
out = 1 - out; /* change current output file */
}
我有一个这样的输入文件:
This is 1nd Line. This is 2nd Line. This 3rd Line.
我需要输出类似
的文件OddLines.txt:
This is 1nd Line. This 3rd Line.
EvenLines.txt:
This is 2nd Line.
这是我的代码。并没有按照我的意愿工作。
char buf[256];
int ch;
int lines;
lines = 1;
FILE *myFile = fopen(argv[1], "r");
if (myFile == NULL) {
printf("Open error \n");
exit(-1);
}
FILE *outFile = fopen("oddlines.txt", "w");
FILE *outFile1 = fopen("evenlines.txt", "w");
while (fgets(buf, sizeof(buf), myFile) != NULL) {
if (ch == '\n')
lines++;
else
if ((lines % 2) == 0)
fputs(buf, outFile1);
else
fputs(buf, outFile);
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
}
if (ch == '\n')
这段代码没有意义。你想找到输入字符吗?如果是,您可以使用 strcspn
函数:
int pos = strcspn ( buf, "\n" );
lines
应该由 0
初始化,而不是 1
:
int lines = 0;
while(fgets(buf, sizeof(buf),myFile))
{
lines++;
int pos = strcspn ( buf, "\n" ); // find the enter character
buf[pos] = ' '; // add space at the end of string
buf[pos+1] = '[=12=]';
if ((lines%2)==0) // if even
fputs (buf,outFile1);
else // if odd
fputs (buf,outFile);
}
您应该检查 fopen
函数:
FILE *myFile = fopen(argv[1], "r");
if(!myFile) {return -1;}
FILE *outFile = fopen("oddlines.txt", "w");
if(!outFile) {return -1;}
FILE *outFile1 = fopen("evenlines.txt", "w");
if(!outFile1) {return -1;}
以及命令行条件:
if(argc < 2 ) {
printf("usage: %s <input_filename>", argv[0]);
return-1;
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char buf[256];
int lines = 0;
if(argc < 2 ) {
printf("usage: %s <input_filename>", argv[0]);
return-1;
}
FILE *myFile = fopen(argv[1], "r");
if(!myFile) {return -1;}
FILE *outFile = fopen("oddlines.txt", "w");
if(!outFile) {return -1;}
FILE *outFile1 = fopen("evenlines.txt", "w");
if(!outFile1) {return -1;}
while(fgets(buf, sizeof(buf),myFile)!=NULL)
{
lines++;
int pos = strcspn ( buf, "\n" );
buf[pos] = ' ';
buf[pos+1] = '[=15=]';
if ((lines%2)==0)
fputs (buf,outFile1);
else
fputs (buf,outFile);
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
}
输入文件:
This is 1nd Line.
This is 2nd Line.
This is 3rd Line.
This is 4th Line.
This is 5th Line.
This is 6th Line.
输出文件:
$cat evenlines.txt
This is 2nd Line. This is 4th Line. This is 6th Line.
$cat oddlines.txt
This is 1nd Line. This is 3rd Line. This is 5th Line.
if (ch == '\n')
问题出在这一行。
#include <stdio.h>
#include<string.h>
void main()
{
int line=1;
char buf[256];
FILE *fp, *fp_Odd, *fp_Even;
fp=fopen("USERS.TXT", "r");
fp_Odd=fopen("ODD.TXT", "a");
fp_Even=fopen("EVEN.TXT", "a");
if(fp == NULL)
{
printf("Open error \n");
exit(-1);
}
while(fgets(buf, sizeof(buf), fp)!=NULL)
{
if(strcmp(buf, "\n")==0)
line++;
else if(line%2 != 0)
fprintf(fp_Odd, "%s", buf);
else
fprintf(fp_Even, "%s", buf);
line++;
}
fclose(fp);
}
您可以查看此代码。我已经进行了必要的更改。希望你明白。
不清楚您是希望输出文件包含输入文件中的行,还是必须连接这些行,去除换行符。
您的代码无法运行,因为测试 if (ch == '\n')
具有未定义的行为,因为 ch
未初始化。因此,行计数器未正确更新,所有行都进入其中一个文件。
计算行数实际上并不像计算循环迭代那么简单,因为输入文件中的某些行可能比 fgets()
使用的数组长度长。您应该在写入后更新行计数器,方法是测试刚刚写入的行是否实际包含换行符。
这是修改后的版本:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char buf[256];
int lines = 1;
FILE *myFile, *outFile, *outFile1;
if (argc < 2) {
printf("Missing argument\n");
return 1;
}
if ((myFile = fopen(argv[1], "r")) == NULL) {
printf("Open error for %s\n", argv[1]);
return 1;
}
if ((outFile = fopen("oddlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "oddlines.txt");
return 1;
}
if ((outFile1 = fopen("evenlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "evenlines.txt");
return 1;
}
while (fgets(buf, sizeof(buf), myFile) != NULL) {
if (lines % 2 == 0)
fputs(buf, outFile1);
else
fputs(buf, outFile);
/* if a full line was read, increment the line number */
if (strchr(buf, '\n')
lines++;
}
fclose(myFile);
fclose(outFile);
fclose(outFile1);
return 0;
}
这里是一个不使用fgets()
的更简单的版本:
#include <stdio.h>
int main(int argc, char *argv[]) {
int c, out;
FILE *myFile, *outFile[2];
if (argc < 2) {
printf("Missing argument\n");
return 1;
}
if ((myFile = fopen(argv[1], "r")) == NULL) {
printf("Open error for %s\n", argv[1]);
return 1;
}
if ((outFile[0] = fopen("oddlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "oddlines.txt");
return 1;
}
if ((outFile[1] = fopen("evenlines.txt", "w")) == NULL) {
printf("Open error for %s\n", "evenlines.txt");
return 1;
}
out = 0; /* current output file is "oddlines.txt" */
while ((c = getc(myFile)) != EOF) {
putc(c, outFile[out]);
if (c == '\n')
out = 1 - out; /* change current output file */
}
fclose(myFile);
fclose(outFile[0]);
fclose(outFile[1]);
return 0;
}
如果您希望从第三个输出文件中去除元音,只需包含 <string.h>
,打开该文件至 FILE *noVowelFile
并在 while
循环中添加一个测试:
out = 0; /* current output file is "oddlines.txt" */
while ((c = getc(myFile)) != EOF) {
if (strchr("aeiouAEIOU", c) == NULL) {
putc(c, noVowelFile);
}
putc(c, outFile[out]);
if (c == '\n')
out = 1 - out; /* change current output file */
}