Need help reading information from file program in C











up vote
-1
down vote

favorite












#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;

struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];

int student_no, i=0, choice=0;
char keyword[50];

FILE *fptr;

printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);

if(choice == 1){

fptr = (fopen("userInfo.txt","ab"));

system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");

for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);

printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);

printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){

fptr = (fopen("userInfo.txt","rb+"));

system("CLS");

printf("What students information would you like to retreive?: ");
scanf("%s",keyword);

fseek(fptr, sizeof(struct student), SEEK_SET);

fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);

fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);

printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);

system("PAUSE");

fclose(fptr);
return 0;
}
}


This is my code, input into the file looks like this:
Name: Riley

ID: 1

Date of birth: 01/10/2001



When I read the information from the file I get correct information but not all of it, when read in the message looks like this:
Name: y

ID: 1

Date of birth: 10/2001/2686248



The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!










share|improve this question


















  • 2




    Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
    – Yunnosch
    Nov 21 at 16:37






  • 3




    Why is every fread size sizeof(struct student)?
    – Osiris
    Nov 21 at 16:38






  • 2




    fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
    – Osiris
    Nov 21 at 16:40

















up vote
-1
down vote

favorite












#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;

struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];

int student_no, i=0, choice=0;
char keyword[50];

FILE *fptr;

printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);

if(choice == 1){

fptr = (fopen("userInfo.txt","ab"));

system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");

for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);

printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);

printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){

fptr = (fopen("userInfo.txt","rb+"));

system("CLS");

printf("What students information would you like to retreive?: ");
scanf("%s",keyword);

fseek(fptr, sizeof(struct student), SEEK_SET);

fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);

fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);

printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);

system("PAUSE");

fclose(fptr);
return 0;
}
}


This is my code, input into the file looks like this:
Name: Riley

ID: 1

Date of birth: 01/10/2001



When I read the information from the file I get correct information but not all of it, when read in the message looks like this:
Name: y

ID: 1

Date of birth: 10/2001/2686248



The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!










share|improve this question


















  • 2




    Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
    – Yunnosch
    Nov 21 at 16:37






  • 3




    Why is every fread size sizeof(struct student)?
    – Osiris
    Nov 21 at 16:38






  • 2




    fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
    – Osiris
    Nov 21 at 16:40















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;

struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];

int student_no, i=0, choice=0;
char keyword[50];

FILE *fptr;

printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);

if(choice == 1){

fptr = (fopen("userInfo.txt","ab"));

system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");

for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);

printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);

printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){

fptr = (fopen("userInfo.txt","rb+"));

system("CLS");

printf("What students information would you like to retreive?: ");
scanf("%s",keyword);

fseek(fptr, sizeof(struct student), SEEK_SET);

fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);

fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);

printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);

system("PAUSE");

fclose(fptr);
return 0;
}
}


This is my code, input into the file looks like this:
Name: Riley

ID: 1

Date of birth: 01/10/2001



When I read the information from the file I get correct information but not all of it, when read in the message looks like this:
Name: y

ID: 1

Date of birth: 10/2001/2686248



The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!










share|improve this question













#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;

struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];

int student_no, i=0, choice=0;
char keyword[50];

FILE *fptr;

printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);

if(choice == 1){

fptr = (fopen("userInfo.txt","ab"));

system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");

for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);

printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);

printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){

fptr = (fopen("userInfo.txt","rb+"));

system("CLS");

printf("What students information would you like to retreive?: ");
scanf("%s",keyword);

fseek(fptr, sizeof(struct student), SEEK_SET);

fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);

fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);

printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);

system("PAUSE");

fclose(fptr);
return 0;
}
}


This is my code, input into the file looks like this:
Name: Riley

ID: 1

Date of birth: 01/10/2001



When I read the information from the file I get correct information but not all of it, when read in the message looks like this:
Name: y

ID: 1

