为什么 fputs() 只在文件中添加一个单词?
Why fputs() only add one word in a file?
在这个程序中,我的 edit() 函数不起作用 properly.When 我尝试写一些东西,它只会擦除所有内容,而在 appendText() 中只附加一个词?有什么方法可以写整个字符串到文件?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(void){
char fName[100];
printf("Enter file name :\n");
scanf("%s",&fName); //File Name
int choice;
printf("Enter your choice : \n1.Edit text\n2.Read the contents of the file\n3.Append text\n4.Exit\n"); //Enter choice
scanf("%d",&choice);
switch(choice){
case 1 :
edit(fName); //Edit text
break;
case 2 :
readContents(fName); //Read file
break;
case 3 :
appendText(fName); //Append
break;
case 4 :
exit(0); //Exit
break;
default :
printf("Invalide Option!\n");
break;
}//End switch
}//End main
//Function to edit contents of the file
void edit(char file[100] ){
int line,temp = 0;
printf("Enter the line no. to be edited : \n");
scanf("%d",&line); //Line no
char sentence[100];
printf("Enter the content : \n");
scanf("%s",sentence);
char str[100];
FILE *fName = fopen(file,"w");
while(!feof(fName)){
temp++;
fgets(str,99,fName);
if(line == temp)
fputs(sentence,fName); break;
}
printf("\nContents of the file has been updated!\n");
fclose(fName);
}//End edit()
//Function to read the contents of the file
void readContents(char file[100]){
char str[100];
FILE *fName = fopen(file,"r");
while(!feof(fName)){
puts(fgets(str,99,fName));
}
fclose(fName);
printf("\n");
} //End readContents()
//Funtion to append string to an existing file
void appendText(char file[100]){
char str[100];
FILE *fName = fopen(file,"a");
printf("Enter your string :\n");
scanf("%s",&str);
fputs(str,fName);
fclose(fName);
printf("\nText added to the file\n");
}//End of append()
您不能真的只更改(文本)文件的内容来更改您正在执行的操作中的一行。当您写入新字符串时,您将覆盖文件的其余部分。您需要将整个文件读入内存,更改所需的行,然后将整个内容写入文件。或者,从文件中读取,将每一行写入第二个文件(找到后替换文本),然后 delete/rename 文件。
至于 appendText,来自 http://www.cplusplus.com/reference/cstdio/scanf/ 上的 %s(强调我的):
Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
即,您的追加 scanf 仅读取第一个单词,因此为什么只追加第一个单词。
此代码的众多问题之一是,当您使用模式 "w"
打开文件时,您 删除了其所有现有内容 。如果您不想这样做,请改用模式 "r+"
。
在这个程序中,我的 edit() 函数不起作用 properly.When 我尝试写一些东西,它只会擦除所有内容,而在 appendText() 中只附加一个词?有什么方法可以写整个字符串到文件?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(void){
char fName[100];
printf("Enter file name :\n");
scanf("%s",&fName); //File Name
int choice;
printf("Enter your choice : \n1.Edit text\n2.Read the contents of the file\n3.Append text\n4.Exit\n"); //Enter choice
scanf("%d",&choice);
switch(choice){
case 1 :
edit(fName); //Edit text
break;
case 2 :
readContents(fName); //Read file
break;
case 3 :
appendText(fName); //Append
break;
case 4 :
exit(0); //Exit
break;
default :
printf("Invalide Option!\n");
break;
}//End switch
}//End main
//Function to edit contents of the file
void edit(char file[100] ){
int line,temp = 0;
printf("Enter the line no. to be edited : \n");
scanf("%d",&line); //Line no
char sentence[100];
printf("Enter the content : \n");
scanf("%s",sentence);
char str[100];
FILE *fName = fopen(file,"w");
while(!feof(fName)){
temp++;
fgets(str,99,fName);
if(line == temp)
fputs(sentence,fName); break;
}
printf("\nContents of the file has been updated!\n");
fclose(fName);
}//End edit()
//Function to read the contents of the file
void readContents(char file[100]){
char str[100];
FILE *fName = fopen(file,"r");
while(!feof(fName)){
puts(fgets(str,99,fName));
}
fclose(fName);
printf("\n");
} //End readContents()
//Funtion to append string to an existing file
void appendText(char file[100]){
char str[100];
FILE *fName = fopen(file,"a");
printf("Enter your string :\n");
scanf("%s",&str);
fputs(str,fName);
fclose(fName);
printf("\nText added to the file\n");
}//End of append()
您不能真的只更改(文本)文件的内容来更改您正在执行的操作中的一行。当您写入新字符串时,您将覆盖文件的其余部分。您需要将整个文件读入内存,更改所需的行,然后将整个内容写入文件。或者,从文件中读取,将每一行写入第二个文件(找到后替换文本),然后 delete/rename 文件。
至于 appendText,来自 http://www.cplusplus.com/reference/cstdio/scanf/ 上的 %s(强调我的):
Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
即,您的追加 scanf 仅读取第一个单词,因此为什么只追加第一个单词。
此代码的众多问题之一是,当您使用模式 "w"
打开文件时,您 删除了其所有现有内容 。如果您不想这样做,请改用模式 "r+"
。