为什么我不能在 C 中删除并重命名这些文件
我正在开发一个关于 C 语言文件处理的小型项目,这里我有一个房间列表(已排序),我需要一个 ViewList
、borrowRoom< /code> 和
returnRoom
功能。
除 returnRoom
功能外,所有功能均运行良好。我的 retunrnRoom
删除用户输入的房间号的特定行的方法是:
- 制作借用房间列表的精确副本,同时跳过特定行
- 删除旧文件并重命名新文件(由于这是一个小文件,因此此方法是兼容的)。
我设法使用复制的内容和跳过的行创建新文件。但是,remove()
函数不起作用(它返回 -1
),因此 rename()
函数无法执行为出色地。这是我的代码片段:(
我想指出,结构元素的使用来自于我在代码的其他部分中用于存储status
、borrower
(PIC)和desc
(目的))
void returnRoom(){
char enter[5];
system("cls");
printf ("+======+==========+=================+===============================================+\n");
printf ("| Room | Status | PIC | Purpose |\n");
printf ("+======+==========+=================+===============================================+\n");
for (int j = 0; j < i; j++){
if (strcmp (room[j].status, "Borrowed") == 0){
printf ("| %d | %s | %s", room[j].roomnum, room[j].status, room[j].borrower);
for (int k = 0; k < 16 - strlen(room[j].borrower); k++){
room[j].spaceborrow[k] = ' ';
printf ("%c", room[j].spaceborrow[k]);
}
printf ("| %s", room[j].desc);
for (int k = 0; k < 46 - strlen(room[j].desc); k++){
room[j].spacedesc[k] = ' ';
printf ("%c", room[j].spacedesc[k]);
}
printf ("|\n");
}
}
printf ("+======+==========+=================+===============================================+\n");
printf ("-> Return Room\n");
returnroom:
int roomReturn;
int flag = 0;
printf ("Please input the room number [0 to cancel]: ");
scanf ("%d", &roomReturn);
getchar();
if (roomReturn == 0) return;
temp = fopen("usagenew.txt", "w");
for (int j = 0; j < i; j++){
if (j == i-1 && roomReturn != room[j].roomnum){
printf ("Room does not exist\n");
flag = 1;
}
if (roomReturn == room[j].roomnum){
if (strcmp(room[j].status, "Borrowed") == 0){
for (int k = 0; k < i; k++){
if (strcmp(room[k].status, "Borrowed") == 0){
if (roomReturn == room[k].roomnum) continue;
fprintf (temp, "%d#%s#%s\n", room[k].roomnum, room[k].borrower, room[k].desc);
}
}
fclose (roomusage);
fclose (temp);
int deletefile = remove ("usage.txt");
printf ("%d", deletefile);
if (deletefile != 0) printf ("File failed to delete\n");
else {
int renamefile = rename ("usagenew.txt", "usage.txt");
if (renamefile != 0) printf ("Renaming new file failed\n");
}
break;
}
if (strcmp(room[j].status, "Empty ") == 0){
printf ("Room is not borrowed by anyone\n");
flag = 1;
break;
}
}
}
if (flag == 1) goto returnroom;
scanf ("%[^\n]", enter);}
奇怪的是,代码偶尔会正常工作 - 它也会创建新文件如删除旧文件。但是,新文件不会按应有的方式弹出。
我想我的代码中一定有错误,尽管我不确定错误在哪里。 我真的很感激一个答案指出我错在哪里以及如何修复它,而不是仅仅提供一段完整且现成的代码供我使用。谢谢你! :)
更新: 我最终改变了我的方法,并决定覆盖同一个 usage.txt
文件中的所有内容,因此(除了我仍在试图找出的一些错误之外)设法获得了 returnRoom< /code> 启动并运行。再次感谢您:)
I'm working on a small side project about file-handling in C, and here I have a list of rooms (which has been sorted) and I need to have a ViewList
, borrowRoom
, and returnRoom
feature.
All the features are working well, except on the returnRoom
feature. My method of retunrnRoom
in deleting the specific line with the room number that the user inputted is by:
- Making the exact copy of the borrowed rooms list while skipping the specific line
- Deleting the old file and renaming the new one (since this is a small file, this method is compatible).
I managed to create the new file with the copied content and skipped line. However, the remove()
function is not working (it is returning -1
), thus rename()
function can't be done as well. Here is my code snippet:
(I would like to note that the use of struct elements is from the struct
room
that I used in the other part of the code to store the status
, borrower
(PIC), and desc
(Purpose))
void returnRoom(){
char enter[5];
system("cls");
printf ("+======+==========+=================+===============================================+\n");
printf ("| Room | Status | PIC | Purpose |\n");
printf ("+======+==========+=================+===============================================+\n");
for (int j = 0; j < i; j++){
if (strcmp (room[j].status, "Borrowed") == 0){
printf ("| %d | %s | %s", room[j].roomnum, room[j].status, room[j].borrower);
for (int k = 0; k < 16 - strlen(room[j].borrower); k++){
room[j].spaceborrow[k] = ' ';
printf ("%c", room[j].spaceborrow[k]);
}
printf ("| %s", room[j].desc);
for (int k = 0; k < 46 - strlen(room[j].desc); k++){
room[j].spacedesc[k] = ' ';
printf ("%c", room[j].spacedesc[k]);
}
printf ("|\n");
}
}
printf ("+======+==========+=================+===============================================+\n");
printf ("-> Return Room\n");
returnroom:
int roomReturn;
int flag = 0;
printf ("Please input the room number [0 to cancel]: ");
scanf ("%d", &roomReturn);
getchar();
if (roomReturn == 0) return;
temp = fopen("usagenew.txt", "w");
for (int j = 0; j < i; j++){
if (j == i-1 && roomReturn != room[j].roomnum){
printf ("Room does not exist\n");
flag = 1;
}
if (roomReturn == room[j].roomnum){
if (strcmp(room[j].status, "Borrowed") == 0){
for (int k = 0; k < i; k++){
if (strcmp(room[k].status, "Borrowed") == 0){
if (roomReturn == room[k].roomnum) continue;
fprintf (temp, "%d#%s#%s\n", room[k].roomnum, room[k].borrower, room[k].desc);
}
}
fclose (roomusage);
fclose (temp);
int deletefile = remove ("usage.txt");
printf ("%d", deletefile);
if (deletefile != 0) printf ("File failed to delete\n");
else {
int renamefile = rename ("usagenew.txt", "usage.txt");
if (renamefile != 0) printf ("Renaming new file failed\n");
}
break;
}
if (strcmp(room[j].status, "Empty ") == 0){
printf ("Room is not borrowed by anyone\n");
flag = 1;
break;
}
}
}
if (flag == 1) goto returnroom;
scanf ("%[^\n]", enter);}
Weirdly enough, the code occasionally works correctly- it creates the new file as well as delete the old file. However, the new file won't pop out as it should be.
I guess there must be an error in my code, though I'm not sure where.
Would really appreciate an answer pointing to where I am wrong and how to fix it, rather than just giving a complete and ready piece of code for me to use. Thank you! :)
UPDATE:
I finally changed my method and decided to just overwrite everything in the same usage.txt
file, thus (aside of some bugs that I am still trying to figure out) managed to get the returnRoom
up and running. Thank you again :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论