C language ~ data file by using delete function modify text file content (File IO)












1















I'm the novice and still learning C language. Currently, I'm having a problem with my code. The task is delete something in the file IO, by default there is some record in my txt file. The task is asking the user to input the which lines he/she would like to delete in txt file. However, the problem occurs:



1 )the c program still work in the first time, but the second, third time and below cannot work.(that mean just can delete lines in the first time)



I think it is a buffer problem, so I add many fflush codes, but still cant solve.



2) I would like to change my program into using a keyword to find the record and delete.User type the recordnum can delete whole item record. However, I have not idea how it works.



if someone can help me , I am very grateful and happy.
1)code is below :



#include<stdio.h> #include<stdlib.h>      
int main(){
FILE *fileptr1, *fileptr2, *fileptr3;
char filename[40]="itemrecord.txt";
char save;
int delete_line, temp = 1;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{


//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}

//rewind
rewind(fileptr1);

fflush(stdout);
printf(" nn Enter line number of the line to be deleted <type 0 = not
delete anything>:");
fflush(stdin);
scanf("%d", &delete_line);


//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = 'a';
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
if (temp != delete_line)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}
if (save == 'n')
{
temp++;
}
}

fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("copy.c", filename);
fflush(stdout);
printf("nThe contents of file after being changed are as follows:n");

fileptr1 = fopen(filename, "r");
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}
fflush(stdout);
fflush(stdin);
fclose(fileptr1);

printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);

}while(reply=='y' || reply=='Y');
return 0;
}


Result :



Enter file name:itemrecord
mary 1 123
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:y
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:n

//second time doesn't work.//


I'm sorry for the long code and long question, but I'm so confusing...



2)if my record into



Mary
1
123

sam
2
124

bob
3
125


SO how I can type 'Mary' delele whole record item? something like that...



sam  
2
124

bob
3
125









share|improve this question


















  • 1





    Note that getc returns an int. That's actually kind of important for the EOF check.

    – Some programmer dude
    Nov 22 '18 at 12:24






  • 1





    Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

    – Some programmer dude
    Nov 22 '18 at 12:25






  • 1





    You never reset temp. (Which is poorly named, btw).

    – Johnny Mopp
    Nov 22 '18 at 12:30











  • thanks a lot!! I reset the temp and solve my first question

    – Hang Wui
    Nov 22 '18 at 13:14













  • So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

    – Hang Wui
    Nov 22 '18 at 13:17
















1















I'm the novice and still learning C language. Currently, I'm having a problem with my code. The task is delete something in the file IO, by default there is some record in my txt file. The task is asking the user to input the which lines he/she would like to delete in txt file. However, the problem occurs:



1 )the c program still work in the first time, but the second, third time and below cannot work.(that mean just can delete lines in the first time)



I think it is a buffer problem, so I add many fflush codes, but still cant solve.



2) I would like to change my program into using a keyword to find the record and delete.User type the recordnum can delete whole item record. However, I have not idea how it works.



if someone can help me , I am very grateful and happy.
1)code is below :



#include<stdio.h> #include<stdlib.h>      
int main(){
FILE *fileptr1, *fileptr2, *fileptr3;
char filename[40]="itemrecord.txt";
char save;
int delete_line, temp = 1;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{


//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}

//rewind
rewind(fileptr1);

fflush(stdout);
printf(" nn Enter line number of the line to be deleted <type 0 = not
delete anything>:");
fflush(stdin);
scanf("%d", &delete_line);


//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = 'a';
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
if (temp != delete_line)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}
if (save == 'n')
{
temp++;
}
}

fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("copy.c", filename);
fflush(stdout);
printf("nThe contents of file after being changed are as follows:n");

fileptr1 = fopen(filename, "r");
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}
fflush(stdout);
fflush(stdin);
fclose(fileptr1);

printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);

}while(reply=='y' || reply=='Y');
return 0;
}


Result :



Enter file name:itemrecord
mary 1 123
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:y
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:n

//second time doesn't work.//


I'm sorry for the long code and long question, but I'm so confusing...



2)if my record into



Mary
1
123

sam
2
124

bob
3
125


SO how I can type 'Mary' delele whole record item? something like that...



sam  
2
124

bob
3
125









share|improve this question


















  • 1





    Note that getc returns an int. That's actually kind of important for the EOF check.

    – Some programmer dude
    Nov 22 '18 at 12:24






  • 1





    Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

    – Some programmer dude
    Nov 22 '18 at 12:25






  • 1





    You never reset temp. (Which is poorly named, btw).

    – Johnny Mopp
    Nov 22 '18 at 12:30











  • thanks a lot!! I reset the temp and solve my first question

    – Hang Wui
    Nov 22 '18 at 13:14













  • So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

    – Hang Wui
    Nov 22 '18 at 13:17














