C -— How can i put ALL file IO record in my array successfully? thankyou












0















I have a text file with default data that I created. Now I want to put them into my Array. But not success.. I dont know why only the first record of the struct data can store in my array.



I want first 9 records stored in Arr[0], secord 9 records stored in Arr[1]
, and third 9 record stored in Arr[2] But the result is only recordNum can store in Arr[ I ].



I am very grateful and proud of you if you can help me because i try many time can not success



Here is my data file content:



1001 - bric - 1 - human - 10 - 70.00  - Eric - home - arrive

1002 - She - 1 - human - 10 - 50.00 - she - home - arrive

1003 - She - 2 - human - 10 - 120.00 - Eric - home - arrived


Here is my Code:



#include <stdio.h>
#include <stdlib.h>
#include<string.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]="data.txt";
int save;
char delete_num ;
char reply;
int n=1 , i=0;
#define MAX 9
struct record Arr[MAX];

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

//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}

while(n>0){
n=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 );
i++;
}
n=i;
for(i=0; i<n-1 ;i++){
printf("%s n" ,Arr[i]);
}
fclose(fileptr1);
return 0;
}


Result:



Enter file name: data.txt
1001
1002
1003









share|improve this question




















  • 1





    Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

    – chux
    Nov 24 '18 at 6:55













  • But no compiler warning and error here. @chux

    – Hang Wui
    Nov 24 '18 at 7:00











  • Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

    – chux
    Nov 24 '18 at 7:02








  • 1





    You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

    – alamit
    Nov 24 '18 at 7:14






  • 1





    OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

    – Hang Wui
    Nov 24 '18 at 7:33
















0















I have a text file with default data that I created. Now I want to put them into my Array. But not success.. I dont know why only the first record of the struct data can store in my array.



I want first 9 records stored in Arr[0], secord 9 records stored in Arr[1]
, and third 9 record stored in Arr[2] But the result is only recordNum can store in Arr[ I ].



I am very grateful and proud of you if you can help me because i try many time can not success



Here is my data file content:



1001 - bric - 1 - human - 10 - 70.00  - Eric - home - arrive

1002 - She - 1 - human - 10 - 50.00 - she - home - arrive

1003 - She - 2 - human - 10 - 120.00 - Eric - home - arrived


Here is my Code:



#include <stdio.h>
#include <stdlib.h>
#include<string.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]="data.txt";
int save;
char delete_num ;
char reply;
int n=1 , i=0;
#define MAX 9
struct record Arr[MAX];

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

//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}

while(n>0){
n=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 );
i++;
}
n=i;
for(i=0; i<n-1 ;i++){
printf("%s n" ,Arr[i]);
}
fclose(fileptr1);
return 0;
}


Result:



Enter file name: data.txt
1001
1002
1003









share|improve this question




















  • 1





    Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

    – chux
    Nov 24 '18 at 6:55













  • But no compiler warning and error here. @chux

    – Hang Wui
    Nov 24 '18 at 7:00











  • Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

    – chux
    Nov 24 '18 at 7:02








  • 1





    You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

    – alamit
    Nov 24 '18 at 7:14






  • 1





    OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

    – Hang Wui
    Nov 24 '18 at 7:33














0












0








0








I have a text file with default data that I created. Now I want to put them into my Array. But not success.. I dont know why only the first record of the struct data can store in my array.



I want first 9 records stored in Arr[0], secord 9 records stored in Arr[1]
, and third 9 record stored in Arr[2] But the result is only recordNum can store in Arr[ I ].



I am very grateful and proud of you if you can help me because i try many time can not success



Here is my data file content:



1001 - bric - 1 - human - 10 - 70.00  - Eric - home - arrive

1002 - She - 1 - human - 10 - 50.00 - she - home - arrive

1003 - She - 2 - human - 10 - 120.00 - Eric - home - arrived


Here is my Code:



#include <stdio.h>
#include <stdlib.h>
#include<string.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]="data.txt";
int save;
char delete_num ;
char reply;
int n=1 , i=0;
#define MAX 9
struct record Arr[MAX];

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

//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}

while(n>0){
n=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 );
i++;
}
n=i;
for(i=0; i<n-1 ;i++){
printf("%s n" ,Arr[i]);
}
fclose(fileptr1);
return 0;
}


Result:



Enter file name: data.txt
1001
1002
1003









share|improve this question
















I have a text file with default data that I created. Now I want to put them into my Array. But not success.. I dont know why only the first record of the struct data can store in my array.



I want first 9 records stored in Arr[0], secord 9 records stored in Arr[1]
, and third 9 record stored in Arr[2] But the result is only recordNum can store in Arr[ I ].



I am very grateful and proud of you if you can help me because i try many time can not success



Here is my data file content:



1001 - bric - 1 - human - 10 - 70.00  - Eric - home - arrive

1002 - She - 1 - human - 10 - 50.00 - she - home - arrive

1003 - She - 2 - human - 10 - 120.00 - Eric - home - arrived


