c - 在c中编辑文本文件

标签 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,这是输出

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

, 姓名 1, bsba

12346,name2,bsba

12347, name3, bsba

12350, name4, bsba

12390,姓名 5,bs

这是原始文件:

12345, name1, bsba

12346,name2,bsba

12347, name3, bsba

12350, name4, bsba

12390,姓名 5,bs

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

最佳答案

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

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

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

我怀疑写作业的人希望您阅读整行,将其拆分(通过寻找逗号)到各个字段并将信息放入您的 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

关于c - 在c中编辑文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358123/

相关文章:

c# - 如何将文件 (PDF) 插入到 varbinary SQL Server 列中并稍后检索它?

windows - 为什么有些控件没有窗口句柄?

macos - 如何在Mac OS X上列出POSIX信号量

c - 为什么匹配的正则表达式字符串即使匹配也没有存储?

c - Printf 到 Fprintf

c - 如何在C中删除char指针数组

c - 我的 C 代码在第二次循环运行时在 "realloc"函数中中断

java - 为什么对于某些文件,getResourceAsStream()和使用FileInputStream读取文件会返回不同长度的数组?

javascript - 如何使以下 readAsDataURL 返回多个 readAsDataURL?

haskell - Haskell中的句柄是半封闭错误?