在c中编辑文本文件

时间:2010-12-05 09:14:58

标签: 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;
     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。

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

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

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

,name1,bsba

12346,name2,bsba

12347,name3,bsba

12350,name4,bsba

12390,name5,bs

这是原始文件:

12345,name1,bsba

12346,name2,bsba

12347,name3,bsba

12350,name4,bsba

12390,name5,bs

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

2 个答案:

答案 0 :(得分:2)

您一次只能阅读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

答案 1 :(得分:1)

您无法直接编辑文本文件。无论何时,您需要更改特定内容,您必须先在内存中更改内容,然后再写回所有内容。