C中的fopen和fprintf未按预期工作?
我正在尝试编写一个程序,将行号添加到已经存在的txt文件中 例如,如果文件当前为:C中的fopen和fprintf未按预期工作?,c,file,printf,fopen,C,File,Printf,Fopen,我正在尝试编写一个程序,将行号添加到已经存在的txt文件中 例如,如果文件当前为: Hello this is an exercise 然后在运行代码后,它将是: (1) Hello (2) this is (3) an (4) exercise 我写了这段代码: #include<stdio.h> #include<conio.h> FILE *fp; void main() { int counter=1; char newline; fp
Hello
this is
an
exercise
然后在运行代码后,它将是:
(1) Hello
(2) this is
(3) an
(4) exercise
我写了这段代码:
#include<stdio.h>
#include<conio.h>
FILE *fp;
void main()
{
int counter=1;
char newline;
fp=fopen("G:\\name.txt","r+");
if(fp==NULL)
printf("Failed to open file!");
fprintf(fp,"(%d)",counter);
newline=fgetc(fp);
while(newline!=EOF)
{
if(newline=='\n')
{
counter++;
fprintf(fp,"(%d)",counter);
}
newline=fgetc(fp);
}
printf("All done!");
getch();
fclose(fp);
}
#包括
#包括
文件*fp;
void main()
{
int计数器=1;
字符换行符;
fp=fopen(“G:\\name.txt”、“r+”);
如果(fp==NULL)
printf(“打开文件失败!”);
fprintf(fp,(%d)”,计数器);
换行符=fgetc(fp);
while(换行符!=EOF)
{
如果(换行符=='\n')
{
计数器++;
fprintf(fp,(%d)”,计数器);
}
换行符=fgetc(fp);
}
printf(“全部完成!”);
getch();
fclose(fp);
}
输出很奇怪
首先,它不会在文件的开头打印。出于某种原因,它从文件末尾开始。另一件奇怪的事情是,只有第一次印刷成功
while
循环中的那些是乱七八糟的(看起来像小点,根本不像数字)
当我在fopen中使用“r+”时,整个数据都被删除了,我所能看到的只是(1)和胡言乱语
如果在FPEN中使用“A+”,则从文件的末尾开始,然后写入(1)和GABBERISH。
AFIAK,基本上不能将“插入”字节放在文件中间。相反,您将覆盖文件中的字节。因此,当您使用同一个文件进行读写时,您将“干扰您自己”。
我建议您为写入的输出创建一个临时文件,或者只写入标准输出,这样您就可以通过管道将输出传输到合适的位置>AfAIK,基本上不能在文件的中间插入“字节”。相反,您将覆盖文件中的字节。因此,当您使用同一个文件进行读写时,您将“干扰您自己”。 我建议您为写入输出创建一个临时文件,或者只写到标准输出,这样您就可以将输出管道到适当的位置 以下是我的建议:
#include <stdio.h>
// None conio.h
// Not global variable fp
#include <stdlib.h> // To use exit()
void handle_error(char* message);
int is_a10power(int n);
int main(void) // Standard form of main()
{
FILE *fp1 = fopen("G:\\name.txt","r"); // 1st file open for reading only
if (fp1 == NULL)
handle_error("Error open file for reading\n");
FILE *fp2 = fopen("G:\\name2.txt","w"); // 2nd file open for writting only
if (fp1 == NULL)
handle_error("Error open file for writting\n");
int io_result;
char c = '\n'; // The name "newline" is not adequate: changed by "c"
int counter = 0; // Start in 0 because increment will be done at the beginning
int no_digits = 0; // Number of digits of "counter"
for(;;) // Loop
{
if (c == '\n') // End-of-line handling
{
counter++;
if (is_a10power(counter))
no_digits++;
io_result = fprintf(fp2,"(%d) ", counter);
if (io_result < 3 + no_digits) // Error: not all bytes could be written to fp2
handle_error("Cannot write file\n");
}
c = fgetc(fp1); // Reads only 1 character from fp1
if (ferror(fp1))
handle_error("Cannot read file\n");
if (c == EOF)
break; // <--------- Loop exits here
io_result = fprintf(fp2,"%c", c);
if (io_result < 1) // Less than 1 bytes transmited implies "fail"
handle_error("Cannot write file\n");
}
io_result = fclose(fp1);
if (io_result == EOF)
handle_error("Close file error\n");
io_result = fclose(fp2);
if (io_result == EOF)
handle_error("Close file error\n");
printf("All done!"); // The success message goes after all operations are finished
getchar(); // I like more getchar() than getch()
return 0; // Success
}
void handle_error(char* message)
{
printf("%s",message);
exit(EXIT_FAILURE); // If error, end the program
}
int is_a10power(int n)
{
while (n % 10 == 0)
n /= 10;
return (n == 1);
}
#包括
//无conio.h
//非全局变量fp
#包含//以使用exit()
无效句柄错误(字符*消息);
int是_a10power(int n);
int main(void)//main()的标准形式
{
FILE*fp1=fopen(“G:\\name.txt”,“r”);//打开第一个文件,仅供阅读
如果(fp1==NULL)
handle_error(“打开文件读取时出错”);
FILE*fp2=fopen(“G:\\name2.txt”,“w”);//第二个文件仅用于写入
如果(fp1==NULL)
handle_error(“打开文件进行写入时出错”);
国际io_结果;
char c='\n';//名称“newline”不足够:由“c”更改
int counter=0;//从0开始,因为增量将在开始时完成
int no_digits=0;//计数器的位数
for(;;)//循环
{
if(c=='\n')//行结束处理
{
计数器++;
如果(是功率(计数器))
没有数字++;
io_结果=fprintf(fp2,(%d)”,计数器);
if(io_结果<3+无数字)//错误:并非所有字节都能写入fp2
处理错误(“无法写入文件”);
}
c=fgetc(fp1);//从fp1中仅读取1个字符
if(铁合金(fp1))
处理错误(“无法读取文件”);
如果(c==EOF)
中断;//以下是我的建议:
#include <stdio.h>
// None conio.h
// Not global variable fp
#include <stdlib.h> // To use exit()
void handle_error(char* message);
int is_a10power(int n);
int main(void) // Standard form of main()
{
FILE *fp1 = fopen("G:\\name.txt","r"); // 1st file open for reading only
if (fp1 == NULL)
handle_error("Error open file for reading\n");
FILE *fp2 = fopen("G:\\name2.txt","w"); // 2nd file open for writting only
if (fp1 == NULL)
handle_error("Error open file for writting\n");
int io_result;
char c = '\n'; // The name "newline" is not adequate: changed by "c"
int counter = 0; // Start in 0 because increment will be done at the beginning
int no_digits = 0; // Number of digits of "counter"
for(;;) // Loop
{
if (c == '\n') // End-of-line handling
{
counter++;
if (is_a10power(counter))
no_digits++;
io_result = fprintf(fp2,"(%d) ", counter);
if (io_result < 3 + no_digits) // Error: not all bytes could be written to fp2
handle_error("Cannot write file\n");
}
c = fgetc(fp1); // Reads only 1 character from fp1
if (ferror(fp1))
handle_error("Cannot read file\n");
if (c == EOF)
break; // <--------- Loop exits here
io_result = fprintf(fp2,"%c", c);
if (io_result < 1) // Less than 1 bytes transmited implies "fail"
handle_error("Cannot write file\n");
}
io_result = fclose(fp1);
if (io_result == EOF)
handle_error("Close file error\n");
io_result = fclose(fp2);
if (io_result == EOF)
handle_error("Close file error\n");
printf("All done!"); // The success message goes after all operations are finished
getchar(); // I like more getchar() than getch()
return 0; // Success
}
void handle_error(char* message)
{
printf("%s",message);
exit(EXIT_FAILURE); // If error, end the program
}
int is_a10power(int n)
{
while (n % 10 == 0)
n /= 10;
return (n == 1);
}
#包括
//无conio.h
//非全局变量fp
#包含//以使用exit()
无效句柄错误(字符*消息);
int是_a10power(int n);
int main(void)//main()的标准形式
{
FILE*fp1=fopen(“G:\\name.txt”,“r”);//第一个文件仅为只读而打开
如果(fp1==NULL)
handle_error(“打开文件读取时出错”);
FILE*fp2=fopen(“G:\\name2.txt”,“w”);//第二个文件仅用于写入
如果(fp1==NULL)
handle_error(“打开文件进行写入时出错”);
国际io_结果;
char c='\n';//名称“newline”不充分:由“c”更改
int counter=0;//从0开始,因为增量将在开始时完成
int no_digits=0;//计数器的位数
for(;;)//循环
{
if(c=='\n')//行结束处理
{
计数器++;
如果(是功率(计数器))
没有数字++;
io_结果=fprintf(fp2,(%d)”,计数器);
if(io_结果<3+无数字)//错误:并非所有字节都能写入fp2
处理错误(“无法写入文件”);
}
c=fgetc(fp1);//从fp1中仅读取1个字符
if(铁合金(fp1))
处理错误(“无法读取文件”);
如果(c==EOF)
break;//不是您的主要问题,但是fgetc
返回一个int
,使用newline
作为char
会给您带来意外的结果。这不是您的主要问题,但是fgetc
返回一个int
,使用newline
作为char
会给您带来意外的结果。