Here is my Code:



#include <stdio.h>
#include <stdlib.h>
#include<string.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]="data.txt";
int save;
char delete_num ;
char reply;
int n=1 , i=0;
#define MAX 9
struct record Arr[MAX];

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

//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist");
exit(1);
}

while(n>0){
n=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 );
i++;
}
n=i;
for(i=0; i<n-1 ;i++){
printf("%s n" ,Arr[i]);
}
fclose(fileptr1);
return 0;
}


Result:



Enter file name: data.txt
1001
1002
1003






c arrays struct file-io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 12:31









kit

1,1063816




1,1063816










asked Nov 24 '18 at 6:47









Hang WuiHang Wui

195




195








  • 1





    Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

    – chux
    Nov 24 '18 at 6:55













  • But no compiler warning and error here. @chux

    – Hang Wui
    Nov 24 '18 at 7:00











  • Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

    – chux
    Nov 24 '18 at 7:02








  • 1





    You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

    – alamit
    Nov 24 '18 at 7:14






  • 1





    OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

    – Hang Wui
    Nov 24 '18 at 7:33














  • 1





    Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

    – chux
    Nov 24 '18 at 6:55













  • But no compiler warning and error here. @chux

    – Hang Wui
    Nov 24 '18 at 7:00











  • Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

    – chux
    Nov 24 '18 at 7:02








  • 1





    You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

    – alamit
    Nov 24 '18 at 7:14






  • 1





    OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

    – Hang Wui
    Nov 24 '18 at 7:33








1




1





Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

– chux
Nov 24 '18 at 6:55







Review printf("%s n" ,Arr[i]); Is Arr[i] a pointer to a string? Tip: Enable all compiler warnings.

– chux
Nov 24 '18 at 6:55















But no compiler warning and error here. @chux

– Hang Wui
Nov 24 '18 at 7:00





But no compiler warning and error here. @chux

– Hang Wui
Nov 24 '18 at 7:00













Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

– chux
Nov 24 '18 at 7:02







Do not simply use default settings. Enable ALL warnings. What compiler are you using? And again: Is Arr[i] a pointer to a string?

– chux
Nov 24 '18 at 7:02






1




1





You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

– alamit
Nov 24 '18 at 7:14





You should print each field of your struct one by one, printing a struct using %s format is undefined behaviour.

– alamit
Nov 24 '18 at 7:14




1




1





OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

– Hang Wui
Nov 24 '18 at 7:33





OK, I get this warning: [Warning] format '%s' expects argument of type 'char *', but argument 2 has type 'struct record' [-Wformat=]

– Hang Wui
Nov 24 '18 at 7:33












1 Answer
1






active

oldest

votes


















1














The printf function's "%s" format is for strings.
Strings in c are represented as one dimensional arrays (or a pointer to a portion of memory) containing characters and ended by a '' character.



Here you are trying to print a variable of type struct record with this format, which is undefined behaviour, in your example it happens that the bytes pointed by Arr + i(which is the address of the value Arr[i] of type struct record) are those of Arr[i].recordnum, therefore because you told printf to treat those bytes as string using the "%s" format, the characters are printed until a '' character is met. However, this is undefined behaviour because depending on architectures, structs might have padding bytes at the beginning instead of the bytes of their first field.



There is no printf format to print your custom struct, so you need to manually print each of your struct fields using their respective formats, in your case %s.



printf("%s, ..., %sn", Arr[i].recordnum, ..., Arr[i].status);





share|improve this answer
























  • that right. so my content is stored in my array successfully?

    – Hang Wui
    Nov 24 '18 at 7:48











  • Yes @HangWui, but you should check it out by yourself to be sure.

    – alamit
    Nov 24 '18 at 7:50











  • Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

    – Hang Wui
    Nov 24 '18 at 7:51











  • i have a concern about is there 9 record store in one i ?

    – Hang Wui
    Nov 24 '18 at 8:00











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%2f53455860%2fc-how-can-i-put-all-file-io-record-in-my-array-successfully-thankyou%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









1














The printf function's "%s" format is for strings.
Strings in c are represented as one dimensional arrays (or a pointer to a portion of memory) containing characters and ended by a '' character.



Here you are trying to print a variable of type struct record with this format, which is undefined behaviour, in your example it happens that the bytes pointed by Arr + i(which is the address of the value Arr[i] of type struct record) are those of Arr[i].recordnum, therefore because you told printf to treat those bytes as string using the "%s" format, the characters are printed until a '' character is met. However, this is undefined behaviour because depending on architectures, structs might have padding bytes at the beginning instead of the bytes of their first field.



There is no printf format to print your custom struct, so you need to manually print each of your struct fields using their respective formats, in your case %s.



printf("%s, ..., %sn", Arr[i].recordnum, ..., Arr[i].status);





