用c语言编辑文本文件

用c语言编辑文本文件,c,file,handle,C,File,Handle,伙计们,你们能帮我写代码吗。。我想用c编辑文本文件中的特定行我有这个代码 #include<stdio.h> #include<stdlib.h> #include<string.h> struct studentinfo{ char id[8]; char name[30]; char course[5]; }s1; int main(void){ FILE *stream = NULL;

伙计们,你们能帮我写代码吗。。我想用c编辑文本文件中的特定行我有这个代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct studentinfo{

       char id[8];
       char name[30];
       char course[5];
}s1;

int main(void){

     FILE *stream = NULL;
     FILE *stream2 = NULL;
     stream = fopen("studentinfo.txt", "rt");
     stream2 = fopen("studentinfo2.txt", "w+");

     char arr [100];
     char arr2[100];
     char arr3[100];
     int i=0;
     int count=0;

     printf("enter details: ");
     gets(arr2);
     printf("enter new student id: ");
     gets(arr3);

    while(!feof(stream)){ 
     fgets(arr, 6, stream);
        if(strcmp(arr, arr2)!=0){
        fprintf(stream2, "%s", arr);
        }else printf("student id found!");
    }
     fclose(stream);
     fclose(stream2);
     getch();
}
#包括
#包括
#包括
结构学生信息{
字符id[8];
字符名[30];
炭层[5];
}s1;
内部主(空){
文件*stream=NULL;
文件*stream2=NULL;
stream=fopen(“studentinfo.txt”、“rt”);
stream2=fopen(“studentinfo.txt”,“w+”);
char-arr[100];
char-arr2[100];
char-arr3[100];
int i=0;
整数计数=0;
printf(“输入详细信息:”);
获取(arr2);
printf(“输入新学生id:”);
获取(arr3);
而(!feof(stream)){
fgets(arr,6,溪流);
如果(strcmp(arr,arr2)!=0){
fprintf(流2,“%s”,arr);
}else printf(“找到学生id!”);
}
fclose(流);
fclose(stream2);
getch();
}
如果与文本文件中的数据匹配,程序将成功删除用户输入的学生id w/c

但我仍然不知道如何替换学生id或与之相关的任何字段

此程序仅复制与用户输入不相等的数据,并将其存储到另一个文本文件(我有2个文本文件)如果用户输入12345,则此为输出

它将数据存储到其他文件的方式:

,姓名1,英国工商管理局

12346,名称2,bsba

12347,名称3,bsba

12350,名称4,bsba

12390,名称5,英国标准

这是原始文件:

12345,名称1,英国工商管理局

12346,名称2,bsba

12347,名称3,bsba

12350,名称4,bsba

12390,名称5,英国标准

有更好的解决办法吗?谢谢:)
无论如何,再次感谢aix,因为我从他那里得到了这个想法。。。不幸的是我不能完成它。。。希望你能帮我…

你不能直接编辑文本文件。每当您需要更改特定内容时,您必须先在内存中更改该内容,然后再写回所有内容。

您不能直接编辑文本文件。每当您需要更改特定内容时,您必须首先在内存中更改该内容,然后将所有内容写回。

您一次只能读取5个字符。虽然这会起作用(因为fgets将在一行末尾停止),但效率非常低,这意味着您要将用户输入与文件的每6个字符进行比较,即使这些文件内容不是学生id

如果您确实希望继续使用程序的方法,当您确实获得与用户输入匹配的结果时,您需要在继续检查其他行之前读取(并放弃)该行的其余部分

对于不匹配的行,您应该读取(并复制到新文件中)行的其余部分,而不将其与用户输入进行比较(因为您知道它不是学生id)

我怀疑写作业的人希望你读入整行,将它(通过查找逗号)拆分到各个字段中,并将信息放入studentinfo结构中。然后以作业要求的任何方式处理studentinfo,最后用修改后的数据编写新文件

虽然您可以使您的方法用于删除指定学生id的记录,但它非常不灵活。搜索记录或添加记录需要完全重写程序。如果您有代码可以将信息读入studentinfo结构数组,然后再次写出该信息,那么您需要做的任何处理都只会在这些结构上工作,并且更改会小得多

所以,在伪代码中,您需要这样的东西

allocate space for one line of the file
allocate space for an array of struct studentinfos

readinfo function:

open the student info file for reading
set the count of student records to 0
while not at eof
    read in a line
    split the line on commas
        copy the bit before the first comma to the 'id' field of the newly allocated studentinfo record 
        copy the bit between first and second commas to the name field
        copy the bit from the second comma to the course field
    add one to the count of student records
go back to read another line
close the file

writeinfo function:
open the studentinfo file for writing
loop over the studentinfo structs in order
    writeout the id, name and course strings of the current record, separated by comma and followed by new line
close the file
deletestudent function:
read a course id from the user (or read it in your main program and pass it here as a parameter)
loop over the studentinfo array
    compare the id to the one of the current record
    if a match
        shift all records after this down one by copying them over the top of the record before
       subtract one from the number of student records (since we've deleted one)
       return from the function indicating found and delete
repeat for next record
if you complete looking at all records,
    return from the function indicating no match found

您一次只能读取5个字符。虽然这会起作用(因为fgets将在一行末尾停止),但效率非常低,这意味着您要将用户输入与文件的每6个字符进行比较,即使这些文件内容不是学生id

如果您确实希望继续使用程序的方法,当您确实获得与用户输入匹配的结果时,您需要在继续检查其他行之前读取(并放弃)该行的其余部分

对于不匹配的行,您应该读取(并复制到新文件中)行的其余部分,而不将其与用户输入进行比较(因为您知道它不是学生id)

我怀疑写作业的人希望你读入整行,将它(通过查找逗号)拆分到各个字段中,并将信息放入studentinfo结构中。然后以作业要求的任何方式处理studentinfo,最后用修改后的数据编写新文件

虽然您可以使您的方法用于删除指定学生id的记录,但它非常不灵活。搜索记录或添加记录需要完全重写程序。如果您有代码可以将信息读入studentinfo结构数组,然后再次写出该信息,那么您需要做的任何处理都只会在这些结构上工作,并且更改会小得多

所以,在伪代码中,您需要这样的东西

allocate space for one line of the file
allocate space for an array of struct studentinfos

readinfo function:

open the student info file for reading
set the count of student records to 0
while not at eof
    read in a line
    split the line on commas
        copy the bit before the first comma to the 'id' field of the newly allocated studentinfo record 
        copy the bit between first and second commas to the name field
        copy the bit from the second comma to the course field
    add one to the count of student records
go back to read another line
close the file

writeinfo function:
open the studentinfo file for writing
loop over the studentinfo structs in order
    writeout the id, name and course strings of the current record, separated by comma and followed by new line
close the file
deletestudent function:
read a course id from the user (or read it in your main program and pass it here as a parameter)
loop over the studentinfo array
    compare the id to the one of the current record
    if a match
        shift all records after this down one by copying them over the top of the record before
       subtract one from the number of student records (since we've deleted one)
       return from the function indicating found and delete
repeat for next record
if you complete looking at all records,
    return from the function indicating no match found

这个解决方案几乎和老师告诉我们的一样你真了不起,保罗先生。试试这个@newbieatc。がんばってください这个解决方案几乎和老师告诉我们的一样你真了不起,保罗先生。试试这个@newbieatc。がんばってください