1












1








1


1






I'm the novice and still learning C language. Currently, I'm having a problem with my code. The task is delete something in the file IO, by default there is some record in my txt file. The task is asking the user to input the which lines he/she would like to delete in txt file. However, the problem occurs:



1 )the c program still work in the first time, but the second, third time and below cannot work.(that mean just can delete lines in the first time)



I think it is a buffer problem, so I add many fflush codes, but still cant solve.



2) I would like to change my program into using a keyword to find the record and delete.User type the recordnum can delete whole item record. However, I have not idea how it works.



if someone can help me , I am very grateful and happy.
1)code is below :



#include<stdio.h> #include<stdlib.h>      
int main(){
FILE *fileptr1, *fileptr2, *fileptr3;
char filename[40]="itemrecord.txt";
char save;
int delete_line, temp = 1;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{


//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}

//rewind
rewind(fileptr1);

fflush(stdout);
printf(" nn Enter line number of the line to be deleted <type 0 = not
delete anything>:");
fflush(stdin);
scanf("%d", &delete_line);


//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = 'a';
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
if (temp != delete_line)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}
if (save == 'n')
{
temp++;
}
}

fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("copy.c", filename);
fflush(stdout);
printf("nThe contents of file after being changed are as follows:n");

fileptr1 = fopen(filename, "r");
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}
fflush(stdout);
fflush(stdin);
fclose(fileptr1);

printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);

}while(reply=='y' || reply=='Y');
return 0;
}


Result :



Enter file name:itemrecord
mary 1 123
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:y
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:n

//second time doesn't work.//


I'm sorry for the long code and long question, but I'm so confusing...



2)if my record into



Mary
1
123

sam
2
124

bob
3
125


SO how I can type 'Mary' delele whole record item? something like that...



sam  
2
124

bob
3
125









share|improve this question














I'm the novice and still learning C language. Currently, I'm having a problem with my code. The task is delete something in the file IO, by default there is some record in my txt file. The task is asking the user to input the which lines he/she would like to delete in txt file. However, the problem occurs:



1 )the c program still work in the first time, but the second, third time and below cannot work.(that mean just can delete lines in the first time)



I think it is a buffer problem, so I add many fflush codes, but still cant solve.



2) I would like to change my program into using a keyword to find the record and delete.User type the recordnum can delete whole item record. However, I have not idea how it works.



if someone can help me , I am very grateful and happy.
1)code is below :



#include<stdio.h> #include<stdlib.h>      
int main(){
FILE *fileptr1, *fileptr2, *fileptr3;
char filename[40]="itemrecord.txt";
char save;
int delete_line, temp = 1;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{


//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}

//rewind
rewind(fileptr1);

fflush(stdout);
printf(" nn Enter line number of the line to be deleted <type 0 = not
delete anything>:");
fflush(stdin);
scanf("%d", &delete_line);


//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = 'a';
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
if (temp != delete_line)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}
if (save == 'n')
{
temp++;
}
}

fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("copy.c", filename);
fflush(stdout);
printf("nThe contents of file after being changed are as follows:n");

fileptr1 = fopen(filename, "r");
save = getc(fileptr1);
while (save != EOF)
{
printf("%c", save);
save = getc(fileptr1);
}
fflush(stdout);
fflush(stdin);
fclose(fileptr1);

printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);

}while(reply=='y' || reply=='Y');
return 0;
}


Result :



Enter file name:itemrecord
mary 1 123
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:y
sam 2 124
bob 3 125

Enter line number of the line to be deleted<type 0=not delete anything>:1

The contents of file after being changed are as follows:
sam 2 124
bob 3 125

Delete other record?<y/n>:n

//second time doesn't work.//


I'm sorry for the long code and long question, but I'm so confusing...



2)if my record into



Mary
1
123

sam
2
124

bob
3
125


SO how I can type 'Mary' delele whole record item? something like that...



sam  
2
124

bob
3
125






c file file-io text-files






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:20









Hang WuiHang Wui

195




195








  • 1





    Note that getc returns an int. That's actually kind of important for the EOF check.

    – Some programmer dude
    Nov 22 '18 at 12:24






  • 1





    Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

    – Some programmer dude
    Nov 22 '18 at 12:25






  • 1





    You never reset temp. (Which is poorly named, btw).

    – Johnny Mopp
    Nov 22 '18 at 12:30











  • thanks a lot!! I reset the temp and solve my first question

    – Hang Wui
    Nov 22 '18 at 13:14













  • So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

    – Hang Wui
    Nov 22 '18 at 13:17














  • 1





    Note that getc returns an int. That's actually kind of important for the EOF check.

    – Some programmer dude
    Nov 22 '18 at 12:24






  • 1





    Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

    – Some programmer dude
    Nov 22 '18 at 12:25






  • 1





    You never reset temp. (Which is poorly named, btw).

    – Johnny Mopp
    Nov 22 '18 at 12:30











  • thanks a lot!! I reset the temp and solve my first question

    – Hang Wui
    Nov 22 '18 at 13:14













  • So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

    – Hang Wui
    Nov 22 '18 at 13:17