Date of birth: 10/2001/2686248



The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!







c arrays file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 16:32









Krytec

124




124








  • 2




    Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
    – Yunnosch
    Nov 21 at 16:37






  • 3




    Why is every fread size sizeof(struct student)?
    – Osiris
    Nov 21 at 16:38






  • 2




    fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
    – Osiris
    Nov 21 at 16:40
















  • 2




    Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
    – Yunnosch
    Nov 21 at 16:37






  • 3




    Why is every fread size sizeof(struct student)?
    – Osiris
    Nov 21 at 16:38






  • 2




    fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
    – Osiris
    Nov 21 at 16:40










2




2




Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37




Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37




3




3




Why is every fread size sizeof(struct student)?
– Osiris
Nov 21 at 16:38




Why is every fread size sizeof(struct student)?
– Osiris
Nov 21 at 16:38




2




2




fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
– Osiris
Nov 21 at 16:40






fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr); would be the proper way to write one entry to the file. Same goes for read.
– Osiris
Nov 21 at 16:40














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I doubt you're writing or reading what you intend.



When you write,



fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);


you're writing 1 element of sizeof(struct student) bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student) is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!



Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.



fwrite(&stu_data[i], sizeof(struct student), 1, fptr);


As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:



fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);


The same advice applies to reading. I'll only point out one other error:



    fseek(fptr, sizeof(struct student), SEEK_SET);


No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student) bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.



Also, hexdump(1) is your friend, especially the -C option.






share|improve this answer





















  • Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
    – Krytec
    Nov 26 at 16:07











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',
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%2f53416607%2fneed-help-reading-information-from-file-program-in-c%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








up vote
0
down vote



accepted










I doubt you're writing or reading what you intend.



When you write,



fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);


you're writing 1 element of sizeof(struct student) bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student) is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!



Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.



fwrite(&stu_data[i], sizeof(struct student), 1, fptr);


As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:



fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);


The same advice applies to reading. I'll only point out one other error:



    fseek(fptr, sizeof(struct student), SEEK_SET);


No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student) bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.



Also, hexdump(1) is your friend, especially the -C option.






share|improve this answer





















  • Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
    – Krytec
    Nov 26 at 16:07















up vote
0
down vote



accepted










I doubt you're writing or reading what you intend.



When you write,



fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);


you're writing 1 element of sizeof(struct student) bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student) is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!



Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.



fwrite(&stu_data[i], sizeof(struct student), 1, fptr);


As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:



fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);


The same advice applies to reading. I'll only point out one other error:



    fseek(fptr, sizeof(struct student), SEEK_SET);


No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student) bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.



Also, hexdump(1) is your friend, especially the -C option.






share|improve this answer





















  • Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
    – Krytec
    Nov 26 at 16:07













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I doubt you're writing or reading what you intend.



When you write,



fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);


you're writing 1 element of sizeof(struct student) bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student) is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!



Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.



fwrite(&stu_data[i], sizeof(struct student), 1, fptr);


As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:



fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);


The same advice applies to reading. I'll only point out one other error:



    fseek(fptr, sizeof(struct student), SEEK_SET);


No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student) bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.



Also, hexdump(1) is your friend, especially the -C option.






share|improve this answer












I doubt you're writing or reading what you intend.



When you write,



fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);


you're writing 1 element of sizeof(struct student) bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student) is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!



Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.



fwrite(&stu_data[i], sizeof(struct student), 1, fptr);


As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:



fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);


The same advice applies to reading. I'll only point out one other error:



    fseek(fptr, sizeof(struct student), SEEK_SET);


No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student) bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.



Also, hexdump(1) is your friend, especially the -C option.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 23:14









James K. Lowden

5,08411024




5,08411024












  • Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
    – Krytec
    Nov 26 at 16:07


















  • Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
    – Krytec
    Nov 26 at 16:07
















Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07




Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53416607%2fneed-help-reading-information-from-file-program-in-c%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)