2010-12-05 51 views
0

傢伙你能幫助我與我的代碼..我想用CI有這樣的代碼來編輯一個文本文件中的特定行...編輯在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

123 90,name5,bs

有什麼更好的解決辦法?謝謝:) 反正再次感謝AIX,怎麼把我心中已經從他那裏得到了這樣的想法...不幸的是我不能完成它...希望你能幫助我...

回答

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 
+0

該解決方案几乎是一樣的就是我們的老師告訴我們做的。 :)你真是太棒了先生。保羅。嘗試做一個@ newbieatc。がんばってください – newbie 2010-12-05 10:23:56

1

您不能直接編輯文本文件。無論何時,您需要更改特定內容,您必須首先更改內存中的內容,然後再寫回所有內容。