【问题标题】:Editing a text file in c在c中编辑文本文件
【发布时间】:2010-12-05 09:14:57
【问题描述】:

你们能帮我写代码吗.. 我想使用 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 w/c。

但我仍然不知道如何替换学生 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,因为我从他那里得到了这个想法......不幸的是我无法完成它......希望你能帮助我......

【问题讨论】:

    标签: c file handle


    【解决方案1】:

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

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

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

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

    虽然您可以使您的方法适用于删除指定学生 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。がんばってください
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 2010-12-30
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多