I have a text file "123.txt" with this content:
我有一个带有以下内容的文本文件“123.txt”:
123456789
123456789
I want the output to be:
我希望输出为:
123
456
789123 456 789
This means, a newline character must be inserted after every 3 characters.
这意味着,必须在每3个字符后插入一个换行符。
void convert1 (){
FILE *fp, *fq;
int i,c = 0;
fp = fopen("~/123.txt","r");
fq = fopen("~/file2.txt","w");
if(fp == NULL)
printf("Error in opening 123.txt");
if(fq == NULL)
printf("Error in opening file2.txt");
while (!feof(fp)){
for (i=0; i<3; i++){
c = fgetc(fp);
if(c == 10)
i=3;
fprintf(fq, "%c", c);
}
if(i==4)
break;
fprintf (fq, "\n");
}
fclose(fp);
fclose(fq);
}
My code works fine, but prints a newline character also at the end of file, which is not desired. This means, a newline character is added after 789 in the above example. How can I prevent my program from adding a spurious newline character at the end of the output file?
我的代码工作正常,但也在文件的末尾打印换行符,这是不希望的。这意味着,在上面的示例中,在789之后添加换行符。如何防止我的程序在输出文件的末尾添加虚假的换行符?
2 个解决方案
#1
2
As indicated in the comments, your while
loop is not correct. Please try to exchange your while
loop with the following code:
如评论中所示,您的while循环不正确。请尝试使用以下代码交换while循环:
i = 0;
while(1)
{
// Read a character and stop if reading fails.
c = fgetc(fp);
if(feof(fp))
break;
// When a line ends, then start over counting (similar as you did it).
if(c == '\n')
i = -1;
// Just before a "fourth" character is written, write an additional newline character.
// This solves your main problem of a newline character at the end of the file.
if(i == 3)
{
fprintf(fq, "\n");
i = 0;
}
// Write the character that was read and count it.
fprintf(fq, "%c", c);
i++;
}
Example: A file containing:
示例:包含以下内容的文件
12345
12345678912345 123456789
is turned into a file containing:
变成一个包含以下内容的文件:
123
45
123
456
789123 45 123 456 789
#2
0
I think you should do your new line at the beggining of the lopp:
我认为你应该在lopp的开始时做你的新线:
// first read
c = fgetc(fp);
i=0;
// fgetc returns EOF when end of file is read, I usually do like that
while((c = fgetc(fp)) != EOF)
{
// Basically, that means "if i divided by 3 is not afloating number". So,
// it will be true every 3 loops, no need to reset i but the first loop has
// to be ignored
if(i%3 == 0 && i != 0)
{
fprintf (fq, "\n");
}
// Write the character
fprintf(fq, "%c", c);
// and increase i
i++;
}
I can't test it right now, maybe there is some mistakes but you see what I mean.
我现在无法测试,也许有一些错误,但你明白我的意思。
#1
2
As indicated in the comments, your while
loop is not correct. Please try to exchange your while
loop with the following code:
如评论中所示,您的while循环不正确。请尝试使用以下代码交换while循环:
i = 0;
while(1)
{
// Read a character and stop if reading fails.
c = fgetc(fp);
if(feof(fp))
break;
// When a line ends, then start over counting (similar as you did it).
if(c == '\n')
i = -1;
// Just before a "fourth" character is written, write an additional newline character.
// This solves your main problem of a newline character at the end of the file.
if(i == 3)
{
fprintf(fq, "\n");
i = 0;
}
// Write the character that was read and count it.
fprintf(fq, "%c", c);
i++;
}
Example: A file containing:
示例:包含以下内容的文件
12345
12345678912345 123456789
is turned into a file containing:
变成一个包含以下内容的文件:
123
45
123
456
789123 45 123 456 789
#2
0
I think you should do your new line at the beggining of the lopp:
我认为你应该在lopp的开始时做你的新线:
// first read
c = fgetc(fp);
i=0;
// fgetc returns EOF when end of file is read, I usually do like that
while((c = fgetc(fp)) != EOF)
{
// Basically, that means "if i divided by 3 is not afloating number". So,
// it will be true every 3 loops, no need to reset i but the first loop has
// to be ignored
if(i%3 == 0 && i != 0)
{
fprintf (fq, "\n");
}
// Write the character
fprintf(fq, "%c", c);
// and increase i
i++;
}
I can't test it right now, maybe there is some mistakes but you see what I mean.
我现在无法测试,也许有一些错误,但你明白我的意思。