share|improve this answer
























  • that right. so my content is stored in my array successfully?

    – Hang Wui
    Nov 24 '18 at 7:48











  • Yes @HangWui, but you should check it out by yourself to be sure.

    – alamit
    Nov 24 '18 at 7:50











  • Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

    – Hang Wui
    Nov 24 '18 at 7:51











  • i have a concern about is there 9 record store in one i ?

    – Hang Wui
    Nov 24 '18 at 8:00
















1














The printf function's "%s" format is for strings.
Strings in c are represented as one dimensional arrays (or a pointer to a portion of memory) containing characters and ended by a '' character.



Here you are trying to print a variable of type struct record with this format, which is undefined behaviour, in your example it happens that the bytes pointed by Arr + i(which is the address of the value Arr[i] of type struct record) are those of Arr[i].recordnum, therefore because you told printf to treat those bytes as string using the "%s" format, the characters are printed until a '' character is met. However, this is undefined behaviour because depending on architectures, structs might have padding bytes at the beginning instead of the bytes of their first field.



There is no printf format to print your custom struct, so you need to manually print each of your struct fields using their respective formats, in your case %s.



printf("%s, ..., %sn", Arr[i].recordnum, ..., Arr[i].status);





share|improve this answer
























  • that right. so my content is stored in my array successfully?

    – Hang Wui
    Nov 24 '18 at 7:48











  • Yes @HangWui, but you should check it out by yourself to be sure.

    – alamit
    Nov 24 '18 at 7:50











  • Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

    – Hang Wui
    Nov 24 '18 at 7:51











  • i have a concern about is there 9 record store in one i ?

    – Hang Wui
    Nov 24 '18 at 8:00














1












1








1







The printf function's "%s" format is for strings.
Strings in c are represented as one dimensional arrays (or a pointer to a portion of memory) containing characters and ended by a '' character.



Here you are trying to print a variable of type struct record with this format, which is undefined behaviour, in your example it happens that the bytes pointed by Arr + i(which is the address of the value Arr[i] of type struct record) are those of Arr[i].recordnum, therefore because you told printf to treat those bytes as string using the "%s" format, the characters are printed until a '' character is met. However, this is undefined behaviour because depending on architectures, structs might have padding bytes at the beginning instead of the bytes of their first field.



There is no printf format to print your custom struct, so you need to manually print each of your struct fields using their respective formats, in your case %s.



printf("%s, ..., %sn", Arr[i].recordnum, ..., Arr[i].status);





share|improve this answer













The printf function's "%s" format is for strings.
Strings in c are represented as one dimensional arrays (or a pointer to a portion of memory) containing characters and ended by a '' character.



Here you are trying to print a variable of type struct record with this format, which is undefined behaviour, in your example it happens that the bytes pointed by Arr + i(which is the address of the value Arr[i] of type struct record) are those of Arr[i].recordnum, therefore because you told printf to treat those bytes as string using the "%s" format, the characters are printed until a '' character is met. However, this is undefined behaviour because depending on architectures, structs might have padding bytes at the beginning instead of the bytes of their first field.



There is no printf format to print your custom struct, so you need to manually print each of your struct fields using their respective formats, in your case %s.



printf("%s, ..., %sn", Arr[i].recordnum, ..., Arr[i].status);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 7:39









alamitalamit

36719




36719













  • that right. so my content is stored in my array successfully?

    – Hang Wui
    Nov 24 '18 at 7:48











  • Yes @HangWui, but you should check it out by yourself to be sure.

    – alamit
    Nov 24 '18 at 7:50











  • Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

    – Hang Wui
    Nov 24 '18 at 7:51











  • i have a concern about is there 9 record store in one i ?

    – Hang Wui
    Nov 24 '18 at 8:00



















  • that right. so my content is stored in my array successfully?

    – Hang Wui
    Nov 24 '18 at 7:48











  • Yes @HangWui, but you should check it out by yourself to be sure.

    – alamit
    Nov 24 '18 at 7:50











  • Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

    – Hang Wui
    Nov 24 '18 at 7:51











  • i have a concern about is there 9 record store in one i ?

    – Hang Wui
    Nov 24 '18 at 8:00

















that right. so my content is stored in my array successfully?

– Hang Wui
Nov 24 '18 at 7:48





that right. so my content is stored in my array successfully?

– Hang Wui
Nov 24 '18 at 7:48













Yes @HangWui, but you should check it out by yourself to be sure.

– alamit
Nov 24 '18 at 7:50





Yes @HangWui, but you should check it out by yourself to be sure.

– alamit
Nov 24 '18 at 7:50













Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

– Hang Wui
Nov 24 '18 at 7:51





Because i need to add delete function in my program, when I type the recordNum, whole rekevant record will be deleted @alamit

– Hang Wui
Nov 24 '18 at 7:51













i have a concern about is there 9 record store in one i ?

– Hang Wui
Nov 24 '18 at 8:00





i have a concern about is there 9 record store in one i ?

– Hang Wui
Nov 24 '18 at 8:00


















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%2f53455860%2fc-how-can-i-put-all-file-io-record-in-my-array-successfully-thankyou%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

Basket-ball féminin

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...