C 为什么fputs()在一个文件中只添加一个单词?

C 为什么fputs()在一个文件中只添加一个单词?,c,file-handling,C,File Handling,在这个程序中,我的edit()函数不能正常工作。当我试图写一些东西时,它只会删除整个内容,而在appendText()中只添加了一个单词。有没有办法将整个字符串写入文件 #include<stdio.h> #include<stdlib.h> #include<string.h> main(void){ char fName[100]; printf("Enter file name :\n"); scanf("%s",&a

在这个程序中,我的edit()函数不能正常工作。当我试图写一些东西时,它只会删除整个内容,而在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()
#包括
#包括
#包括
主(空){
char-fName[100];
printf(“输入文件名:\n”);
scanf(“%s”,&fName);//文件名
智力选择;
printf(“输入您的选择:\n1.Edit text\n2.Read文件内容\n3.Append text\n4.Exit\n”);//输入选择
scanf(“%d”,选择(&C);
开关(选择){
案例1:
编辑(fName);//编辑文本
打破
案例2:
readContents(fName);//读取文件
打破
案例3:
appendText(fName);//追加
打破
案例4:
退出(0);//退出
打破
违约:
printf(“无效选项!\n”);
打破
}//终端开关
}//端干管
//用于编辑文件内容的函数
无效编辑(字符文件[100]){
内线,温度=0;
printf(“输入要编辑的行号:\n”);
scanf(“%d”,&line);//行号
char语句[100];
printf(“输入内容:\n”);
scanf(“%s”,句子);
char-str[100];
文件*fName=fopen(文件“w”);
而(!feof(fName)){
temp++;
fgets(str,99,fName);
如果(行==温度)
FPUT(句子,fName);中断;
} 
printf(“\n文件内容已更新!\n”);
fclose(fName);
}//结束编辑()
//函数读取文件的内容
无效读取内容(字符文件[100]){
char-str[100];
文件*fName=fopen(文件“r”);
而(!feof(fName)){
看跌期权(fgets(str,99,fName));
} 
fclose(fName);
printf(“\n”);
}//End readContents()
//将字符串附加到现有文件的函数
无效追加文本(字符文件[100]){
char-str[100];
文件*fName=fopen(文件“a”);
printf(“输入字符串:\n”);
scanf(“%s”和&str);
fputs(str,fName);
fclose(fName);
printf(“\n文本已添加到文件\n”);
}//append()的结尾

你不能仅仅改变一个(文本)文件的内容来改变你正在做的一行。写入新字符串时,将覆盖文件的其余部分。您需要将整个文件读入内存,更改所需的行,然后将整个内容写入文件。或者,从文件中读取,将每一行写入第二个文件(找到文本时替换),然后删除/重命名文件

对于附录文本,从%s开始(强调):

任意数量的非空白字符,在找到的第一个空白字符处停止。将在存储序列的末尾自动添加终止空字符


也就是说,您对追加的scanf只读取第一个单词,因此为什么只追加第一个单词。

您不能仅仅更改(文本)文件的内容,以更改您正在执行的方式的一行。写入新字符串时,将覆盖文件的其余部分。您需要将整个文件读入内存,更改所需的行,然后将整个内容写入文件。或者,从文件中读取,将每一行写入第二个文件(找到文本时替换),然后删除/重命名文件

对于附录文本,从%s开始(强调):

任意数量的非空白字符,在找到的第一个空白字符处停止。将在存储序列的末尾自动添加终止空字符


也就是说,您的追加扫描只读取第一个单词,因此为什么只追加第一个单词。

此代码的许多问题之一是,当您以模式
“w”
打开文件时,会删除其所有现有内容。如果您不想这样做,请改用模式
“r+”

此代码的许多问题之一是,当您以模式
“w”
打开文件时,会删除其所有现有内容。如果您不想这样做,请使用模式
“r+”

如何读取内存中的文件,然后更改其内容?您可能需要使用数组和动态内存分配(
malloc/free
)。在您进一步了解该语言之前,使用2文件方法可能更容易;这样,您一次只需要处理一行文本。如何读取内存中的文件,然后更改其内容?您可能需要使用数组和动态内存分配(
malloc/free
)。在您进一步了解该语言之前,使用2文件方法可能更容易;这样,一次只需要处理一行文字。