1




1





Note that getc returns an int. That's actually kind of important for the EOF check.

– Some programmer dude
Nov 22 '18 at 12:24





Note that getc returns an int. That's actually kind of important for the EOF check.

– Some programmer dude
Nov 22 '18 at 12:24




1




1





Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

– Some programmer dude
Nov 22 '18 at 12:25





Also, calling fflush with an input-only stream (like stdin) is explicitly mentioned in the C specification as undefined behavior. Some compilers implement it as an extension of the library, but you should never really do that.

– Some programmer dude
Nov 22 '18 at 12:25




1




1





You never reset temp. (Which is poorly named, btw).

– Johnny Mopp
Nov 22 '18 at 12:30





You never reset temp. (Which is poorly named, btw).

– Johnny Mopp
Nov 22 '18 at 12:30













thanks a lot!! I reset the temp and solve my first question

– Hang Wui
Nov 22 '18 at 13:14







thanks a lot!! I reset the temp and solve my first question

– Hang Wui
Nov 22 '18 at 13:14















So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

– Hang Wui
Nov 22 '18 at 13:17





So i should truncate the file to slove my second question? (because I'm the novice and sorry for my stupid brain

– Hang Wui
Nov 22 '18 at 13:17












1 Answer
1






active

oldest

votes


















0














Try this



int delete_line, temp;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{
temp = 1;
....


I hope this helps.






share|improve this answer
























  • I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

    – Hang Wui
    Nov 22 '18 at 13:28











  • The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

    – 42LeapsOfFaith
    Nov 22 '18 at 13:40











  • yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

    – Hang Wui
    Nov 22 '18 at 13:46













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430909%2fc-language-data-file-by-using-delete-function-modify-text-file-content-file-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try this



int delete_line, temp;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{
temp = 1;
....


I hope this helps.






share|improve this answer
























  • I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

    – Hang Wui
    Nov 22 '18 at 13:28











  • The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

    – 42LeapsOfFaith
    Nov 22 '18 at 13:40











  • yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

    – Hang Wui
    Nov 22 '18 at 13:46


















0














Try this



int delete_line, temp;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{
temp = 1;
....


I hope this helps.






share|improve this answer
























  • I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

    – Hang Wui
    Nov 22 '18 at 13:28











  • The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

    – 42LeapsOfFaith
    Nov 22 '18 at 13:40











  • yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

    – Hang Wui
    Nov 22 '18 at 13:46
















0












0








0







Try this



int delete_line, temp;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{
temp = 1;
....


I hope this helps.






share|improve this answer













Try this



int delete_line, temp;
char reply;

printf("Enter file name: ");
scanf("%s", filename);

do{
temp = 1;
....


I hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 13:17









42LeapsOfFaith42LeapsOfFaith

515




515













  • I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

    – Hang Wui
    Nov 22 '18 at 13:28











  • The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

    – 42LeapsOfFaith
    Nov 22 '18 at 13:40











  • yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

    – Hang Wui
    Nov 22 '18 at 13:46





















  • I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

    – Hang Wui
    Nov 22 '18 at 13:28











  • The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

    – 42LeapsOfFaith
    Nov 22 '18 at 13:40











  • yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

    – Hang Wui
    Nov 22 '18 at 13:46



















I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

– Hang Wui
Nov 22 '18 at 13:28





I would like to know the concept and how it work , because it is not similar with delete_line function. if delete the record item(Mary record) line-by-line is not userfriendly. grateful for your reply:)

– Hang Wui
Nov 22 '18 at 13:28













The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

– 42LeapsOfFaith
Nov 22 '18 at 13:40





The problem with your code is that 'temp' needs re-initialised to '1' (temp = 1;) at the top of the do loop - as shown in the snippet of code above.

– 42LeapsOfFaith
Nov 22 '18 at 13:40













yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

– Hang Wui
Nov 22 '18 at 13:46







yup, it is helpful and I solved. but I'm still confusing about how to delete designation entire record (like my question second part mentioned that

– Hang Wui
Nov 22 '18 at 13:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430909%2fc-language-data-file-by-using-delete-function-modify-text-file-content-file-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga