编辑在C文本文件 [英] Editing a text file in c

查看:146
本文介绍了编辑在C文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙你能帮助我与我的code ..我想用c我有这个code编辑一个文本文件中的特定行...

guys can you help me with my code.. i want to edit a specific line in a text file using c i have this code...

#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();
}

该程序成功地删除学生凭学生证W / C是由用户inputed如果在文本文件中匹配的数据。

The program successfully deletes the student id w/c was inputed by the user if it matches to the data in the text file.

但我仍然不知道如何来取代学生证或有关它的任何字段。

but i still don't know how to replace the student id or any fields related with it.

此方案仅复制资料所不等同于用户的输入并将其存储到另一个文本文件(我有2文本文件)这是在输出如果用户输入12345

this program only copies data which is not equivalent to the user's input and store it to another text file(i have 2 text files) this is the output if the user entered 12345

它的方式将数据存储到另一个文件:

the way it stores data to the other file:

,NAME1 BSBA

, name1, bsba

12346,NAME2 BSBA

12346, name2, bsba

12347,NAME3,BSBA

12347, name3, bsba

12350,NAME4,BSBA

12350, name4, bsba

12390,NAME5,BS

12390, name5, bs

这是原始文件:

12345,NAME1 BSBA

12345, name1, bsba

12346,NAME2 BSBA

12346, name2, bsba

12347,NAME3,BSBA

12347, name3, bsba

12350,NAME4,BSBA

12350, name4, bsba

12390,NAME5,BS

12390, name5, bs

没有更好的办法?谢谢 :)
反正再次感谢AIX,怎么把我心中已经从他那里得到了这个想法...不幸的是我不能完成它...希望你可以帮我...

any better solutions? thanks :) anyway thanks again to aix, coz i'v got this idea from him... unfortunately i cant finish it... hope you can help me...

推荐答案

您在每次读仅5个字符。虽然这将工作(因为与fgets将在一行的末尾停止),这是非常低效,意味着你的用户输入比较文件的每6个字符,即使这些文件的内容是不是学生证。

You are reading only 5 characters at a a time. While this will work (because fgets will stop at the end of a line), it's very inefficient and means you are comparing the users input to every 6 characters of a file, even when those file contents are not the student id.

如果你想继续使用你的程序的方法,当你获得与用户输入的匹配,需要继续研究进一步的行前阅读(丢弃)该行的其余部分。

If you do want to continue with the approach of your program, when you do get a match with the user input, you need to read (and discard) the rest of the line before continuing examining further lines.

对于不匹配,你应该阅读(和复制到新文件)中的行的剩余没有它比较用户输入(因为你知道它是不是学生证)线。

For lines that don't match, you should read (and copy into the new file) the remainder of the line without comparing it to the user input (since you know it is not the student id).

我怀疑到各个领域谁写的分配指望你在阅读整条生产线,分割它(通过寻找逗号)的人,并把信息到你的studentinfo结构。然后处理以任何方式转让要求studentinfo,并最终写入新文件,修改后的数据。

I suspect the person who wrote the assignment expected you to read an entire line in, split it (by looking for the commas) into the various fields and put the information into your studentinfo structures. Then process the studentinfo in whatever way the assignment requested, and finally write the new file with the modified data.

虽然你可以让你的工作方法删除指定的学生证的记录,这是非常不灵活。搜索记录或添加一条记录就需要你的程序完全重写。如果你有code,可以读取的信息转换成studentinfo结构的数组,然后再编写信息时,你需要做的就只是这些结构工作的处理和变化会小得多。

Although you can make your approach work for deleting a record of a specified student id, it is very inflexible. Searching for a record, or adding a record would require a complete rewrite of your program. If you had code that could read the information into an array of studentinfo structs, and write that info out again, any processing you needed to do would just work on those structs and the changes would be much smaller.

所以,在伪code,你想这样的事情

So, in pseudo code, you want something like this

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文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