C programming — how can I put text file content into my Array?
I'm trying to write a program which can delete a record in file io. The file content is set by default. Now I am trying put my text file content into Array, but there is some trouble happening....
Firstly,it is my default data file content:
1001
eric
1
human
10
70.00
eric
home
arrive
1002
She
1
human
10
50.00
she
home
arrive
1003
She_eric
2
human
10
120.00
eric
home
arrive
Here is my code:(i am using fscanf putting my text file data into array,you can see it in the middle about open file for read)
#include <stdio.h>
#include <stdlib.h>
struct record{
char recordnum [40];
char itemrecord [40];
char quantity [40];
char weight [40];
char itemname [40];
char catagory [40];
char recipient [40];
char final_destination [40];
char status [40];
};
int main()
{
FILE *fileptr1, *fileptr2, fileptr3;
char filename[40]="record.txt";
char save;
int delete_num, temp ;
char reply;
#define MAX 9
struct record Arr[MAX];
printf("Enter file name: ");
scanf("%s", filename);
do{
temp =1; //reset temp and loop can work below time
//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);
int i =0;
for (i=0; i<3 ; i++){
fscanf(fileptr1,"%s %s %s %s %s %s %s %s %s", Arr[i].recordnum,
Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity,
Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status
);
}
}
//rewind
rewind(fileptr1);
printf(" nn Enter record number to be deleted <type 0 = not delete
anything>:");
fflush(stdin);
scanf("%d", &delete_num);
//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = getc(fileptr1);
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
int i ;
for (i=0;i<3;i++){
if (temp != delete_num)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}}
}
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);
}
fclose(fileptr1);
fflush(stdin);
printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);
}while(reply=='y' || reply=='Y');
return 0;
}
Sorry for my long code......
Result:
Enter file name:record.txt
10
Enter record number to be deleted <type 0 = not delete anything>:1001
The contents of file after being changed are as follows:
0000000111
eeerrriiiccc
111
hhhuuummmaaannn
111000
777000...000
eeerrriiiccc
hhhooommmeee
aaarrrrrriiivvveee
and below result same......interesting....
It is not my expected result... i know the word appear three time because of my for loop. However I want my for loop run 3 time , first time include 9record and below..
c arrays struct file-io
add a comment |
I'm trying to write a program which can delete a record in file io. The file content is set by default. Now I am trying put my text file content into Array, but there is some trouble happening....
Firstly,it is my default data file content:
1001
eric
1
human
10
70.00
eric
home
arrive
1002
She
1
human
10
50.00
she
home
arrive
1003
She_eric
2
human
10
120.00
eric
home
arrive
Here is my code:(i am using fscanf putting my text file data into array,you can see it in the middle about open file for read)
#include <stdio.h>
#include <stdlib.h>
struct record{
char recordnum [40];
char itemrecord [40];
char quantity [40];
char weight [40];
char itemname [40];
char catagory [40];
char recipient [40];
char final_destination [40];
char status [40];
};
int main()
{
FILE *fileptr1, *fileptr2, fileptr3;
char filename[40]="record.txt";
char save;
int delete_num, temp ;
char reply;
#define MAX 9
struct record Arr[MAX];
printf("Enter file name: ");
scanf("%s", filename);
do{
temp =1; //reset temp and loop can work below time
//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);
int i =0;
for (i=0; i<3 ; i++){
fscanf(fileptr1,"%s %s %s %s %s %s %s %s %s", Arr[i].recordnum,
Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity,
Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status
);
}
}
//rewind
rewind(fileptr1);
printf(" nn Enter record number to be deleted <type 0 = not delete
anything>:");
fflush(stdin);
scanf("%d", &delete_num);
//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = getc(fileptr1);
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
int i ;
for (i=0;i<3;i++){
if (temp != delete_num)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}}
}
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);
}
fclose(fileptr1);
fflush(stdin);
printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);
}while(reply=='y' || reply=='Y');
return 0;
}
Sorry for my long code......
Result:
Enter file name:record.txt
10
Enter record number to be deleted <type 0 = not delete anything>:1001
The contents of file after being changed are as follows:
0000000111
eeerrriiiccc
111
hhhuuummmaaannn
111000
777000...000
eeerrriiiccc
hhhooommmeee
aaarrrrrriiivvveee
and below result same......interesting....
It is not my expected result... i know the word appear three time because of my for loop. However I want my for loop run 3 time , first time include 9record and below..
c arrays struct file-io
fflush(stdin);
is UB, dont do it.
– Sourav Ghosh
Nov 23 '18 at 14:21
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you whyfopen
failed.if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25
add a comment |
I'm trying to write a program which can delete a record in file io. The file content is set by default. Now I am trying put my text file content into Array, but there is some trouble happening....
Firstly,it is my default data file content:
1001
eric
1
human
10
70.00
eric
home
arrive
1002
She
1
human
10
50.00
she
home
arrive
1003
She_eric
2
human
10
120.00
eric
home
arrive
Here is my code:(i am using fscanf putting my text file data into array,you can see it in the middle about open file for read)
#include <stdio.h>
#include <stdlib.h>
struct record{
char recordnum [40];
char itemrecord [40];
char quantity [40];
char weight [40];
char itemname [40];
char catagory [40];
char recipient [40];
char final_destination [40];
char status [40];
};
int main()
{
FILE *fileptr1, *fileptr2, fileptr3;
char filename[40]="record.txt";
char save;
int delete_num, temp ;
char reply;
#define MAX 9
struct record Arr[MAX];
printf("Enter file name: ");
scanf("%s", filename);
do{
temp =1; //reset temp and loop can work below time
//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);
int i =0;
for (i=0; i<3 ; i++){
fscanf(fileptr1,"%s %s %s %s %s %s %s %s %s", Arr[i].recordnum,
Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity,
Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status
);
}
}
//rewind
rewind(fileptr1);
printf(" nn Enter record number to be deleted <type 0 = not delete
anything>:");
fflush(stdin);
scanf("%d", &delete_num);
//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = getc(fileptr1);
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
int i ;
for (i=0;i<3;i++){
if (temp != delete_num)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}}
}
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);
}
fclose(fileptr1);
fflush(stdin);
printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);
}while(reply=='y' || reply=='Y');
return 0;
}
Sorry for my long code......
Result:
Enter file name:record.txt
10
Enter record number to be deleted <type 0 = not delete anything>:1001
The contents of file after being changed are as follows:
0000000111
eeerrriiiccc
111
hhhuuummmaaannn
111000
777000...000
eeerrriiiccc
hhhooommmeee
aaarrrrrriiivvveee
and below result same......interesting....
It is not my expected result... i know the word appear three time because of my for loop. However I want my for loop run 3 time , first time include 9record and below..
c arrays struct file-io
I'm trying to write a program which can delete a record in file io. The file content is set by default. Now I am trying put my text file content into Array, but there is some trouble happening....
Firstly,it is my default data file content:
1001
eric
1
human
10
70.00
eric
home
arrive
1002
She
1
human
10
50.00
she
home
arrive
1003
She_eric
2
human
10
120.00
eric
home
arrive
Here is my code:(i am using fscanf putting my text file data into array,you can see it in the middle about open file for read)
#include <stdio.h>
#include <stdlib.h>
struct record{
char recordnum [40];
char itemrecord [40];
char quantity [40];
char weight [40];
char itemname [40];
char catagory [40];
char recipient [40];
char final_destination [40];
char status [40];
};
int main()
{
FILE *fileptr1, *fileptr2, fileptr3;
char filename[40]="record.txt";
char save;
int delete_num, temp ;
char reply;
#define MAX 9
struct record Arr[MAX];
printf("Enter file name: ");
scanf("%s", filename);
do{
temp =1; //reset temp and loop can work below time
//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);
int i =0;
for (i=0; i<3 ; i++){
fscanf(fileptr1,"%s %s %s %s %s %s %s %s %s", Arr[i].recordnum,
Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity,
Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status
);
}
}
//rewind
rewind(fileptr1);
printf(" nn Enter record number to be deleted <type 0 = not delete
anything>:");
fflush(stdin);
scanf("%d", &delete_num);
//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = getc(fileptr1);
while (save != EOF)
{
save = getc(fileptr1);
//except the line to be deleted
int i ;
for (i=0;i<3;i++){
if (temp != delete_num)
{
//copy all lines in file replica.c
putc(save, fileptr2);
}}
}
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);
}
fclose(fileptr1);
fflush(stdin);
printf("nn Delete anther item?<y/n>: ");
scanf("%c",&reply);
}while(reply=='y' || reply=='Y');
return 0;
}
Sorry for my long code......
Result:
Enter file name:record.txt
10
Enter record number to be deleted <type 0 = not delete anything>:1001
The contents of file after being changed are as follows:
0000000111
eeerrriiiccc
111
hhhuuummmaaannn
111000
777000...000
eeerrriiiccc
hhhooommmeee
aaarrrrrriiivvveee
and below result same......interesting....
It is not my expected result... i know the word appear three time because of my for loop. However I want my for loop run 3 time , first time include 9record and below..
c arrays struct file-io
c arrays struct file-io
edited Nov 24 '18 at 3:52
Hang Wui
asked Nov 23 '18 at 14:19
Hang WuiHang Wui
195
195
fflush(stdin);
is UB, dont do it.
– Sourav Ghosh
Nov 23 '18 at 14:21
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you whyfopen
failed.if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25
add a comment |
fflush(stdin);
is UB, dont do it.
– Sourav Ghosh
Nov 23 '18 at 14:21
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you whyfopen
failed.if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25
fflush(stdin);
is UB, dont do it.– Sourav Ghosh
Nov 23 '18 at 14:21
fflush(stdin);
is UB, dont do it.– Sourav Ghosh
Nov 23 '18 at 14:21
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you why fopen
failed. if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you why fopen
failed. if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25
add a comment |
1 Answer
1
active
oldest
votes
You're using getc
to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc
calls and using the return value of fscanf
to control your loop:
while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
if (++i >= MAX)
break;
}
Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr
and leaving out the record you want to delete.
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since yourrecordnum
is a string rather than an integer, you'll need to usestrcmp
rather than!=
– Chris Dodd
Nov 26 '18 at 20:32
you meanif(strcmp(Arr[i].recordnum,delete_num)==0)
?
– Hang Wui
Nov 27 '18 at 3:35
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448384%2fc-programming-how-can-i-put-text-file-content-into-my-array%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
You're using getc
to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc
calls and using the return value of fscanf
to control your loop:
while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
if (++i >= MAX)
break;
}
Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr
and leaving out the record you want to delete.
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since yourrecordnum
is a string rather than an integer, you'll need to usestrcmp
rather than!=
– Chris Dodd
Nov 26 '18 at 20:32
you meanif(strcmp(Arr[i].recordnum,delete_num)==0)
?
– Hang Wui
Nov 27 '18 at 3:35
add a comment |
You're using getc
to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc
calls and using the return value of fscanf
to control your loop:
while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
if (++i >= MAX)
break;
}
Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr
and leaving out the record you want to delete.
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since yourrecordnum
is a string rather than an integer, you'll need to usestrcmp
rather than!=
– Chris Dodd
Nov 26 '18 at 20:32
you meanif(strcmp(Arr[i].recordnum,delete_num)==0)
?
– Hang Wui
Nov 27 '18 at 3:35
add a comment |
You're using getc
to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc
calls and using the return value of fscanf
to control your loop:
while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
if (++i >= MAX)
break;
}
Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr
and leaving out the record you want to delete.
You're using getc
to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc
calls and using the return value of fscanf
to control your loop:
while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
if (++i >= MAX)
break;
}
Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr
and leaving out the record you want to delete.
answered Nov 24 '18 at 18:59
Chris DoddChris Dodd
81.2k680160
81.2k680160
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since yourrecordnum
is a string rather than an integer, you'll need to usestrcmp
rather than!=
– Chris Dodd
Nov 26 '18 at 20:32
you meanif(strcmp(Arr[i].recordnum,delete_num)==0)
?
– Hang Wui
Nov 27 '18 at 3:35
add a comment |
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since yourrecordnum
is a string rather than an integer, you'll need to usestrcmp
rather than!=
– Chris Dodd
Nov 26 '18 at 20:32
you meanif(strcmp(Arr[i].recordnum,delete_num)==0)
?
– Hang Wui
Nov 27 '18 at 3:35
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
– Hang Wui
Nov 26 '18 at 13:33
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`– Hang Wui
Nov 26 '18 at 13:37
for(i=0;i<total;i++)
then..if (Arr[i].recordnum != delete_num){
fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`– Hang Wui
Nov 26 '18 at 13:37
@HangWui: exactly, though since your
recordnum
is a string rather than an integer, you'll need to use strcmp
rather than !=
– Chris Dodd
Nov 26 '18 at 20:32
@HangWui: exactly, though since your
recordnum
is a string rather than an integer, you'll need to use strcmp
rather than !=
– Chris Dodd
Nov 26 '18 at 20:32
you mean
if(strcmp(Arr[i].recordnum,delete_num)==0)
?– Hang Wui
Nov 27 '18 at 3:35
you mean
if(strcmp(Arr[i].recordnum,delete_num)==0)
?– Hang Wui
Nov 27 '18 at 3:35
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448384%2fc-programming-how-can-i-put-text-file-content-into-my-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
fflush(stdin);
is UB, dont do it.– Sourav Ghosh
Nov 23 '18 at 14:21
@SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :))
– Hang Wui
Nov 23 '18 at 14:37
file not exist
is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you whyfopen
failed.if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);}
– William Pursell
Nov 24 '18 at 4:25